Skip to content

Instantly share code, notes, and snippets.

@e2thenegpii
Last active December 13, 2018 13:41
Show Gist options
  • Save e2thenegpii/5ff0d3701be09df214e5be18f84e2e21 to your computer and use it in GitHub Desktop.
Save e2thenegpii/5ff0d3701be09df214e5be18f84e2e21 to your computer and use it in GitHub Desktop.
password proxy
import argparse
import getpass
def password_proxy(value):
password_proxy.prompt = 'Default password prompt:'
class _password_proxy(str):
def __new__(cls, value):
obj = str.__new__(cls, value)
obj._context = password_proxy
return obj
def __str__(self):
if self.isempty():
if hasattr(self._context, 'prompt'):
self = getpass.getpass(self._context.prompt)
else:
self = getpass.getpass()
return self
def isempty(self):
return self == ''
return _password_proxy(value)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--user', default='user')
parser.add_argument('--password', type=password_proxy, defualt='')
args = parser.parse_args()
print 'look ma, no password prompt (yet)'
password_proxy.prompt = 'Hello {} please enter your password:'.format(args.user)
print 'The password is {}'.format(args.password)
if __name__ == '__main__':
main()
@e2thenegpii
Copy link
Author

Try running with
python password_prompt.py --password=abcd
and
python password_prompt.py --user=bob

The first won't prompt for a password and the second will prompt for a password after printing 'look ma, no password prompt (yet)' Also notable is the password prompt can be changed based on the argparse parsed parameters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment