Last active
August 29, 2015 14:20
-
-
Save marcinwyszynski/6726c3a46a69071a0292 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env python | |
import sys | |
def parse_arglist(args): | |
arglist = [] | |
for arg in args: | |
if not arg.startswith('-'): | |
arglist.append(arg) | |
continue | |
arglist.extend(arg.split('=', 1)) | |
return _arglist_as_dict(arglist) | |
def _arglist_as_dict(args): | |
ret, expecting_value, last_key = {}, False, None | |
for arg in args: | |
if arg.startswith('-'): # it's a flag! | |
last_key = arg.lstrip('-') | |
ret[last_key] = expecting_value = True # flag presence is data | |
continue | |
if not expecting_value: | |
continue | |
ret[last_key], expecting_value = arg, False | |
# otherwise it's a standalone value and we don't know what to do with it | |
return ret | |
def main(): | |
print(parse_arglist(sys.argv[1:])) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment