Replies: 1 comment
-
I am unsure if this is the best way of doing it, but I would like to rewrite your code as follows: import typer
# Create CLI app
app = typer.Typer(
name="simple",
add_completion=False,
)
username = typer.Option(
"", # or None
"-u",
"--username",
help="Username for authentication",
)
password = typer.Option(
"", # or None
"-p",
"--password",
help="Password for authentication",
)
token = typer.Option(
"", # or None
"-t",
"--token",
help="Token for authentication",
)
@app.command()
def main(
username=username,
password=password,
token=token,
):
if username and password and token:
typer.echo(
"Using username, password, and token in authentication is not allowed. Please try again.")
raise typer.Exit(code=1)
if username and password:
typer.echo("Using username and password authentication")
elif token:
typer.echo("Using token authentication")
else:
typer.echo(
"Please provide either a username and password OR a token, but not both.")
raise typer.Exit(code=1)
if __name__ == "__main__":
app() the output should be like this: python main.py - t api-token
Using token authentication with username and password: python main.py - u myUserName - p passw0rd
Using username and password authentication and it will check for validation: python main.py - t ""
Please provide either a username and password OR a token, but not both.
python main.py - u myUserName
Please provide either a username and password OR a token, but not both.
python main.py - p passw0rd
Please provide either a username and password OR a token, but not both. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
First Check
Commit to Help
Example Code
Description
I'd like to define a CLI command that supports two sorts of options - either
username
andpassword
, ortoken
. I've attached a self-contained example that implements this, but I'm interested to know if there is a better way to accomplish this with Typer.Passing
--username
and--password
(and not passing--token
) is valid:Passing
--token
(and not passing--username
or--password
) is also valid:Passing other combinations of these options is not valid:
Operating System
macOS
Operating System Details
No response
Typer Version
0.12.3
Python Version
3.12.2
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions