pythonpython-3.xauthenticationpython-click

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

Solution

  • 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 foo@bar.com
    $ Password:
    $ Repeat for confirmation:
    foo@bar.com test