unlimited argument for an option with comma spliter #1341
-
First Check
Commit to Help
Example Codeimport typer
def main(command: List[str] = typer.Option(None)):
print(command)DescriptionI want to pass unlimited multiple arguments to $ python cli.py add test -C "hi I'm test", "hello this is test"I want to get a list like Operating SystemWindows Operating System DetailsWindows Version 10.0.19045.2486 Typer Version0.7.0 Python VersionPython 3.9.1 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
|
As far as i understand the intended way to use a list type parameter would be like this: $ python app.py --command arg1 --command arg2
['arg1', 'arg2']If you really want to use comma separated strings you could do the following: from typing import Annotated
import typer
def comma_list(raw: str) -> list[str]:
return raw.split(",")
def main(
command: Annotated[list, typer.Option(parser=comma_list)],
):
print(command)
if __name__ == "__main__":
typer.run(main)$ python potato/main.py --command "hello","world"
['hello', 'world']Note that using import typer
def main(commands: str = typer.Option()):
cmd_list = commands.split(",")
print(cmd_list)
if __name__ == "__main__":
typer.run(main) |
Beta Was this translation helpful? Give feedback.
-
|
I implemented something quite similar in my Essentially extending on what @heiskane is suggesting you do for from typing import Optional, List
from typing_extensions import Annotated
import typer
@app.command("ack")
async def acknowledge(
ids: Annotated[List[int], typer.Argument(help="alarm id")], #multiple ids
message: Annotated[
Optional[str],
typer.Option(
"-m",
"--message",
help="comment to add to history",
),
] = None,
):
for id in ids:
print(id)See issue on my project for more detail. |
Beta Was this translation helpful? Give feedback.
I don't think it's possible to make it accept values in quotes - shell (?) strips quotation marks and
becomes just
You can improve the solution suggested by @heiskane this way (using
callbackinstead ofparser):Now
commandargument is properly typed.Note: this implementation takes only last…