Replies: 2 comments
-
| 
         First thing to say is that I was not in the right page of the docs. The full docs are here. So, after reading them I have achieved the optional argument for the  The relevant part of the code that has changed is this: @app.command("build")
def build_command(
        ctx: typer.Context,
        directory: Annotated[Path, typer.Argument(
                                                 exists=True,
                                                 file_okay=False,
                                                 dir_okay=True,
                                                 readable=True,
                                                 resolve_path=True,
                                                 help="Path to the project directory. If omitted, the current directory is used.")] = os.getcwd(),Now I can provide or not the  The rest of option arguments (now shown in the code) still work as expected (this is a normal use case, the other ones are not). The problem now is that for achieving the argument passthrough I need an optional argument that accepts multiple values with undefined number of values. It can be only at the end of command and only once in a command to avoid complications. I can't use argument with multiple values because typer confuses it with the value for the  So, I think that this means that you can't do this with typer currently, because as the documentation says 
 I think that we could implement this, I have seen argument passthrough in many commands such as  or  Although it is true that is the previous command  As a workaround you can relax the spec for  You can also say that passthrough only accepts one value, and this value is the string containing all args, so it needs to be split, but in this case you will not handle correctly spaces for the shell. I can raise a PR but since it seems a non-trivial feature that can clash with other features I am requesting some help / guidelines for implementing it. Thank you.  | 
  
Beta Was this translation helpful? Give feedback.
-
| 
         Final version of the code looks like this: @app.command("build")
def build_command(
        ctx: typer.Context,
        params: Annotated[
            List[str], typer.Argument(
                                      help="Arguments are passed directly to the backend entrypoint.")] = (),
        directory: Annotated[Path, typer.Option(os.getcwd(), "-d", "--directory",
                                                 exists=True,
                                                 file_okay=False,
                                                 dir_okay=True,
                                                 readable=True,
                                                 resolve_path=True,
                                                 help="Path to the project directory. If omitted, the current directory is used.")] = os.getcwd(),(I changed  Now I can do: ../../venv/bin/python -m tot build -d . clean # (d: ., pasthrough: clean, shell: false) 
../../venv/bin/python -m tot build clean # (d: cwd, pasthrough: clean, shell: false) 
../../venv/bin/python -m tot build # (d: cwd, pasthrough: , shell: false)
../../venv/bin/python -m tot build -s clean # (d: cwd, pasthrough: clean, shell: true) 
../../venv/bin/python -m tot build -s -d . clean install# (d: ., pasthrough: clean install, shell: true)  | 
  
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
Description
Create a typer script so that there is an optional positional argument in combination with other named arguments and an argument passwthough, so that the last thing in the CLI command will sometimes be -- arg1 arg2 arg3 ...
The command usage and its expected output in all different scenarios is the following:
I would like to know how to do this with Typer or at least understand how can I do this without it but still using it to manage the rest of my argument. I could not find anything related to this in the documentation.
Operating System
Linux
Operating System Details
Ubuntu 20.04
Typer Version
0.20.0
Python Version
3.11.9
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions