6.20. Module getoptions

Provides generic command line processing. Input: a list of words, each of which is an option or filename. Output: a list of pairs, each of which is a list of option value pairs, and a filename. The format for options is --longoption, --longoption=value, -abcd, -abcdef=value.
Start python section to interscript/getoptions.py[1 /1 ]
     1: #line 10 "options.ipk"
     2: import re
     3: 
     4: longopts=re.compile('^--([A-Za-z][-A-Za-z_0-9]*)(?:=(.*))?$')
     5: shortopts=re.compile('^-([A-Za-z]+)(?:=(.*))?$')
     6: 
     7: 
     8: def getopt(args):
     9:   wordno = 0
    10:   result = []
    11:   opts=[]
    12:   while wordno<len(args):
    13:     filename = ''
    14:     word = args[wordno]
    15:     #print 'word',word,
    16:     match = longopts.match(word)
    17:     if match:
    18:       opts.append((match.group(1),match.group(2)))
    19:       #print ':longopt'
    20:     else:
    21:       match = shortopts.match(word)
    22:       if match:
    23:         #print ':shortopt'
    24:         for letter in match.group(1)[:-1]:
    25:           opts.append((letter,None))
    26:         opts.append((match.group(1)[-1],match.group(2)))
    27:       else:
    28:         #print ':filename'
    29:         filename = args[wordno]
    30:         result.append((opts,filename))
    31:         opts=[]
    32:     wordno = wordno + 1
    33: 
    34:   if opts:
    35:     result.append((opts,filename))
    36:   return result
End python section to interscript/getoptions.py[1]


6.20.1. Test 19: generic getoptions module