S
S
Sergey Bard2018-03-28 00:02:50
Python
Sergey Bard, 2018-03-28 00:02:50

How to get password via argparse?

I try to get the password in this way , but even nothing comes out, I do it like this

class Password(argparse.Action):
     def __call__(self, parser, namespace, values, option_string):
        if values is None:
            values = getpass.getpass()
        setattr(namespace, self.dest, values)

parser = argparse.ArgumentParser('bla bla bla')
parser.add_argument('user', help='User name to use for authentication', type=str)
parser.add_argument('-p', action=Password, nargs='?', dest='password', helс p='Enter your password')
print(parser.parse_args())

but in the end I get Namespace( password=None, user='user'), I tried it as in the documentation with init but nothing works either, I checked it with a print from __call__ and realized that the script does not even go inside, please tell me what I'm doing wrong, python 2.7

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question