annotate pyloader/parser.py @ 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 36f5d31c3ed6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
28
bd50a1317ec4 stub command line parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 #!/usr/bin/env python
bd50a1317ec4 stub command line parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2
bd50a1317ec4 stub command line parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 """
31
8ffddc58dc81 make the arg parser work
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
4 free-form arg parser
8ffddc58dc81 make the arg parser work
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
5
8ffddc58dc81 make the arg parser work
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
6 python parser.py foo -bar --fleem blah -gaz hi --flarg=play
8ffddc58dc81 make the arg parser work
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
7 ['foo', 'hi']
8ffddc58dc81 make the arg parser work
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
8 ['bar', 'gaz']
8ffddc58dc81 make the arg parser work
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
9 {'fleem': 'blah', 'flarg': 'play'}
28
bd50a1317ec4 stub command line parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
10 """
30
66382932514e more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
11
31
8ffddc58dc81 make the arg parser work
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
12 def parse(args):
8ffddc58dc81 make the arg parser work
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
13 _args1 = []
8ffddc58dc81 make the arg parser work
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
14 _args2 = []
8ffddc58dc81 make the arg parser work
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
15 kw = {}
8ffddc58dc81 make the arg parser work
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
16 key = None
8ffddc58dc81 make the arg parser work
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
17 for arg in args:
8ffddc58dc81 make the arg parser work
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
18 if arg.startswith('-'):
8ffddc58dc81 make the arg parser work
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
19 if key:
8ffddc58dc81 make the arg parser work
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
20 raise Exception
8ffddc58dc81 make the arg parser work
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
21 if arg.startswith('--'):
8ffddc58dc81 make the arg parser work
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
22 if arg.startswith('---'):
8ffddc58dc81 make the arg parser work
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
23 raise Exception
8ffddc58dc81 make the arg parser work
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
24 arg = arg.lstrip('-')
8ffddc58dc81 make the arg parser work
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
25 if '=' in arg:
8ffddc58dc81 make the arg parser work
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
26 key, value = arg.split('=', 1)
8ffddc58dc81 make the arg parser work
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
27 kw[key] = value
8ffddc58dc81 make the arg parser work
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
28 key = None
8ffddc58dc81 make the arg parser work
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
29 else:
8ffddc58dc81 make the arg parser work
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
30 key = arg
8ffddc58dc81 make the arg parser work
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
31 else:
8ffddc58dc81 make the arg parser work
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
32 _args2.append(arg[1:])
8ffddc58dc81 make the arg parser work
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
33 else:
8ffddc58dc81 make the arg parser work
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
34 if key:
8ffddc58dc81 make the arg parser work
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
35 kw[key] = arg
8ffddc58dc81 make the arg parser work
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
36 key = None
8ffddc58dc81 make the arg parser work
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
37 else:
8ffddc58dc81 make the arg parser work
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
38 _args1.append(arg)
8ffddc58dc81 make the arg parser work
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
39 if key:
8ffddc58dc81 make the arg parser work
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
40 raise Exception
8ffddc58dc81 make the arg parser work
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
41 return _args1, _args2, kw
8ffddc58dc81 make the arg parser work
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
42
8ffddc58dc81 make the arg parser work
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
43 if __name__ == '__main__':
8ffddc58dc81 make the arg parser work
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
44 import sys
8ffddc58dc81 make the arg parser work
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
45 args, positional_args, kwargs = parse(sys.argv[1:])
8ffddc58dc81 make the arg parser work
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
46 print args
8ffddc58dc81 make the arg parser work
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
47 print positional_args
8ffddc58dc81 make the arg parser work
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
48 print kwargs