Totem command line parser

Using Totem command line parser ( one may pass arguments to a configuration file directly from a command line, e.g.:

cmsRun +nevents=1 +beta=2p5 +energy=3500

This CL parser defines several common options such as nevents, energy, beta, help etc. so user of this parser doesn't need to define those options in each configuration. This parser uses python modul argparse (see .

The Problem

The problem is that if we try to pass arguments to a configuration file ( from command line like:

cmsRun arg1 arg2 ...

then all arguments arg1, arg2, ... are passed by cmsRun to the configuration file but only if they don't have a dash ('-') in prefix, e.g. -x or --xxx are not allowed, because options with a dash prefix are recognized by cmsRun and not by So we can't do e.g. :

cmsRun -nevents=10

which would be realy nice... The arg1, arg2, etc. may be accesed in using sys.argv:

import sys

 for arg in sys.argv:
     print arg, "\n"

but then we have to do all the arguments handling on our own which is really tedious...

Elegant Solution

Using purely python modul argparse (see we may avoid many problems because it is posible to configure also prefix '+' for arg options instead of just dash ('-'). This aproach is used by totemCLparser which predefine some options such as nevents, energy, optics, help (i.e., options which are common for many users). So it is possible from command line to do something like:

cmsRun -p +nevents=10 +energy=3500 +beta=2.5

these +options are correctly passed to the as options and possible -options are passed to the cmsRun. Definition of the first +option in is just:

    "+nevents",                 # option name(s) used from command line
    dest     = 'nevents',     # one common name
    default = -1,                # dafault value
    type     = int,               # support for types int,float,complex...
    action  = "store",        
    help     = "number of events to be processed"

so it is really easy to specify default value for an option, type, define help and much more (all one needs...). Help for all options is automatically generated and may be printed to a screen also with usage etc - as it is common in unix world...

For details and possible options see documentation of argparse, mainly the pdf which is there:

How to use totemCLparser

Into your configuration file put:

# import the totem CL parser
from Configuration.TotemCommon.totemCLParser import parser

# add new option if you want
    dest    = 'nplanes',
    default = 3,
    type    = int,
    action  = "store",
    help    = "minimum number of planes needed for fitting"

# default values of some options are not defined (energy, beta and so on) so it is necessery to define it here
# (totemCLparser sets default values of all the options which defines to "None" so user is forced to specify default values of those options)
# one may set here also default value for "nplanes" argument
      nevents =-1, 
      beta       = "2p5", 
      energy   = 3500
      #nplanes = 3

# here the parser reads and sets values passed from command line (or just sets the default values) 
args = parser.parser_args()

# prints all arguments and their values handled by the parser
print "Parsed arguments = ", args

# now, we may set the number of events 
process.maxEvents = cms.untracked.PSet(
    input = cms.untracked.int32(args.nevents) 

# print value of  newly defined argument "nplanes" (or do something else with nplanes)
print 'Number of planes', args.nplanes

To see help for our defined options (and options predefined by totemCLparser) just use +h (or ++help) option

cmsRun +h

and we obtain something like:

usage: [+nevents NEVENTS] [+beta BETA]  [+energy ENERGY] [+h] [+nplanes NPLANES]  cfgfilename

positional arguments:
  cfgfilename           cfg file name; this positional argument has to be  defined to suppres one error...

optional arguments:
  +nevents NEVENTS      number of events to be processed, as default all
                                       events from a source are processed
  +beta BETA                  beta* - optics
  +energy ENERGY        energy
  +h, ++help                    show this help message and exit

  +nplanes NPLANES      minimum number of planes needed  for  fitting

%MSG-s ConfigFileReadError:  25-Mar-2010 19:09:24 CET pre-events
Problem with configuration file
---- Configuration BEGIN
python encountered the error: exceptions.SystemExit 0
---- Configuration END


-- JiriProchazka - 25-Mar-2010

Edit | Attach | Watch | Print version | History: r4 < r3 < r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions...
Topic revision: r3 - 2010-03-26 - JiriProchazka
    • Cern Search Icon Cern Search
    • TWiki Search Icon TWiki Search
    • Google Search Icon Google Search

    TOTEM All webs login

This site is powered by the TWiki collaboration platform Powered by PerlCopyright & 2008-2022 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
or Ideas, requests, problems regarding TWiki? use Discourse or Send feedback