annotate python/cli.py @ 309:6095d439d1f2

new stuff! realwhich
author Jeff Hammel <jhammel@mozilla.com>
date Thu, 16 May 2013 23:27:18 -0700
parents e7948549afa1
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
107
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 #!/usr/bin/env python
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 """
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 program illustrating command line in the form of
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5 ``--option foo`` or ``--option=foo`` goes to a (key,value) paid
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6 and ``-tag`` gets appended to a list. Further options go to a further list::
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
7
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
8 >>> main(['-foo', '--bar=fleem', 'baz'])
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
9 (['foo'], {'bar': 'fleem'}, ['baz'])
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
10 """
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12 import sys
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14 class ParserError(Exception):
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15 """error for exceptions while parsing the command line"""
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17 def main(_args=sys.argv[1:]):
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19 # return values
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20 _dict = {}
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21 tags = []
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22 args = []
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24 # parse the arguments
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25 key = None
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26 for arg in _args:
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27 if arg.startswith('---'):
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
28 raise ParserError("arguments should start with '-' or '--' only")
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29 elif arg.startswith('--'):
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30 if key:
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
31 raise ParserError("Key %s still open" % key)
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
32 key = arg[2:]
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
33 if '=' in key:
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
34 key, value = key.split('=', 1)
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
35 _dict[key] = value
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
36 key = None
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
37 continue
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
38 elif arg.startswith('-'):
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
39 if key:
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
40 raise ParserError("Key %s still open" % key)
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
41 tags.append(arg[1:])
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
42 continue
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
43 else:
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
44 if key:
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
45 _dict[key] = arg
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
46 continue
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
47 args.append(arg)
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
48
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
49 # return values
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
50 return (_dict, tags, args)
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
51
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
52 if __name__ == '__main__':
108
e7948549afa1 do some stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 107
diff changeset
53 try:
e7948549afa1 do some stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 107
diff changeset
54 _dict, tags, args = main()
e7948549afa1 do some stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 107
diff changeset
55 except ParserError, e:
e7948549afa1 do some stuff
Jeff Hammel <jhammel@mozilla.com>
parents: 107
diff changeset
56 import pdb; pdb.set_trace() # for debugging
107
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
57 print _dict
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
58 print tags
1da1baa03ac1 add a cli example program
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
59 print args