簡體   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