Typer class¶
Here's the reference information for the Typer class, with all its parameters, attributes and methods.
You can import the Typer class directly from typer:
from typer import Typer
typer.Typer
¶
Typer(
*,
name=Default(None),
cls=Default(None),
invoke_without_command=Default(False),
no_args_is_help=Default(False),
subcommand_metavar=Default(None),
chain=Default(False),
result_callback=Default(None),
context_settings=Default(None),
callback=Default(None),
help=Default(None),
epilog=Default(None),
short_help=Default(None),
options_metavar=Default("[OPTIONS]"),
add_help_option=Default(True),
hidden=Default(False),
deprecated=Default(False),
add_completion=True,
rich_markup_mode=DEFAULT_MARKUP_MODE,
rich_help_panel=Default(None),
suggest_commands=True,
pretty_exceptions_enable=True,
pretty_exceptions_show_locals=False,
pretty_exceptions_short=True,
)
Typer main class, the main entrypoint to use Typer.
Read more in the Typer docs for First Steps.
Example¶
import typer
app = typer.Typer()
| PARAMETER | DESCRIPTION |
|---|---|
name
|
The name of this application.
Mostly used to set the name for subcommands, in which case it can be overridden by Example
TYPE:
|
cls
|
The class of this app. Mainly used when using the Click library underneath. Can usually be left at the default value Example
TYPE:
|
invoke_without_command
|
By setting this to Example
TYPE:
|
no_args_is_help
|
If this is set to Example
TYPE:
|
subcommand_metavar
|
Note: you probably shouldn't use this parameter, it is inherited from Click and supported for compatibility. How to represent the subcommand argument in help.
TYPE:
|
chain
|
Note: you probably shouldn't use this parameter, it is inherited from Click and supported for compatibility. Allow passing more than one subcommand argument.
TYPE:
|
result_callback
|
Note: you probably shouldn't use this parameter, it is inherited from Click and supported for compatibility. A function to call after the group's and subcommand's callbacks.
TYPE:
|
context_settings
|
Pass configurations for the context.
Available configurations can be found in the docs for Click's Example
TYPE:
|
callback
|
Add a callback to the main Typer app. Can be overridden with Example
TYPE:
|
help
|
Help text for the main Typer app. See the tutorial about name and help for different ways of setting a command's help, and which one takes priority. Example
TYPE:
|
epilog
|
Text that will be printed right after the help text. Example
TYPE:
|
short_help
|
A shortened version of the help text that can be used e.g. in the help table listing subcommands.
When not defined, the normal Example
TYPE:
|
options_metavar
|
In the example usage string of the help text for a command, the default placeholder for various arguments is Example
TYPE:
|
add_help_option
|
Note: you probably shouldn't use this parameter, it is inherited from Click and supported for compatibility. By default each command registers a
TYPE:
|
hidden
|
Hide this command from help outputs. Example
TYPE:
|
deprecated
|
Mark this command as being deprecated in the help text. Example
TYPE:
|
add_completion
|
Toggle whether or not to add the Example
TYPE:
|
rich_markup_mode
|
Enable markup text if you have Rich installed. This can be set to Example
TYPE:
|
rich_help_panel
|
Set the panel name of the command when the help is printed with Rich. Example
TYPE:
|
suggest_commands
|
As of version 0.20.0, Typer provides support for mistyped command names by printing helpful suggestions.
You can turn this setting off with Example
TYPE:
|
pretty_exceptions_enable
|
If you want to disable pretty exceptions with Rich,
you can set Example
TYPE:
|
pretty_exceptions_show_locals
|
If Rich is installed, error messages will be nicely printed. If you set However, if such a variable contains delicate information, you should consider leaving Example
TYPE:
|
pretty_exceptions_short
|
By default, pretty exceptions formatted with Rich hide the long stack trace.
If you want to show the full trace instead, you can set the parameter Example
TYPE:
|
Source code in typer/main.py
def __init__(
self,
*,
name: Annotated[
str | None,
Doc(
"""
The name of this application.
Mostly used to set the name for [subcommands](https://typer.tiangolo.com/tutorial/subcommands/), in which case it can be overridden by `add_typer(name=...)`.
**Example**
```python
import typer
app = typer.Typer(name="users")
```
"""
),
] = Default(None),
cls: Annotated[
type[TyperGroup] | None,
Doc(
"""
The class of this app. Mainly used when [using the Click library underneath](https://typer.tiangolo.com/tutorial/using-click/). Can usually be left at the default value `None`.
Otherwise, should be a subtype of `TyperGroup`.
**Example**
```python
import typer
app = typer.Typer(cls=TyperGroup)
```
"""
),
] = Default(None),
invoke_without_command: Annotated[
bool,
Doc(
"""
By setting this to `True`, you can make sure a callback is executed even when no subcommand is provided.
**Example**
```python
import typer
app = typer.Typer(invoke_without_command=True)
```
"""
),
] = Default(False),
no_args_is_help: Annotated[
bool,
Doc(
"""
If this is set to `True`, running a command without any arguments will automatically show the help page.
**Example**
```python
import typer
app = typer.Typer(no_args_is_help=True)
```
"""
),
] = Default(False),
subcommand_metavar: Annotated[
str | None,
Doc(
"""
**Note**: you probably shouldn't use this parameter, it is inherited
from Click and supported for compatibility.
---
How to represent the subcommand argument in help.
"""
),
] = Default(None),
chain: Annotated[
bool,
Doc(
"""
**Note**: you probably shouldn't use this parameter, it is inherited
from Click and supported for compatibility.
---
Allow passing more than one subcommand argument.
"""
),
] = Default(False),
result_callback: Annotated[
Callable[..., Any] | None,
Doc(
"""
**Note**: you probably shouldn't use this parameter, it is inherited
from Click and supported for compatibility.
---
A function to call after the group's and subcommand's callbacks.
"""
),
] = Default(None),
# Command
context_settings: Annotated[
dict[Any, Any] | None,
Doc(
"""
Pass configurations for the [context](https://typer.tiangolo.com/tutorial/commands/context/).
Available configurations can be found in the docs for Click's `Context` [here](https://click.palletsprojects.com/en/stable/api/#context).
**Example**
```python
import typer
app = typer.Typer(context_settings={"help_option_names": ["-h", "--help"]})
```
"""
),
] = Default(None),
callback: Annotated[
Callable[..., Any] | None,
Doc(
"""
Add a callback to the main Typer app. Can be overridden with `@app.callback()`.
See [the tutorial about callbacks](https://typer.tiangolo.com/tutorial/commands/callback/) for more details.
**Example**
```python
import typer
def callback():
print("Running a command")
app = typer.Typer(callback=callback)
```
"""
),
] = Default(None),
help: Annotated[
str | None,
Doc(
"""
Help text for the main Typer app.
See [the tutorial about name and help](https://typer.tiangolo.com/tutorial/subcommands/name-and-help) for different ways of setting a command's help,
and which one takes priority.
**Example**
```python
import typer
app = typer.Typer(help="Some help.")
```
"""
),
] = Default(None),
epilog: Annotated[
str | None,
Doc(
"""
Text that will be printed right after the help text.
**Example**
```python
import typer
app = typer.Typer(epilog="May the force be with you")
```
"""
),
] = Default(None),
short_help: Annotated[
str | None,
Doc(
"""
A shortened version of the help text that can be used e.g. in the help table listing subcommands.
When not defined, the normal `help` text will be used instead.
**Example**
```python
import typer
app = typer.Typer(help="A lot of explanation about user management", short_help="user management")
```
"""
),
] = Default(None),
options_metavar: Annotated[
str,
Doc(
"""
In the example usage string of the help text for a command, the default placeholder for various arguments is `[OPTIONS]`.
Set `options_metavar` to change this into a different string.
**Example**
```python
import typer
app = typer.Typer(options_metavar="[OPTS]")
```
"""
),
] = Default("[OPTIONS]"),
add_help_option: Annotated[
bool,
Doc(
"""
**Note**: you probably shouldn't use this parameter, it is inherited
from Click and supported for compatibility.
---
By default each command registers a `--help` option. This can be disabled by this parameter.
"""
),
] = Default(True),
hidden: Annotated[
bool,
Doc(
"""
Hide this command from help outputs. `False` by default.
**Example**
```python
import typer
app = typer.Typer(hidden=True)
```
"""
),
] = Default(False),
deprecated: Annotated[
bool,
Doc(
"""
Mark this command as being deprecated in the help text. `False` by default.
**Example**
```python
import typer
app = typer.Typer(deprecated=True)
```
"""
),
] = Default(False),
add_completion: Annotated[
bool,
Doc(
"""
Toggle whether or not to add the `--install-completion` and `--show-completion` options to the app.
Set to `True` by default.
**Example**
```python
import typer
app = typer.Typer(add_completion=False)
```
"""
),
] = True,
# Rich settings
rich_markup_mode: Annotated[
MarkupMode,
Doc(
"""
Enable markup text if you have Rich installed. This can be set to `"markdown"`, `"rich"`, or `None`.
By default, `rich_markup_mode` is `None` if Rich is not installed, and `"rich"` if it is installed.
See [the tutorial on help formatting](https://typer.tiangolo.com/tutorial/commands/help/#rich-markdown-and-markup) for more information.
**Example**
```python
import typer
app = typer.Typer(rich_markup_mode="rich")
```
"""
),
] = DEFAULT_MARKUP_MODE,
rich_help_panel: Annotated[
str | None,
Doc(
"""
Set the panel name of the command when the help is printed with Rich.
**Example**
```python
import typer
app = typer.Typer(rich_help_panel="Utils and Configs")
```
"""
),
] = Default(None),
suggest_commands: Annotated[
bool,
Doc(
"""
As of version 0.20.0, Typer provides [support for mistyped command names](https://typer.tiangolo.com/tutorial/commands/help/#suggest-commands) by printing helpful suggestions.
You can turn this setting off with `suggest_commands`:
**Example**
```python
import typer
app = typer.Typer(suggest_commands=False)
```
"""
),
] = True,
pretty_exceptions_enable: Annotated[
bool,
Doc(
"""
If you want to disable [pretty exceptions with Rich](https://typer.tiangolo.com/tutorial/exceptions/#exceptions-with-rich),
you can set `pretty_exceptions_enable` to `False`. When doing so, you will see the usual standard exception trace.
**Example**
```python
import typer
app = typer.Typer(pretty_exceptions_enable=False)
```
"""
),
] = True,
pretty_exceptions_show_locals: Annotated[
bool,
Doc(
"""
If Rich is installed, [error messages](https://typer.tiangolo.com/tutorial/exceptions/#exceptions-and-errors)
will be nicely printed.
If you set `pretty_exceptions_show_locals=True` it will also include the values of local variables for easy debugging.
However, if such a variable contains delicate information, you should consider leaving `pretty_exceptions_show_locals=False`
(the default) to `False` to enhance security.
**Example**
```python
import typer
app = typer.Typer(pretty_exceptions_show_locals=True)
```
"""
),
] = False,
pretty_exceptions_short: Annotated[
bool,
Doc(
"""
By default, [pretty exceptions formatted with Rich](https://typer.tiangolo.com/tutorial/exceptions/#exceptions-with-rich) hide the long stack trace.
If you want to show the full trace instead, you can set the parameter `pretty_exceptions_short` to `False`:
**Example**
```python
import typer
app = typer.Typer(pretty_exceptions_short=False)
```
"""
),
] = True,
):
self._add_completion = add_completion
self.rich_markup_mode: MarkupMode = rich_markup_mode
self.rich_help_panel = rich_help_panel
self.suggest_commands = suggest_commands
self.pretty_exceptions_enable = pretty_exceptions_enable
self.pretty_exceptions_show_locals = pretty_exceptions_show_locals
self.pretty_exceptions_short = pretty_exceptions_short
self.info = TyperInfo(
name=name,
cls=cls,
invoke_without_command=invoke_without_command,
no_args_is_help=no_args_is_help,
subcommand_metavar=subcommand_metavar,
chain=chain,
result_callback=result_callback,
context_settings=context_settings,
callback=callback,
help=help,
epilog=epilog,
short_help=short_help,
options_metavar=options_metavar,
add_help_option=add_help_option,
hidden=hidden,
deprecated=deprecated,
)
self.registered_groups: list[TyperInfo] = []
self.registered_commands: list[CommandInfo] = []
self.registered_callback: TyperInfo | None = None
callback
¶
callback(
*,
cls=Default(None),
invoke_without_command=Default(False),
no_args_is_help=Default(False),
subcommand_metavar=Default(None),
chain=Default(False),
result_callback=Default(None),
context_settings=Default(None),
help=Default(None),
epilog=Default(None),
short_help=Default(None),
options_metavar=Default(None),
add_help_option=Default(True),
hidden=Default(False),
deprecated=Default(False),
rich_help_panel=Default(None),
)
Using the decorator @app.callback, you can declare the CLI parameters for the main CLI application.
Read more in the Typer docs for Callbacks.
Example¶
import typer
app = typer.Typer()
state = {"verbose": False}
@app.callback()
def main(verbose: bool = False):
if verbose:
print("Will write verbose output")
state["verbose"] = True
@app.command()
def delete(username: str):
# define subcommand
...
| PARAMETER | DESCRIPTION |
|---|---|
cls
|
The class of this app. Mainly used when using the Click library underneath. Can usually be left at the default value
TYPE:
|
invoke_without_command
|
By setting this to
TYPE:
|
no_args_is_help
|
If this is set to
TYPE:
|
subcommand_metavar
|
Note: you probably shouldn't use this parameter, it is inherited from Click and supported for compatibility. How to represent the subcommand argument in help.
TYPE:
|
chain
|
Note: you probably shouldn't use this parameter, it is inherited from Click and supported for compatibility. Allow passing more than one subcommand argument.
TYPE:
|
result_callback
|
Note: you probably shouldn't use this parameter, it is inherited from Click and supported for compatibility. A function to call after the group's and subcommand's callbacks.
TYPE:
|
context_settings
|
Pass configurations for the context.
Available configurations can be found in the docs for Click's
TYPE:
|
help
|
Help text for the command. See the tutorial about name and help for different ways of setting a command's help, and which one takes priority.
TYPE:
|
epilog
|
Text that will be printed right after the help text.
TYPE:
|
short_help
|
A shortened version of the help text that can be used e.g. in the help table listing subcommands.
When not defined, the normal
TYPE:
|
options_metavar
|
In the example usage string of the help text for a command, the default placeholder for various arguments is
TYPE:
|
add_help_option
|
Note: you probably shouldn't use this parameter, it is inherited from Click and supported for compatibility. By default each command registers a
TYPE:
|
hidden
|
Hide this command from help outputs.
TYPE:
|
deprecated
|
Mark this command as deprecated in the help text.
TYPE:
|
rich_help_panel
|
Set the panel name of the command when the help is printed with Rich.
TYPE:
|
Source code in typer/main.py
def callback(
self,
*,
cls: Annotated[
type[TyperGroup] | None,
Doc(
"""
The class of this app. Mainly used when [using the Click library underneath](https://typer.tiangolo.com/tutorial/using-click/). Can usually be left at the default value `None`.
Otherwise, should be a subtype of `TyperGroup`.
"""
),
] = Default(None),
invoke_without_command: Annotated[
bool,
Doc(
"""
By setting this to `True`, you can make sure a callback is executed even when no subcommand is provided.
"""
),
] = Default(False),
no_args_is_help: Annotated[
bool,
Doc(
"""
If this is set to `True`, running a command without any arguments will automatically show the help page.
"""
),
] = Default(False),
subcommand_metavar: Annotated[
str | None,
Doc(
"""
**Note**: you probably shouldn't use this parameter, it is inherited
from Click and supported for compatibility.
---
How to represent the subcommand argument in help.
"""
),
] = Default(None),
chain: Annotated[
bool,
Doc(
"""
**Note**: you probably shouldn't use this parameter, it is inherited
from Click and supported for compatibility.
---
Allow passing more than one subcommand argument.
"""
),
] = Default(False),
result_callback: Annotated[
Callable[..., Any] | None,
Doc(
"""
**Note**: you probably shouldn't use this parameter, it is inherited
from Click and supported for compatibility.
---
A function to call after the group's and subcommand's callbacks.
"""
),
] = Default(None),
# Command
context_settings: Annotated[
dict[Any, Any] | None,
Doc(
"""
Pass configurations for the [context](https://typer.tiangolo.com/tutorial/commands/context/).
Available configurations can be found in the docs for Click's `Context` [here](https://click.palletsprojects.com/en/stable/api/#context).
"""
),
] = Default(None),
help: Annotated[
str | None,
Doc(
"""
Help text for the command.
See [the tutorial about name and help](https://typer.tiangolo.com/tutorial/subcommands/name-and-help) for different ways of setting a command's help,
and which one takes priority.
"""
),
] = Default(None),
epilog: Annotated[
str | None,
Doc(
"""
Text that will be printed right after the help text.
"""
),
] = Default(None),
short_help: Annotated[
str | None,
Doc(
"""
A shortened version of the help text that can be used e.g. in the help table listing subcommands.
When not defined, the normal `help` text will be used instead.
"""
),
] = Default(None),
options_metavar: Annotated[
str | None,
Doc(
"""
In the example usage string of the help text for a command, the default placeholder for various arguments is `[OPTIONS]`.
Set `options_metavar` to change this into a different string. When `None`, the default value will be used.
"""
),
] = Default(None),
add_help_option: Annotated[
bool,
Doc(
"""
**Note**: you probably shouldn't use this parameter, it is inherited
from Click and supported for compatibility.
---
By default each command registers a `--help` option. This can be disabled by this parameter.
"""
),
] = Default(True),
hidden: Annotated[
bool,
Doc(
"""
Hide this command from help outputs. `False` by default.
"""
),
] = Default(False),
deprecated: Annotated[
bool,
Doc(
"""
Mark this command as deprecated in the help text. `False` by default.
"""
),
] = Default(False),
# Rich settings
rich_help_panel: Annotated[
str | None,
Doc(
"""
Set the panel name of the command when the help is printed with Rich.
"""
),
] = Default(None),
) -> Callable[[CommandFunctionType], CommandFunctionType]:
"""
Using the decorator `@app.callback`, you can declare the CLI parameters for the main CLI application.
Read more in the
[Typer docs for Callbacks](https://typer.tiangolo.com/tutorial/commands/callback/).
## Example
```python
import typer
app = typer.Typer()
state = {"verbose": False}
@app.callback()
def main(verbose: bool = False):
if verbose:
print("Will write verbose output")
state["verbose"] = True
@app.command()
def delete(username: str):
# define subcommand
...
```
"""
def decorator(f: CommandFunctionType) -> CommandFunctionType:
self.registered_callback = TyperInfo(
cls=cls,
invoke_without_command=invoke_without_command,
no_args_is_help=no_args_is_help,
subcommand_metavar=subcommand_metavar,
chain=chain,
result_callback=result_callback,
context_settings=context_settings,
callback=f,
help=help,
epilog=epilog,
short_help=short_help,
options_metavar=(
options_metavar or self._info_val_str("options_metavar")
),
add_help_option=add_help_option,
hidden=hidden,
deprecated=deprecated,
rich_help_panel=rich_help_panel,
)
return f
return decorator
command
¶
command(
name=None,
*,
cls=None,
context_settings=None,
help=None,
epilog=None,
short_help=None,
options_metavar=Default(None),
add_help_option=True,
no_args_is_help=False,
hidden=False,
deprecated=False,
rich_help_panel=Default(None),
)
Using the decorator @app.command, you can define a subcommand of the previously defined Typer app.
Read more in the Typer docs for Commands.
Example¶
import typer
app = typer.Typer()
@app.command()
def create():
print("Creating user: Hiro Hamada")
@app.command()
def delete():
print("Deleting user: Hiro Hamada")
| PARAMETER | DESCRIPTION |
|---|---|
name
|
The name of this command.
TYPE:
|
cls
|
The class of this command. Mainly used when using the Click library underneath. Can usually be left at the default value
TYPE:
|
context_settings
|
Pass configurations for the context.
Available configurations can be found in the docs for Click's
TYPE:
|
help
|
Help text for the command. See the tutorial about name and help for different ways of setting a command's help, and which one takes priority.
TYPE:
|
epilog
|
Text that will be printed right after the help text.
TYPE:
|
short_help
|
A shortened version of the help text that can be used e.g. in the help table listing subcommands.
When not defined, the normal
TYPE:
|
options_metavar
|
In the example usage string of the help text for a command, the default placeholder for various arguments is
TYPE:
|
add_help_option
|
Note: you probably shouldn't use this parameter, it is inherited from Click and supported for compatibility. By default each command registers a
TYPE:
|
no_args_is_help
|
If this is set to
TYPE:
|
hidden
|
Hide this command from help outputs.
TYPE:
|
deprecated
|
Mark this command as deprecated in the help outputs.
TYPE:
|
rich_help_panel
|
Set the panel name of the command when the help is printed with Rich.
TYPE:
|
Source code in typer/main.py
def command(
self,
name: Annotated[
str | None,
Doc(
"""
The name of this command.
"""
),
] = None,
*,
cls: Annotated[
type[TyperCommand] | None,
Doc(
"""
The class of this command. Mainly used when [using the Click library underneath](https://typer.tiangolo.com/tutorial/using-click/). Can usually be left at the default value `None`.
Otherwise, should be a subtype of `TyperCommand`.
"""
),
] = None,
context_settings: Annotated[
dict[Any, Any] | None,
Doc(
"""
Pass configurations for the [context](https://typer.tiangolo.com/tutorial/commands/context/).
Available configurations can be found in the docs for Click's `Context` [here](https://click.palletsprojects.com/en/stable/api/#context).
"""
),
] = None,
help: Annotated[
str | None,
Doc(
"""
Help text for the command.
See [the tutorial about name and help](https://typer.tiangolo.com/tutorial/subcommands/name-and-help) for different ways of setting a command's help,
and which one takes priority.
"""
),
] = None,
epilog: Annotated[
str | None,
Doc(
"""
Text that will be printed right after the help text.
"""
),
] = None,
short_help: Annotated[
str | None,
Doc(
"""
A shortened version of the help text that can be used e.g. in the help table listing subcommands.
When not defined, the normal `help` text will be used instead.
"""
),
] = None,
options_metavar: Annotated[
str | None,
Doc(
"""
In the example usage string of the help text for a command, the default placeholder for various arguments is `[OPTIONS]`.
Set `options_metavar` to change this into a different string. When `None`, the default value will be used.
"""
),
] = Default(None),
add_help_option: Annotated[
bool,
Doc(
"""
**Note**: you probably shouldn't use this parameter, it is inherited
from Click and supported for compatibility.
---
By default each command registers a `--help` option. This can be disabled by this parameter.
"""
),
] = True,
no_args_is_help: Annotated[
bool,
Doc(
"""
If this is set to `True`, running a command without any arguments will automatically show the help page.
"""
),
] = False,
hidden: Annotated[
bool,
Doc(
"""
Hide this command from help outputs. `False` by default.
"""
),
] = False,
deprecated: Annotated[
bool,
Doc(
"""
Mark this command as deprecated in the help outputs. `False` by default.
"""
),
] = False,
# Rich settings
rich_help_panel: Annotated[
str | None,
Doc(
"""
Set the panel name of the command when the help is printed with Rich.
"""
),
] = Default(None),
) -> Callable[[CommandFunctionType], CommandFunctionType]:
"""
Using the decorator `@app.command`, you can define a subcommand of the previously defined Typer app.
Read more in the
[Typer docs for Commands](https://typer.tiangolo.com/tutorial/commands/).
## Example
```python
import typer
app = typer.Typer()
@app.command()
def create():
print("Creating user: Hiro Hamada")
@app.command()
def delete():
print("Deleting user: Hiro Hamada")
```
"""
if cls is None:
cls = TyperCommand
def decorator(f: CommandFunctionType) -> CommandFunctionType:
self.registered_commands.append(
CommandInfo(
name=name,
cls=cls,
context_settings=context_settings,
callback=f,
help=help,
epilog=epilog,
short_help=short_help,
options_metavar=(
options_metavar or self._info_val_str("options_metavar")
),
add_help_option=add_help_option,
no_args_is_help=no_args_is_help,
hidden=hidden,
deprecated=deprecated,
# Rich settings
rich_help_panel=rich_help_panel,
)
)
return f
return decorator
add_typer
¶
add_typer(
typer_instance,
*,
name=Default(None),
cls=Default(None),
invoke_without_command=Default(False),
no_args_is_help=Default(False),
subcommand_metavar=Default(None),
chain=Default(False),
result_callback=Default(None),
context_settings=Default(None),
callback=Default(None),
help=Default(None),
epilog=Default(None),
short_help=Default(None),
options_metavar=Default(None),
add_help_option=Default(True),
hidden=Default(False),
deprecated=False,
rich_help_panel=Default(None),
)
Add subcommands to the main app using app.add_typer().
Subcommands may be defined in separate modules, ensuring clean separation of code by functionality.
Read more in the Typer docs for SubCommands.
Example¶
import typer
from .add import app as add_app
from .delete import app as delete_app
app = typer.Typer()
app.add_typer(add_app)
app.add_typer(delete_app)
| PARAMETER | DESCRIPTION |
|---|---|
name
|
The name of this subcommand. See the tutorial about name and help for different ways of setting a command's name, and which one takes priority.
TYPE:
|
cls
|
The class of this subcommand. Mainly used when using the Click library underneath. Can usually be left at the default value
TYPE:
|
invoke_without_command
|
By setting this to
TYPE:
|
no_args_is_help
|
If this is set to
TYPE:
|
subcommand_metavar
|
Note: you probably shouldn't use this parameter, it is inherited from Click and supported for compatibility. How to represent the subcommand argument in help.
TYPE:
|
chain
|
Note: you probably shouldn't use this parameter, it is inherited from Click and supported for compatibility. Allow passing more than one subcommand argument.
TYPE:
|
result_callback
|
Note: you probably shouldn't use this parameter, it is inherited from Click and supported for compatibility. A function to call after the group's and subcommand's callbacks.
TYPE:
|
context_settings
|
Pass configurations for the context.
Available configurations can be found in the docs for Click's
TYPE:
|
callback
|
Add a callback to this app. See the tutorial about callbacks for more details.
TYPE:
|
help
|
Help text for the subcommand. See the tutorial about name and help for different ways of setting a command's help, and which one takes priority.
TYPE:
|
epilog
|
Text that will be printed right after the help text.
TYPE:
|
short_help
|
A shortened version of the help text that can be used e.g. in the help table listing subcommands.
When not defined, the normal
TYPE:
|
options_metavar
|
In the example usage string of the help text for a command, the default placeholder for various arguments is
TYPE:
|
add_help_option
|
Note: you probably shouldn't use this parameter, it is inherited from Click and supported for compatibility. By default each command registers a
TYPE:
|
hidden
|
Hide this command from help outputs.
TYPE:
|
deprecated
|
Mark this command as deprecated in the help outputs.
TYPE:
|
rich_help_panel
|
Set the panel name of the command when the help is printed with Rich.
TYPE:
|
Source code in typer/main.py
def add_typer(
self,
typer_instance: "Typer",
*,
name: Annotated[
str | None,
Doc(
"""
The name of this subcommand.
See [the tutorial about name and help](https://typer.tiangolo.com/tutorial/subcommands/name-and-help) for different ways of setting a command's name,
and which one takes priority.
"""
),
] = Default(None),
cls: Annotated[
type[TyperGroup] | None,
Doc(
"""
The class of this subcommand. Mainly used when [using the Click library underneath](https://typer.tiangolo.com/tutorial/using-click/). Can usually be left at the default value `None`.
Otherwise, should be a subtype of `TyperGroup`.
"""
),
] = Default(None),
invoke_without_command: Annotated[
bool,
Doc(
"""
By setting this to `True`, you can make sure a callback is executed even when no subcommand is provided.
"""
),
] = Default(False),
no_args_is_help: Annotated[
bool,
Doc(
"""
If this is set to `True`, running a command without any arguments will automatically show the help page.
"""
),
] = Default(False),
subcommand_metavar: Annotated[
str | None,
Doc(
"""
**Note**: you probably shouldn't use this parameter, it is inherited
from Click and supported for compatibility.
---
How to represent the subcommand argument in help.
"""
),
] = Default(None),
chain: Annotated[
bool,
Doc(
"""
**Note**: you probably shouldn't use this parameter, it is inherited
from Click and supported for compatibility.
---
Allow passing more than one subcommand argument.
"""
),
] = Default(False),
result_callback: Annotated[
Callable[..., Any] | None,
Doc(
"""
**Note**: you probably shouldn't use this parameter, it is inherited
from Click and supported for compatibility.
---
A function to call after the group's and subcommand's callbacks.
"""
),
] = Default(None),
# Command
context_settings: Annotated[
dict[Any, Any] | None,
Doc(
"""
Pass configurations for the [context](https://typer.tiangolo.com/tutorial/commands/context/).
Available configurations can be found in the docs for Click's `Context` [here](https://click.palletsprojects.com/en/stable/api/#context).
"""
),
] = Default(None),
callback: Annotated[
Callable[..., Any] | None,
Doc(
"""
Add a callback to this app.
See [the tutorial about callbacks](https://typer.tiangolo.com/tutorial/commands/callback/) for more details.
"""
),
] = Default(None),
help: Annotated[
str | None,
Doc(
"""
Help text for the subcommand.
See [the tutorial about name and help](https://typer.tiangolo.com/tutorial/subcommands/name-and-help) for different ways of setting a command's help,
and which one takes priority.
"""
),
] = Default(None),
epilog: Annotated[
str | None,
Doc(
"""
Text that will be printed right after the help text.
"""
),
] = Default(None),
short_help: Annotated[
str | None,
Doc(
"""
A shortened version of the help text that can be used e.g. in the help table listing subcommands.
When not defined, the normal `help` text will be used instead.
"""
),
] = Default(None),
options_metavar: Annotated[
str | None,
Doc(
"""
In the example usage string of the help text for a command, the default placeholder for various arguments is `[OPTIONS]`.
Set `options_metavar` to change this into a different string. When `None`, the default value will be used.
"""
),
] = Default(None),
add_help_option: Annotated[
bool,
Doc(
"""
**Note**: you probably shouldn't use this parameter, it is inherited
from Click and supported for compatibility.
---
By default each command registers a `--help` option. This can be disabled by this parameter.
"""
),
] = Default(True),
hidden: Annotated[
bool,
Doc(
"""
Hide this command from help outputs. `False` by default.
"""
),
] = Default(False),
deprecated: Annotated[
bool,
Doc(
"""
Mark this command as deprecated in the help outputs. `False` by default.
"""
),
] = False,
# Rich settings
rich_help_panel: Annotated[
str | None,
Doc(
"""
Set the panel name of the command when the help is printed with Rich.
"""
),
] = Default(None),
) -> None:
"""
Add subcommands to the main app using `app.add_typer()`.
Subcommands may be defined in separate modules, ensuring clean separation of code by functionality.
Read more in the
[Typer docs for SubCommands](https://typer.tiangolo.com/tutorial/subcommands/add-typer/).
## Example
```python
import typer
from .add import app as add_app
from .delete import app as delete_app
app = typer.Typer()
app.add_typer(add_app)
app.add_typer(delete_app)
```
"""
self.registered_groups.append(
TyperInfo(
typer_instance,
name=name,
cls=cls,
invoke_without_command=invoke_without_command,
no_args_is_help=no_args_is_help,
subcommand_metavar=subcommand_metavar,
chain=chain,
result_callback=result_callback,
context_settings=context_settings,
callback=callback,
help=help,
epilog=epilog,
short_help=short_help,
options_metavar=(
options_metavar or self._info_val_str("options_metavar")
),
add_help_option=add_help_option,
hidden=hidden,
deprecated=deprecated,
rich_help_panel=rich_help_panel,
)
)