typer icon indicating copy to clipboard operation
typer copied to clipboard

Dynamically enabling required arguments for a command based on the user's input from first argument

Open yashika51 opened this issue 2 years ago • 0 comments

First Check

  • [X] I added a very descriptive title to this issue.
  • [X] I used the GitHub search to find a similar issue and didn't find it.
  • [X] I searched the Typer documentation, with the integrated search.
  • [X] I already searched in Google "How to X in Typer" and didn't find any information.
  • [X] I already read and followed all the tutorial in the docs and didn't find an answer.
  • [X] I already checked if it is not related to Typer but to Click.

Commit to Help

  • [X] I commit to help with one of those options 👆

Example Code

import typer

app = typer.Typer()


@app.command()
def connection(
    name: str = typer.Option(
        ..., help=typer.style("Name", fg=typer.colors.BRIGHT_GREEN, bold=True)
    ),
    display_name: str = typer.Option(
        ..., help=typer.style("Display Name", fg=typer.colors.BRIGHT_GREEN, bold=True)
    ),
    service_account_json: str = typer.Option(
        ...,
        help=typer.style(
            "JSON enclosed in single quotes", fg=typer.colors.BRIGHT_GREEN, bold=True
        ),
    ),
    ignored_users: str = typer.Option(
        "[]",
        help=typer.style(
            "Ignored users as single quotes enclosed list of strings",
            fg=typer.colors.YELLOW,
        ),
    )
) -> None:
    """Connect the platform"""

    pass

Description

Hello, I am very new to Typer and have a question regarding the possibility of dynamic arguments or any suggestions :)

In the above code sample, I am trying to connect a platform with the CLI. The platform in example has to have name, display name, service account json and some other optional fields. However, if the command has too many arguments it looks messy and is a lot of work for user in case of positional arguments.

What I want is to give user options. If a user has a json or yaml file with config details I want to just read the values from there and disable input for required arguments, otherwise if the user doesn't have the file, they should pass all the arguments using the right flags(-- name xyz for example).

This can depend on the first argument lets say
is_config_file_available: bool = typer.Option(False, help = "Do you want to load credentials from the config file?"),

If the user input here is True then they can pass the file path along and doesn't have to pass any other argument.

Is this possible to do with Typer?

Operating System

macOS

Operating System Details

No response

Typer Version

0.4.0

Python Version

Python 3.8.10

Additional Context

No response

yashika51 avatar Mar 14 '22 16:03 yashika51