Mercurial > hg > pyloader
changeset 31:8ffddc58dc81
make the arg parser work
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Tue, 07 Jun 2011 07:31:05 -0700 |
parents | 66382932514e |
children | b98cc94ffcfa |
files | pyloader/parser.py |
diffstat | 1 files changed, 43 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- a/pyloader/parser.py Mon Jun 06 11:43:20 2011 -0700 +++ b/pyloader/parser.py Tue Jun 07 07:31:05 2011 -0700 @@ -1,9 +1,48 @@ #!/usr/bin/env python """ -free-form command-line parser +free-form arg parser + +python parser.py foo -bar --fleem blah -gaz hi --flarg=play +['foo', 'hi'] +['bar', 'gaz'] +{'fleem': 'blah', 'flarg': 'play'} """ -def parse(args, **kwargs): - parser = OptionParser(**kwargs) - options, args = parser.parse_args() +def parse(args): + _args1 = [] + _args2 = [] + kw = {} + key = None + for arg in args: + if arg.startswith('-'): + if key: + raise Exception + if arg.startswith('--'): + if arg.startswith('---'): + raise Exception + arg = arg.lstrip('-') + if '=' in arg: + key, value = arg.split('=', 1) + kw[key] = value + key = None + else: + key = arg + else: + _args2.append(arg[1:]) + else: + if key: + kw[key] = arg + key = None + else: + _args1.append(arg) + if key: + raise Exception + return _args1, _args2, kw + +if __name__ == '__main__': + import sys + args, positional_args, kwargs = parse(sys.argv[1:]) + print args + print positional_args + print kwargs