CLI Parameter Types Intro
You can use several data types for the CLI options and CLI arguments, and you can add data validation requirements too.
Data conversionΒΆ
When you declare a CLI parameter with some type Typer will convert the data received from the command line to that data type.
For example:
import typer_cloup as typer
def main(name: str, age: int = 20, height_meters: float = 1.89, female: bool = True):
typer.echo(f"NAME is {name}, of type: {type(name)}")
typer.echo(f"--age is {age}, of type: {type(age)}")
typer.echo(f"--height-meters is {height_meters}, of type: {type(height_meters)}")
typer.echo(f"--female is {female}, of type: {type(female)}")
if __name__ == "__main__":
typer.run(main)
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.
And here's how it looks like:
Watch nextΒΆ
See more about specific types and validations in the next sections...
Technical Details
All the types you will see in the next sections are handled underneath by Click's Parameter Types.