pythonpython-3.xcommand-line-interfacecommand-line-argumentstyper

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


I have this command add in my command line app.

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

It should read description of any length. But the problem is it stops reading after the first whitespace.

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

How to read multiple unknown number of arguments in to the add command as arguments.


Solution

  • Just had to use inverted commas around description (here "Clean House") for it to consider spaces. feels so silly now.

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