I need to hide password when user run script in console (like this: mysql -p
).
For input parameters I use argparse
, how I can add getpass
to password parameter?
parser = argparse.ArgumentParser()
parser.add_argument('-p', action='store', dest='password', type=getpass.getpass())
When I run my script: python script.py -u User -p
I get separate line for enter password (Password:
), but after entering Exception: ValueError: 'my_password' is not callable
is raised.
This guy should solve your problem: getpass
Here is an example with a custom action
class PwdAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
mypass = getpass.getpass()
setattr(namespace, self.dest, mypass)
parser = argparse.ArgumentParser()
parser.add_argument('-f', action=PwdAction, nargs=0)