Fix bug in parsing --add-header

Closes #1614
This commit is contained in:
pukkandan 2021-11-10 18:33:37 +05:30
parent 013ae2e503
commit 44bcb8d122
No known key found for this signature in database
GPG Key ID: 0F00D95A001F4698

View File

@ -151,25 +151,25 @@ def _set_from_options_callback(
def _dict_from_options_callback( def _dict_from_options_callback(
option, opt_str, value, parser, option, opt_str, value, parser,
allowed_keys=r'[\w-]+', delimiter=':', default_key=None, process=None, multiple_keys=True): allowed_keys=r'[\w-]+', delimiter=':', default_key=None, process=None, multiple_keys=True,
process_key=str.lower):
out_dict = getattr(parser.values, option.dest) out_dict = getattr(parser.values, option.dest)
if multiple_keys: if multiple_keys:
allowed_keys = r'(%s)(,(%s))*' % (allowed_keys, allowed_keys) allowed_keys = r'(%s)(,(%s))*' % (allowed_keys, allowed_keys)
mobj = re.match(r'(?i)(?P<keys>%s)%s(?P<val>.*)$' % (allowed_keys, delimiter), value) mobj = re.match(r'(?i)(?P<keys>%s)%s(?P<val>.*)$' % (allowed_keys, delimiter), value)
if mobj is not None: if mobj is not None:
keys = [k.strip() for k in mobj.group('keys').lower().split(',')] keys, val = mobj.group('keys').split(','), mobj.group('val')
val = mobj.group('val')
elif default_key is not None: elif default_key is not None:
keys, val = [default_key], value keys, val = [default_key], value
else: else:
raise optparse.OptionValueError( raise optparse.OptionValueError(
'wrong %s formatting; it should be %s, not "%s"' % (opt_str, option.metavar, value)) 'wrong %s formatting; it should be %s, not "%s"' % (opt_str, option.metavar, value))
try: try:
keys = map(process_key, keys) if process_key else keys
val = process(val) if process else val val = process(val) if process else val
except Exception as err: except Exception as err:
raise optparse.OptionValueError( raise optparse.OptionValueError(f'wrong {opt_str} formatting; {err}')
'wrong %s formatting; %s' % (opt_str, err))
for key in keys: for key in keys:
out_dict[key] = val out_dict[key] = val
@ -792,7 +792,7 @@ def _dict_from_options_callback(
'--add-header', '--add-header',
metavar='FIELD:VALUE', dest='headers', default={}, type='str', metavar='FIELD:VALUE', dest='headers', default={}, type='str',
action='callback', callback=_dict_from_options_callback, action='callback', callback=_dict_from_options_callback,
callback_kwargs={'multiple_keys': False}, callback_kwargs={'multiple_keys': False, 'process_key': None},
help='Specify a custom HTTP header and its value, separated by a colon ":". You can use this option multiple times', help='Specify a custom HTTP header and its value, separated by a colon ":". You can use this option multiple times',
) )
workarounds.add_option( workarounds.add_option(