CLI 选项名称
默认情况下,Typer 将根据函数参数创建一个CLI 选项名称。
因此,如果您有一个函数
def main(user_name: Optional[str] = None):
pass
或
def main(user_name: Annotated[Optional[str], typer.Option()] = None):
pass
Typer 将创建一个CLI 选项
--user-name
但您可以根据需要进行自定义。
假设函数参数名称如上所述为 user_name
,但您希望CLI 选项仅为 --name
。
您可以将您希望拥有的CLI 选项名称传递给传递给 typer.Option()
的以下位置参数
import typer
from typing_extensions import Annotated
def main(user_name: Annotated[str, typer.Option("--name")]):
print(f"Hello {user_name}")
if __name__ == "__main__":
typer.run(main)
您在此将字符串 "--name"
作为 typer.Option()
的第一个位置参数传递。
提示
如果可能,最好使用 Annotated
版本。
import typer
def main(user_name: str = typer.Option(..., "--name")):
print(f"Hello {user_name}")
if __name__ == "__main__":
typer.run(main)
在这里,你将字符串 "--name"
作为 typer.Option()
的第二个位置参数传递,因为第一个参数是 ...
,表示它是必需的。
信息
"位置" 表示它不是带有关键字名称的函数参数。
例如,show_default=True
是一个关键字参数。“show_default
” 是关键字。
但在 "--name"
中没有 option_name="--name"
或类似内容,它只是字符串值 "--name"
,用于 typer.Option()
。
这是函数中的“位置参数”。
检查它
$ python main.py --help
// Notice the --name instead of --user-name
Usage: main.py [OPTIONS]
Options:
--name TEXT [required]
--help Show this message and exit.
// Try it
$ python --name Camila
Hello Camila
CLI 选项 短名称¶
短名称是CLI 选项名称,它使用一个破折号 (-
) 而不是 2 个 (--
) 和一个字母,例如 -n
而不是 --name
。
例如,ls
程序有一个名为 --size
的CLI 选项,并且相同的CLI 选项也有一个短名称 -s
// With the long name --size
$ ls ./myproject --size
12 first-steps.md 4 intro.md
// With the short name -s
$ ls ./myproject -s
12 first-steps.md 4 intro.md
// Both CLI option names do the same
CLI 选项 短名称一起¶
短名称有另一个特性,当它们只有一个字母时,如 -s
,你可以将几个CLI 选项放在一起,用一个破折号。
例如,ls
程序有以下 2 个CLI 选项(以及其他选项)
--size
:显示所列文件的尺寸。--human
:显示人类可读的格式,例如1MB
而不是1024
。
并且这 2 个CLI 选项也有短版本
--size
:短版本-s
。--human
:短版本-h
。
因此,你可以将它们与 -sh
或 -hs
放在一起
// Call ls with long CLI options
$ ls --size --human
12K first-steps.md 4.0K intro.md
// Now with short versions
$ ls -s -h
12K first-steps.md 4.0K intro.md
// And with short versions together
$ ls -sh
12K first-steps.md 4.0K intro.md
// Order in short versions doesn't matter
$ ls -hs
12K first-steps.md 4.0K intro.md
// They all work the same 🎉
带值的CLI 选项 短名称¶
当你使用带有短名称的CLI 选项时,如果它们只是布尔标志,如 --size
或 --human
,你可以将它们放在一起。
但是,如果你有一个带有短名称 -f
的CLI 选项 --file
,它需要一个值,如果你将它与CLI 选项的其他短名称放在一起,你必须将其放在最后一个字母,以便它可以接收紧随其后的值。
例如,假设你正在使用程序 tar
解压缩/提取文件 myproject.tar.gz
。
你可以将这些CLI 选项 短名称传递给 tar
-x
:表示“eX
tract”,用于解压缩和提取内容。-v
:表示“V
erbose”,用于在屏幕上打印它正在做什么,这样你就可以知道它正在解压缩每个文件,并且可以在等待时自娱自乐。-f
:表示“F
ile”,此选项需要一个值,即要提取的压缩文件(在本例中,这是myproject.tar.gz
)。- 因此,如果你将所有短名称放在一起,这个
-f
必须放在最后,以接收紧随其后的值。
- 因此,如果你将所有短名称放在一起,这个
例如
$ tar -xvf myproject.tar.gz
myproject/
myproject/first-steps.md
myproject/intro.md
// But if you put the -f before
$ tar -fxv myproject.tar.gz
// You get an ugly error
tar: You must specify one of the blah, blah, error, error
定义CLI 选项短名称¶
在Typer中,您还可以定义CLI 选项短名称,其方式与自定义长名称相同。
您可以将位置参数传递给typer.Option()
以定义CLI 选项名称。
提示
请记住,位置函数参数是没有关键字的参数。
您传递给typer.Option()
的所有其他函数参数/参数,如prompt=True
和help="This option blah, blah"
,都需要关键字。
您可以覆盖CLI 选项名称以在前面的示例中使用,但您还可以声明其他备用名称,包括短名称。
例如,扩展前面的示例,我们添加一个CLI 选项短名称-n
import typer
from typing_extensions import Annotated
def main(user_name: Annotated[str, typer.Option("--name", "-n")]):
print(f"Hello {user_name}")
if __name__ == "__main__":
typer.run(main)
提示
如果可能,最好使用 Annotated
版本。
import typer
def main(user_name: str = typer.Option(..., "--name", "-n")):
print(f"Hello {user_name}")
if __name__ == "__main__":
typer.run(main)
在这里,我们覆盖了CLI 选项名称,默认情况下该名称为--user-name
,并且我们定义它为--name
。我们还声明了一个CLI 选项短名称-n
。
检查它
// Check the help
$ python main.py --help
// Notice the two CLI option names -n and --name
Usage: main.py [OPTIONS]
Options:
-n, --name TEXT [required]
--help Show this message and exit.
// Try the short version
$ python main.py -n Camila
Hello Camila
CLI 选项仅短名称¶
如果您只声明一个短名称,如-n
,那么这将是唯一的CLI 选项名称。--name
和--user-name
都不可用。
import typer
from typing_extensions import Annotated
def main(user_name: Annotated[str, typer.Option("-n")]):
print(f"Hello {user_name}")
if __name__ == "__main__":
typer.run(main)
提示
如果可能,最好使用 Annotated
版本。
import typer
def main(user_name: str = typer.Option(..., "-n")):
print(f"Hello {user_name}")
if __name__ == "__main__":
typer.run(main)
检查它
$ python main.py --help
// Notice there's no --name nor --user-name, only -n
Usage: main.py [OPTIONS]
Options:
-n TEXT [required]
--help Show this message and exit.
// Try it
$ python main.py -n Camila
Hello Camila
CLI 选项短名称和默认值¶
继续上面的示例,由于Typer允许您声明一个CLI 选项仅具有短名称,因此,如果您希望具有默认长名称和短名称,则必须明确声明这两个名称
import typer
from typing_extensions import Annotated
def main(user_name: Annotated[str, typer.Option("--user-name", "-n")]):
print(f"Hello {user_name}")
if __name__ == "__main__":
typer.run(main)
提示
如果可能,最好使用 Annotated
版本。
import typer
def main(user_name: str = typer.Option(..., "--user-name", "-n")):
print(f"Hello {user_name}")
if __name__ == "__main__":
typer.run(main)
检查它
$ python main.py --help
// Notice that we have the long version --user-name back
// and we also have the short version -n
Usage: main.py [OPTIONS]
Options:
-n, --user-name TEXT [required]
--help Show this message and exit.
// Try it
$ python main.py --user-name Camila
Hello Camila
// And try the short version
$ python main.py -n Camila
CLI 选项短名称组合¶
您可以创建多个短名称并一起使用它们。
您不必做任何特殊的事情来使其工作(除了声明这些短版本之外)
import typer
from typing_extensions import Annotated
def main(
name: Annotated[str, typer.Option("--name", "-n")],
formal: Annotated[bool, typer.Option("--formal", "-f")] = False,
):
if formal:
print(f"Good day Ms. {name}.")
else:
print(f"Hello {name}")
if __name__ == "__main__":
typer.run(main)
提示
如果可能,最好使用 Annotated
版本。
import typer
def main(
name: str = typer.Option(..., "--name", "-n"),
formal: bool = typer.Option(False, "--formal", "-f"),
):
if formal:
print(f"Good day Ms. {name}.")
else:
print(f"Hello {name}")
if __name__ == "__main__":
typer.run(main)
提示
请注意,我们再次声明了CLI 选项名称的长版本和短版本。
检查它
$ python main.py --help
// We now have short versions -n and -f
// And also long versions --name and --formal
Usage: main.py [OPTIONS]
Options:
-n, --name TEXT [required]
-f, --formal
--help Show this message and exit.
// Try the short versions
$ python main.py -n Camila -f
Good day Ms. Camila.
// And try the 2 short versions together
// See how -n has to go last, to be able to get the value
$ python main.py -fn Camila
Good day Ms. Camila.