Last active
January 2, 2018 12:10
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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