Chris
Chris

Reputation: 387

Python Click module, how to take in a username and password as arguments

I've created a very simple CLI application that takes in a username and password as arguments using Click. Using @click.password_option() I'm able to mask the password and have the user confirm their password without any code from my end, which is great.

Though, I noticed that the email field is being slurped in as None. I believe this is because I am using password.option()

Does anyone have a workaround for this? I've tried using Click's Context.invoke() and Context.forward() to pass in the email field but that was no help.

@cli.command()
@click.argument("email", type=str, required=True)
@click.password_option()
def register(email, password):
        click.echo(f"{email} {password}")
Output:
> None test

Upvotes: 1

Views: 2298

Answers (1)

chuckles
chuckles

Reputation: 761

The following code works as expected

import click

@click.command()
@click.argument("email", type=str, required=True)
@click.password_option()
def register(email, password):
        click.echo(f"{email} {password}")

if __name__ == '__main__':
    register()
$ python3 example.py [email protected]
$ Password:
$ Repeat for confirmation:
[email protected] test

Upvotes: 1

Related Questions