Have --auth prompt for password if omitted.

This commit is contained in:
Ismail Badawi 2012-07-16 04:40:36 -04:00
parent 7bc2de2f9d
commit 929ead437a
2 changed files with 20 additions and 2 deletions

View File

@ -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(

View File

@ -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: