diff --git a/httpie/cli.py b/httpie/cli.py index 13f19b62..6f4358dc 100644 --- a/httpie/cli.py +++ b/httpie/cli.py @@ -123,8 +123,11 @@ parser.add_argument( # ``requests.request`` keyword arguments. parser.add_argument( - '--auth', '-a', help='username:password', - type=cliparse.KeyValueType(cliparse.SEP_COMMON) + '--auth', '-a', type=cliparse.AuthCredentials(cliparse.SEP_COMMON), + help=_(''' + username:password. + If the password is omitted, HTTPie will prompt for it. + '''), ) parser.add_argument( diff --git a/httpie/cliparse.py b/httpie/cliparse.py index b183dba5..e2635936 100644 --- a/httpie/cliparse.py +++ b/httpie/cliparse.py @@ -10,6 +10,7 @@ import argparse import mimetypes from collections import namedtuple +from getpass import getpass try: from collections import OrderedDict @@ -204,6 +205,20 @@ class KeyValueType(object): return KeyValue(key=key, value=value, sep=sep, orig=string) +class AuthCredentials(KeyValueType): + def __call__(self, string): + try: + return super(AuthCredentials, self).__call__(string) + except argparse.ArgumentTypeError: + try: + password = getpass("Password for user '%s': " % string) + except (EOFError, KeyboardInterrupt): + sys.stderr.write('\n') + sys.exit(0) + return KeyValue(key=string, value=password, sep=SEP_COMMON, + orig=string) + + def parse_items(items, data=None, headers=None, files=None): """Parse `KeyValueType` `items` into `data`, `headers` and `files`.""" if headers is None: