main.py:
< pre class="lang-py Prettyprint-override">
Код: Выделить всё
import typer
import mycommand
app = typer.Typer()
app.add_typer(mycommand.app, name='mycommand')
@app.command()
def othercmd():
pass
if __name__ == '__main__':
app()
Код: Выделить всё
from typing import List
import typer
app = typer.Typer()
@app.callback(invoke_without_command=True) # Not a sub command - run this by default
def mycommand(files: List[str] = typer.Argument(...), name: str = typer.Option(None)):
if name: print(f'Hello {name}')
print(files)
Однако попытка запустить с помощью python main.py mycommand myfile .txt --name Butty загрузит параметр в аргумент files.
Выполнение main.py mycommand --help показывает, почему; после опций и аргументов обратного вызова ожидается дополнительная команда и аргументы:
Код: Выделить всё
Usage: main.py mycommand [OPTIONS] FILES... COMMAND [ARGS]...
Arguments:
FILES... [required]
Options:
--name TEXT
--help Show this message and exit.
Подробнее здесь: https://stackoverflow.com/questions/731 ... b-commands
Мобильная версия