Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:23:22

0001 // -*- C++ -*-
0002 
0003 #if !defined(CommandLineParser_H)
0004 #define CommandLineParser_H
0005 
0006 #include "PhysicsTools/FWLite/interface/VariableMapCont.h"
0007 
0008 namespace optutl {
0009 
0010   class CommandLineParser : public VariableMapCont {
0011   public:
0012     //////////////////////
0013     // Public Constants //
0014     //////////////////////
0015 
0016     static const std::string kSpaces;
0017 
0018     enum { kEventContOpt = 1 << 0 };
0019 
0020     /////////////
0021     // friends //
0022     /////////////
0023     // tells particle data how to print itself out
0024     friend std::ostream &operator<<(std::ostream &o_stream, const CommandLineParser &rhs);
0025 
0026     //////////////////////////
0027     //            _         //
0028     // |\/|      |_         //
0029     // |  |EMBER | UNCTIONS //
0030     //                      //
0031     //////////////////////////
0032 
0033     /////////////////////////////////
0034     // Constructors and Destructor //
0035     /////////////////////////////////
0036     CommandLineParser(const std::string &usage, unsigned int optionsType = kEventContOpt);
0037 
0038     ////////////////
0039     // One Liners //
0040     ////////////////
0041 
0042     // turn on (true) or off (false) printing of options by default
0043     void setPrintOptoins(bool print) { m_printOptions = print; }
0044 
0045     // return vector calling arguments
0046     const SVec argVec() const { return m_fullArgVec; }
0047 
0048     //////////////////////////////
0049     // Regular Member Functions //
0050     //////////////////////////////
0051 
0052     // parse the command line arguments.  If 'returnArgs' is true, then
0053     // any non-assignments and non-options will be returned.
0054     void parseArguments(int argc, char **argv, bool allowArgs = false);
0055 
0056     // prints out '--help' screen, then exits.
0057     void help();
0058 
0059     // print out all of the variables hooked up
0060     void printOptionValues();
0061 
0062     // Not called by users anymore.  Finish evaluating default options.
0063     // Pass in 'tag' if you want to modify the output name based on
0064     // options the user has passed in.  'tag' is not passed by const
0065     // reference because it may be modified.
0066     void _finishDefaultOptions(std::string tag = "");
0067 
0068     /////////////////////////////
0069     // Static Member Functions //
0070     /////////////////////////////
0071 
0072     // remove an ending (e.g., '.root') from a string
0073     static std::string removeEnding(const std::string &input, const std::string &ending);
0074 
0075     // splits a line into words
0076     static void split(SVec &retval, std::string line, std::string match = " \t", bool ignoreComments = true);
0077 
0078     // removes '# ....' comment
0079     static void removeComment(std::string &line);
0080 
0081     // removes leading and trailing spaces
0082     static void removeLeadingAndTrailingSpaces(std::string &line);
0083 
0084     // given a line, finds first non-space word and rest of line
0085     static void findCommand(const std::string &line, std::string &command, std::string &rest);
0086 
0087   private:
0088     //////////////////////////////
0089     // Private Member Functions //
0090     //////////////////////////////
0091 
0092     // Sets a variable 'varname' to a 'value' from a string
0093     // 'varname=value'.  If 'dontOverrideChange' is set true, then the
0094     // function will NOT set a variable that has been already set.
0095     // This will allow you to read in a file to set most variables and
0096     // still be allowed to make changes from the command line.  If you
0097     // want to ignore the first 'n' characters, simply set 'offset=n'.
0098     bool _setVariableFromString(const std::string &arg, bool dontOverrideChange = false, int offset = 0);
0099 
0100     // sets variable options from file where lines are formatted as
0101     // - var=value
0102     bool _setVariablesFromFile(const std::string &filename);
0103 
0104     // runs command embedded in arg
0105     bool _runVariableCommandFromString(const std::string &arg);
0106 
0107     // given a section number (1..N) and totalsection (N), fills output
0108     // list with correct files.
0109     void _getSectionFiles(const SVec &inputList, SVec &outputList, int section, int totalSection);
0110 
0111     /////////////////////////
0112     // Private Member Data //
0113     /////////////////////////
0114     SVec m_fullArgVec;
0115     std::string m_argv0;
0116     std::string m_usageString;
0117     bool m_printOptions;
0118     unsigned int m_optionsType;
0119   };
0120 
0121 }  // namespace optutl
0122 
0123 #endif  // CommandLineParser_H