Skip to content

Instantly share code, notes, and snippets.

@lucascoelhof
Last active January 2, 2018 12:10
Show Gist options
  • Save lucascoelhof/1200c363cc920212104f32532467e62b to your computer and use it in GitHub Desktop.
Save lucascoelhof/1200c363cc920212104f32532467e62b to your computer and use it in GitHub Desktop.
If receiving a dictionary from docopt with -- and <> annoys you, this function might help
import re
def clean_arguments(args):
"""Cleans docopt arguments, removing --, - and < > from arguments
Parameters
----------
args : dict
Dictionary with arguments form docopt
Returns
-------
args : dict
Dictionary with the clean arguments
"""
re_doctopt = r'^--|^<|^-|>$'
matched_keys = {key for key, value in args.iteritems() if re.sub(re_doctopt, '', key) != key}
for key in matched_keys:
new_key = re.sub(re_doctopt, '', key)
args[new_key] = args[key]
args.pop(key, None)
return args
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment