OptionParser is designed for a fixed set of options. But if the first argument is a command that can affect which options are available, then OptionParser can still be used with some clever coding.
#!/usr/bin/env python
# $Id: options.py 17 2013-11-07 03:43:22Z andrewfn $
"""Demo of using OptionParser where the first argument is a command that can take variable options.
The help system is totally automated and the framework is very easy to use."""
ver = 1.0 #put version number here
use = {}
#put the commands in here, with the integer giving the number of extra arguments
use['command1'] = [0, ' (simple command)']
use['command2'] = [1, 'arg1 (command with a single argument)']
use['command3'] = [2,"""arg1 arg2 (command with two arguments
and a very very long description on more than one line.)"""]
use['help'] = [0, "[options] \nCommands:"]
copyright = "Copyright (c) Andrew Fountain 2014, freely distributed under GPL v3"
#The next section does not need to be changed
for c in sorted(use.iterkeys()):
if c != 'help':
use['help'][1] += "\n " + c + ' ' + use[c][1]
use['help'][1] += "\nFor more info try %prog --help"
from optparse import OptionParser
import sys
if len(sys.argv)