Skip to content

Commit 79730fc

Browse files
committed
content: command name
Add pages: * command custom name * command alias
1 parent 02acedf commit 79730fc

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed

docs/command-alias.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
tags:
3+
- command
4+
- naming
5+
---
6+
7+
## Overview
8+
9+
Typer doesn't support multiple aliases for the same command.
10+
11+
You can define an alias in Python by:
12+
13+
* Calling one command function from the other.
14+
* Calling the same private function from both commands.
15+
16+
## Two Commands Calling the Same Function
17+
18+
Create 2 commands `pycon` and `display-pycon` executing the same function:
19+
20+
```python
21+
@app.command()
22+
def display_pycon():
23+
_print_pycon()
24+
25+
26+
@app.command()
27+
def pycon():
28+
_print_pycon()
29+
30+
31+
def _print_pycon():
32+
Console().print("PyCon!")
33+
```
34+
35+
If the aliased command also has options, consider using the [re-use options](re-using-options.md) pattern.
36+
37+
## CLIG Recommendation on Command Aliases
38+
39+
> Don’t allow arbitrary abbreviations of subcommands. For example, say your command has an install subcommand. When you added it, you wanted to save users some typing, so you allowed them to type any non-ambiguous prefix, like mycmd ins, or even just mycmd i, and have it be an alias for mycmd install. Now you’re stuck: you can’t add any more commands beginning with i, because there are scripts out there that assume i means install.
40+
41+
> There’s nothing wrong with aliases—saving on typing is good—but they should be explicit and remain stable.
42+
43+
[CLIG Guidelines / Future-proofing](https://clig.dev/#future-proofing)
44+
45+
## More Info
46+
47+
* [CLIG Guidelines / Naming](https://clig.dev/#naming)
48+
* [CLIG Guidelines / Subcommands](https://clig.dev/#subcommands)
49+
* [CLIG Guidelines / Future-proofing](https://clig.dev/#future-proofing)

docs/command-custom-name.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
tags:
3+
- command
4+
- naming
5+
---
6+
7+
## Overview
8+
9+
By default, Typer creates a command with the same name as the function.
10+
11+
* If the function name contains an `_` => It wil be replaced with `-`.
12+
* You can define a custom command name in the 1st argument of the `@app.command()` decorator.
13+
14+
## Custom Command Name
15+
16+
Define a command named `list` using the function `list_items`.
17+
18+
```python
19+
@app.command("list")
20+
def list_items():
21+
```
22+
23+
This pattern is useful if your command name is the name of a Python built-in function (like `list`) or a Python standard library module (like `os`).
24+
25+
## More Info
26+
27+
* [Typer / Custom Command Name](https://typer.tiangolo.com/tutorial/commands/name/)
28+
* [CLIG Guidelines / Naming](https://clig.dev/#naming)

docs/option-with-aliases.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
tags:
33
- option
4+
- naming
45
---
56

67
## Overview

docs/re-using-options.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
tags:
33
- consistency
44
- option
5+
- naming
56
title: Re-using Typer Options
67
---
78

0 commit comments

Comments
 (0)