Skip to content

Instantly share code, notes, and snippets.

@marcinwyszynski
Last active August 29, 2015 14:20
Show Gist options
  • Save marcinwyszynski/6726c3a46a69071a0292 to your computer and use it in GitHub Desktop.
Save marcinwyszynski/6726c3a46a69071a0292 to your computer and use it in GitHub Desktop.
#!/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