繁体   English   中英

从“typer”python || 读取多个单词为 arguments 如何使用“typer”“python”从cli读取未知数量的带有空格的字符串

[英]read multiple words as arguments from "typer" python || how to read a unknown number of strings with spaces from cli using "typer" "python"

我在我的命令行应用程序中添加了这个命令。

@app.command(name="add")
    def add(
        priority: int = typer.Argument(...),
        description: List[str] = typer.Argument(...),
        ) -> None:
        print(description)
    
        """Add a new task with a DESCRIPTION."""
        todoer = get_todoer()
        todo, error = todoer.add(description, priority)
        if error:
            typer.secho(
                f'Adding task failed with "{ERRORS[error]}"', fg=typer.colors.RED
            )
            raise typer.Exit(1)
        else:
            typer.secho(
                f"""Added task: "{todo['Description']}" """
                f"""with priority {priority}""",
                fg=typer.colors.GREEN,
            )

它应该阅读任何长度的描述。 但问题是它在第一个空格之后停止读取。

(venv) C:\Users\Asus\Desktop\fellowship-python\python>.\task add 10 Buy Milk
('Buy',)
Added task: "Buy" with priority 10
(venv) C:\Users\Asus\Desktop\fellowship-python\python>

如何在添加命令中读取多个未知数量的 arguments 作为 arguments。

只需要在描述(这里是“Clean House”)周围使用引号来考虑空格。 现在觉得好傻。

C:\Users\Asus\Desktop\fellowship-python\python>.\task add 10 "Clean House"
('Clean House',)
Added task: "Clean House" with priority 10

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM