Custom Types
You can easily use your own custom types in your Typer applications.
The way to do it is by providing a way to parse input into your own types.
There are two ways to achieve this:
- Adding a type
parser
- Expanding Click's custom types
Type Parser
typer.Argument
and typer.Option
can create custom parameter types with a parser
callable.
//// tab | Python 3.7+
{!> ../docs_src/parameter_types/custom_types/tutorial001_an.py!}
////
//// tab | Python 3.7+ non-Annotated
/// tip
Prefer to use the Annotated
version if possible.
///
{!> ../docs_src/parameter_types/custom_types/tutorial001.py!}
////
The function (or callable) that you pass to the parameter parser
will receive the input value as a string and should return the parsed value with your own custom type.
Click Custom Type
If you already have a Click Custom Type, you can use it in typer.Argument()
and typer.Option()
with the click_type
parameter.
//// tab | Python 3.7+
{!> ../docs_src/parameter_types/custom_types/tutorial002_an.py!}
////