【问题标题】:read multiple words as arguments from "typer" python || how to read a unknown number of strings with spaces from cli using "typer" "python"从 "typer" python || 读取多个单词作为参数如何使用“typer”“python”从cli读取未知数量的带有空格的字符串
【发布时间】:2022-01-26 01:07:21
【问题描述】:

我的命令行应用中有这个命令add

@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>

如何在 add 命令中读取多个未知数量的参数作为参数。

【问题讨论】:

    标签: python python-3.x command-line-interface command-line-arguments typer


    【解决方案1】:

    只需要在描述(此处为“Clean House”)周围使用引号即可考虑空格。现在感觉好傻。

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

    【讨论】:

      猜你喜欢
      • 2021-02-21
      • 2021-10-15
      • 2020-03-23
      • 1970-01-01
      • 2020-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多