annotate example/dictarg.py @ 138:cae2a1e1b9da

from config
author Jeff Hammel <k0scist@gmail.com>
date Tue, 04 Nov 2014 15:15:25 -0800
parents bbe1d192a61c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
137
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 #!/usr/bin/python
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3 import sys
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 def dictarg(adict, argv=sys.argv[1:]):
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7 shopts = {}
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9 # build list of keys
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10 for i in adict.keys():
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11 for j in i:
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12 s = str(j)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13 if len(s) == 1:
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14 if shopts.has_key(s):
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15 continue
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16 shopts[s] = i
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17 break
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18 else:
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19 print >> sys.stderr, "dictarg: couldn't generate key for '%s'" % i
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20 sys.exit(1)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22 optstring = "?"
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23 for i in shopts.keys():
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24 optstring += i + ':' # all these options should have arguments
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26 # look for command line args
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27 import getopt
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29 opts, args = getopt.getopt(argv, optstring)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
31 if ('-?', '') in opts:
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32 print 'Options:'
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
33 for i in shopts:
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
34 print '-%s %s [%s]' % (i, shopts[i], adict[shopts[i]])
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
35 sys.exit(0)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
36
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
37 for o, v in opts:
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
38 o = o[1:] # cut off the dash
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
39 adict[shopts[o]] = v
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
40
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
41 # test if invoked from command line
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
42 if __name__ == '__main__':
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
43 adict = {}
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
44 for i in sys.argv:
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
45 adict[i] = len(i)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
46
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
47 # print the help
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
48 dictarg(adict, ['-?'])
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
49
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
50 # test functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
51 print 'Enter test arguments: ',
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
52 line = sys.stdin.readline()[:-1].split(' ')
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
53 dictarg(adict, line)
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
54 print adict