r1 - 12 Dec 2004 - 20:48:40 - MikeTYou are here: OSAF >  Journal Web  >  TWikiUsers > MikeT > MikeTaylorNotes > MikeTaylor20041212

Command line parsing

Migrated a routine that I use in my python code to parse and handle command line options for use in Chandler. I removed for now the ability to have options read in from a configuration file (haven't heard of a need for it in Chandler) but I added the ability for any command line option to get it's value from an environment variable.

What that allows is for any option variable, say foo, to have the following "life cycle":

  1. internal default value set
  2. if present, value is read from the associated environment variable
  3. if found on the command line, use that value

Currently Chandler was doing ad-hoc process of os.environ and also sys.argv and now with this change all of that is moved into a single utility function and options are stored in Globals.options

The option 'profileDir' is handled as a special case - it's value is run thru os.path.expanduser to change any '~' to the os specific user's HOME directory.

def _loadConfig():
    """
    Find and load the configuration file by searching for it in a preset list of locations.
    Sets Globals.options 
    """
      #@@@ Globals.chandlerDirectory can be replaced by '~' if a true profile directory is needed - bear
      
                      # option name, (value, short cmd, long cmd, type flag, default, environment variable, help text)
    _configItems = { 'parcelDir':  ('-p', '--parcelDir',  's', None,  'PARCELDIR', 'Location for private/user parcels'),
                     'stderr':     ('-e', '--stderr',     'b', False, None,        'Echo error output to log file'),
                     'create':     ('-c', '--create',     'b', False, None,        'Force creation of a new repository'),
                     'ramdb':      ('-d', '--ramdb',      'b', False, None,        ''),
                     'exclusive':  ('-x', '--exclusive',  'b', False, None,        'open repository exclusive'),
                     'repo':       ('-r', '--repo',       's', None,  None,        'repository to copy during startup'),
                     'profileDir': ('',   '--profileDir', 's', Globals.chandlerDirectory, 'PROFILE_DIR', 'location of the Chandler Repository'),
                      }

    usage  = "usage: %prog [options]"   # %prog expands to os.path.basename(sys.argv[0])
    parser = OptionParser(usage=usage, version="%prog " + __version__)

    for key in _configItems:
      (shortCmd, longCmd, optionType, defaultValue, environName, helpText) = _configItems[key]
    
      if environName and os.environ.has_key(environName):
          defaultValue = os.environ[environName]

      if optionType == 'b':
          parser.add_option(shortCmd, longCmd, dest=key, action='store_true', default=defaultValue, help=helpText)
      else:
          parser.add_option(shortCmd, longCmd, dest=key, default=defaultValue, help=helpText)

    (Globals.options, Globals.args) = parser.parse_args()

    if Globals.options.profileDir:
        Globals.options.profileDir = os.path.expanduser(Globals.options.profileDir)

-- MikeT - 13 Dec 2004

Edit | WYSIWYG | Attach | Printable | Raw View | Backlinks: Web, All Webs | History: r1 | More topic actions
 
Open Source Applications Foundation
Except where otherwise noted, this site and its content are licensed by OSAF under an Creative Commons License, Attribution Only 3.0.
See list of page contributors for attributions.