CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/PhysicsTools/FWLite/interface/CommandLineParser.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 #if !defined(CommandLineParser_H)
00004 #define CommandLineParser_H
00005 
00006 #include "PhysicsTools/FWLite/interface/VariableMapCont.h"
00007 
00008 namespace optutl
00009 {
00010 
00011 class CommandLineParser : public VariableMapCont
00012 {
00013    public:
00015       // Public Constants //
00017 
00018       static const std::string kSpaces;
00019 
00020       enum
00021       {
00022          kEventContOpt = 1 << 0
00023       };
00024 
00026       // friends //
00028       // tells particle data how to print itself out
00029       friend std::ostream& operator<< (std::ostream& o_stream, 
00030                                        const CommandLineParser &rhs);
00031 
00033       //            _         //
00034       // |\/|      |_         //
00035       // |  |EMBER | UNCTIONS //
00036       //                      //
00038 
00040       // Constructors and Destructor //
00042       CommandLineParser (const std::string &usage, 
00043                          unsigned int optionsType = kEventContOpt);
00044 
00046       // One Liners //
00048 
00049       // turn on (true) or off (false) printing of options by default
00050       void setPrintOptoins (bool print) { m_printOptions = print; }
00051 
00052       // return vector calling arguments
00053       const SVec argVec() const { return m_fullArgVec; }
00054 
00056       // Regular Member Functions //
00058 
00059       // parse the command line arguments.  If 'returnArgs' is true, then
00060       // any non-assignments and non-options will be returned.
00061       void parseArguments (int argc, char** argv, bool allowArgs = false);
00062 
00063       // prints out '--help' screen, then exits.
00064       void help();
00065 
00066       // print out all of the variables hooked up
00067       void printOptionValues();
00068 
00069       // Not called by users anymore.  Finish evaluating default options.
00070       // Pass in 'tag' if you want to modify the output name based on
00071       // options the user has passed in.  'tag' is not passed by const
00072       // reference because it may be modified.
00073       void _finishDefaultOptions (std::string tag = "");
00074 
00076       // Static Member Functions //
00078 
00079       // remove an ending (e.g., '.root') from a string
00080       static std::string removeEnding (const std::string &input, 
00081                                        const std::string &ending);   
00082 
00083       // splits a line into words
00084       static void split (SVec &retval, std::string line, 
00085                          std::string match = " \t",
00086                          bool ignoreComments = true);
00087 
00088       // removes '# ....' comment
00089       static void removeComment (std::string &line);
00090 
00091       // removes leading and trailing spaces
00092       static void removeLeadingAndTrailingSpaces (std::string &line);
00093 
00094       // given a line, finds first non-space word and rest of line
00095       static void findCommand (const std::string &line,
00096                                std::string &command,
00097                                std::string &rest);
00098 
00099 
00100   private:
00102       // Private Member Functions //
00104 
00105       // Sets a variable 'varname' to a 'value' from a string
00106       // 'varname=value'.  If 'dontOverrideChange' is set true, then the
00107       // function will NOT set a variable that has been already set.
00108       // This will allow you to read in a file to set most variables and
00109       // still be allowed to make changes from the command line.  If you
00110       // want to ignore the first 'n' characters, simply set 'offset=n'.
00111       bool _setVariableFromString (const std::string &arg,
00112                                    bool dontOverrideChange = false,
00113                                    int offset = 0);
00114 
00115       // sets variable options from file where lines are formatted as
00116       // - var=value
00117       bool _setVariablesFromFile (const std::string &filename);
00118       
00119       // runs command embedded in arg
00120       bool _runVariableCommandFromString (const std::string &arg);
00121 
00122 
00123       // given a section number (1..N) and totalsection (N), fills output
00124       // list with correct files.
00125       void _getSectionFiles (const SVec &inputList, SVec &outputList,
00126                              int section, int totalSection);
00127 
00129       // Private Member Data //
00131       SVec         m_fullArgVec;
00132       std::string  m_argv0;
00133       std::string  m_usageString;  
00134       bool         m_printOptions;
00135       unsigned int m_optionsType;
00136 
00137 };
00138 
00139 }
00140 
00141 #endif // CommandLineParser_H