[QUESTION] eagerness to mimic click.option ?
See original GitHub issueFirst check
- I used the GitHub search to find a similar issue and didn’t find it.
- I searched the Typer documentation, with the integrated search.
- I already searched in Google “How to X in Typer” and didn’t find any information.
- I already searched in Google “How to X in Click” and didn’t find any information.
Description
how can I mimic the following
@click.option(
"--version",
is_flag=True,
callback=print_version,
expose_value=False,
is_eager=True,
help="Display version and exit.",
)
added to a command it shortcuts the command to run the callback (described here: https://click.palletsprojects.com/en/7.x/options/#callbacks-and-eager-options)
I tried the following yet the callback is called no matter a command is or is not entered, even --help is shortcut so I may be using the typer.Option wrongly or the is_eager kwarg is not used, no idea 😃
click version:
def print_version(ctx, param, value):
if not value or ctx.resilient_parsing:
return
click.echo('Version 1.0')
ctx.exit()
@click.command()
@click.option('--version', is_flag=True, callback=print_version,
expose_value=False, is_eager=True)
def hello():
click.echo('Hello World!')
$ hello
Hello World!
$ hello --version
Version 1.0
typer version
import typer
app = typer.Typer()
def print_version(ctx, param, value):
if not value or ctx.resilient_parsing:
return
typer.echo('Version 1.0')
ctx.exit()
@app.command()
def hello(version: str = typer.Option('--version', is_flag=True, callback=print_version, expose_value=False, is_eager=True)):
typer.echo('Hello World!')
if __name__ == '__main__':
app()
$ python version.py
Version 1.0
$ python version.py --version
Error: --version option requires an argument
$ python version.py version
Version 1.0
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Noninteractive confirmation of eager options in the Python ...
I achieved this by using an eager option that executes the function config_reset if --reset is present, instead of executing the main function ......
Read more >50 Call-to-Action Examples You Can't Help But Click
Check out these call-to-action examples to inspire you to craft a click-worthy CTA.
Read more >How to simulate a click with JavaScript ? - GeeksforGeeks
Method 1: Using the click() method: The click() method is used to simulate a mouse click on an element. It fires the click...
Read more >The Witcher 3: Wild Hunt - to simulate or not to ... - VG247
A confusing early decision with The Witcher 3 is whether to similar a Witcher 2 save or not. Here's what that means and...
Read more >Frequently Asked Questions About Notaries
Personal Appearance · Verify the Document · Identify the Signer and the Signer's Willingness/Awareness to Sign · Complete the Notarial Act and Notarial...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Hi, Is there a way to have a
--version-type eager option with required Arguments? That is, the arguments are required unless you’re just calling--version.thanks