Readthedocs
cyclopts.readthedocs.io › en › latest › vs_typer › argument_vs_option › README.html
Argument vs Option - Cyclopts - Read the Docs
Argument results in a positional CLI argument. Option results in a keyword CLI argument, preceded with a --.
Typer
typer.tiangolo.com › tutorial › arguments › optional
Optional CLI Arguments - Typer
Notice that "Camila" here is an optional CLI argument, not a CLI option, because we didn't use something like "--name Camila", we just passed "Camila" directly to the program. Typer also supports another older alternative syntax for declaring CLI arguments with additional metadata.
Jacob Kaplan-Moss
jacobian.org › til › common-arguments-with-typer
Common Arguments With Typer - Jacob Kaplan-Moss
But I wanted --pinboard-token to be an option on the main command, not on the subcommands. It’s essentially required, so I need to check for it, and didn’t want to do that multiple times. I also want to not repeat myself in each command. ... import typer from types import SimpleNamespace cli = typer.Typer() @cli.callback() def main( ctx: typer.Context, pinboard_token: str = typer.Option(None, envvar="PINBOARD_TOKEN"), ): if not pinboard_token: print( "Missing pinboard token; pass --pinboard-token or set env[PINBOARD_TOKEN]" ) raise typer.Exit(1) ctx.obj = SimpleNamespace(pinboard_token = pinboard_token) @cli.command() def all(ctx: typer.Context): # do something with ctx.obj.pinboard_token @cli.command() def new(ctx: typer.Context): # do something with ctx.obj.pinboard_token if __name__ == "__main__": cli()
Calmcode
calmcode.io › course › typer › options
Calmcode - typer: Options
Arguments are determined by the order they are passed while options have a specific name attached and the other no longer matters. import typer from typing import List from pathlib import Path app = typer.Typer(name="demo", add_completion=False, ...
Typer
typer.tiangolo.com › tutorial › options › name
CLI Option Name - Typer
In Typer you can also define CLI option short names the same way you can customize the long names. You can pass positional arguments to typer.Option() to define the CLI option name(s).
Typer
typer.tiangolo.com › tutorial › options › required
Required CLI Options - Typer
To make a CLI option required, you can put typer.Option() inside of Annotated and leave the parameter without a default value.
Stack Overflow
stackoverflow.com › questions › 79454429 › typer-option-in-python
parameter passing - typer.Option in python - Stack Overflow
also I tried to make default value "all" instead of None but still not working. I always get this error: Option [flag name] requires an argument
GitHub
github.com › fastapi › typer › issues › 554
DOC: Documentation of passing multiple values in "option" vs "argument" isn't sufficiently explicit · Issue #554 · fastapi/typer
February 14, 2023 - It's only possible to pass an exact number of space separated values via an option, but not a list: https://typer.tiangolo.com/tutorial/multiple-values/options-with-multiple-values/ Multiple values are only possible via argument, yet the documentation for the two types reads almost identical (at least the first few paragraphs): https://typer.tiangolo.com/tutorial/multiple-values/arguments-with-multiple-values/
Author: fastapi
Typer
typer.tiangolo.com › tutorial › parameter-types
CLI Parameter Types - Typer
In this example, the value received for the CLI argument name will be treated as str. The value for the CLI option --age will be converted to an int and --height-meters will be converted to a float. And as female is a bool CLI option, Typer will convert it to a "flag" --female and the counterpart --no-female.
Typer
typer.tiangolo.com › tutorial › arguments › default
CLI Arguments with Default - Typer
But the use of Optional doesn't affect Typer in any way, e.g. it doesn't tell Typer if a value is required or not. ... // Check the help $ uv run python main.py --help // Notice the [default: Wade Wilson] ✨ Usage: main.py [OPTIONS] [name] Arguments: name [default: Wade Wilson] Options: --help ...
Stack Overflow
stackoverflow.com › questions › 75649465 › python-typer-one-argument-value-required-for-another-argument
command line interface - Python Typer one argument value required for another argument - Stack Overflow
def app( device: str = typer.Option( None, "--device", "-d", help="Select the device you want to proceed with apples, oranges, banana", callback=__device_callback is_eager=True, ), soc: Optional[str] = typer.Option( "EFR", "--soc", "-s", help="Select the soc for the device type apples.
Typer
typer.tiangolo.com › reference › parameters
Parameters - Typer - Tiangolo.com
Parameters to our command line interface may be both CLI Options and CLI Arguments: from typing import Annotated import typer app = typer.Typer() @app.command() def register( user: Annotated[str, typer.Argument()], age: Annotated[int, typer.Option(min=18)], score: Annotated[float, typer.Option(max=100)] = 0, ): print(f"User is {user}") print(f"--age is {age}") print(f"--score is {score}") if __name__ == "__main__": app() Argument( default=..., *, callback=None, metavar=None, expose_value=True, is_eager=False, envvar=None, shell_complete=None, autocompletion=None, default_factory=None, parser=N
GitHub
github.com › fastapi › typer › issues › 405
Need feature to share options or arguments between commands · Issue #405 · fastapi/typer
June 15, 2022 - I already checked if it is not related to Typer but to Click. ... from typing import Optional, List import typer import os app = typer.Typer() @app.command() def start( debug: bool = typer.Option(False), output_dir: str = typer.Option(os.getcwd()), flows: Optional[List[str]] = typer.Option(None, "--flow", "-f")): typer.echo(f"Debug mode: {debug}") typer.echo(f"Output Dir: {output_dir}") typer.echo(f"start flows: {flows}") @app.command() def stop( debug: bool = typer.Option(False), output_dir: str = typer.Option(os.getcwd())): typer.echo(f"Debug mode: {debug}") typer.echo(f"Output Dir: {output_dir}") typer.echo("STOP!") @app.command() def clean( debug: bool = typer.Option(False), output_dir: str = typer.Option(os.getcwd())): typer.echo(f"Debug mode: {debug}") typer.echo(f"Output Dir: {output_dir}") typer.echo("STOP!") if __name__ == "__main__": app()
Author: fastapi
Typer
typer.tiangolo.com › tutorial › commands › arguments
Command CLI Arguments - Typer
The same way as with a CLI application with a single command, subcommands (or just "commands") can also have their own CLI arguments: ... import typer app = typer.Typer() @app.command() def create(username: str): print(f"Creating user: {username}") @app.command() def delete(username: str): print(f"Deleting user: {username}") if __name__ == "__main__": app() // Check the help for create $ uv run python main.py create --help Usage: main.py create [OPTIONS] {username} Arguments: username [required] Options: --help Show this message and exit.
Top answer 1 of 2
1
from typing import List, Optional
import typer
def main(device: Optional[List[str]] = typer.Option(None)):
if not device:
print("No provided devices")
raise typer.Abort()
for d in device:
print(f"Processing device: {d}")
if __name__ == "__main__":
typer.run(main)
And then it can be used like in:
$ ./my_cli --device 1 --device 2
Take a look at the documentation.
If the only input for the CLI is a list of devices, you can simplify it to using arguments instead. Then it could be used like:
$ ./my_cli 1 2
2 of 2
-2
from pathlib import Path
from typing import List
import typer
def main(files: List[Path], celebration: str):
for path in files:
if path.is_file():
print(f"This file exists: {path.name}")
print(celebration)
if __name__ == "__main__":
typer.run(main)