Module spug.util.ComLine

Command line parsing utility.

Synopsis:

      from spug.util.ComLine import helpOption, ArgType, ComLine
      
      # define the command line options
      clopts = \
         [
         # standard help option (-h or --help)
         helpOption(),
         
         # option -f, --file, or --file-to-use which accepts a single
         # parameter
         ArgType('file', 'f', ['file', 'file-to-use'], parm = 1, 
                 parmHelp = 'file-name',
                 help = 'Specifies the file to be used.'
                 ),
         
         # a verbosity option
         ArgType('verbose', 'v', ['verbose'], help = 'be verbose'),
         ]
      
      # we define the help prefix, options will be automatically printed
      # if help is requested.
      cl = ComLine(clopts, helpPrefix = '''
      widget - defines various widgets
      Usage:
         widget [options] widget-name ...
      ''')
      
      # "others" gives us the list of non-option arguments.  Check to 
      # see if it is empty, show usage blurb if it is.
      if not cl.others():
         cl.help()
      
      # get file option (None if none was supplied).
      file = cl['file']
      
      # get verbosity (1 or None)
      verbose = cl['verbose']

Classes

Functions

helpOption()

def helpOption():

Clients may use this function to easily create the officially sanctioned help function.

help(helpText, force)

def help(helpText, force = 0):

Import this function when you just need to provide the "help" command line option. For example:

          from spug.util.ComLine import help
          help('''
          foo - program to do foo
          Options:
          '''
          )

If you pass in a true value for force, it will force the help message to be printed no matter what the command line options are.