//---------------------------------------------------------------------------- // // Copyright (C) 2004 Intel Corporation // // File: Options.h // // Notes: This file contains the definition of a generic class for command line options // parsing. // //---------------------------------------------------------------------------- #ifndef _OPTIONS_H #define _OPTIONS_H #include /* Options is a generic class for parsing command line arguments. The command line is expected to be of the form: progname.exe -opt1 val1 -opt2 val2... where opt1 is the option name, and val1 is the value for this option. The option names are specified in advance, and the ones specified are the only valid ones: any other option name is considered an error. The values of the parameters are optional, but it must be specified in advance whether each parameter has a value. Any other parameter in the command line, that doesn't start with '-' is ignored, and would not cause errors. */ class Options { private: static const unsigned int USER_MAX_LEN = 32; static const unsigned int PASS_MAX_LEN = 32; static const unsigned int PASS_MIN_LEN = 8; static const unsigned int CERT_CN_MAX_LEN = 64; //internal function object for comparing strings in map (case insensitive) struct StrLess { bool operator()(const char* left, const char* right) const; }; public: //One node in the format list of the options. struct FormatNode { const char* option; //Name of option bool bHasParam; //Indication whether this option has a parameter }; //class that represents the entire format of the options. class Format { public: //ctor to build Format from a list of FormatNode's Format(const FormatNode rawFormat[], int rawFormatSize); //default ctor Format(); //Add one option to the format, with option name and parameter indication. void AddOption(const char* option, bool bHasParam); //returns true iff a certain option exists in the format. bool OptionExists(const char *option) const; //returns true iff a certain option has a prameter indicated by the format. bool OptionHasParam(const char* option) const; private: typedef std::map FormatMap; //Map that holds the association between option name and parameter indication. FormatMap m_formatMap; }; //ctor to build Options by parsing a command line, with a given Format object. Options(int argc, const char* argv[], const Format &format); //ctor to build Options by parsing a command line, with a given array of FormatNode's Options(int argc, const char* argv[], const FormatNode rawFormat[], int rawFormatSize); //default ctor. parse() should be used when using it. Options(); //return the value of a given option. //Returns NULL if option doesn't exist, "" if option has no value. const char* GetOption(const char* option) const; //returns true iff a certain option exists in the Options object. bool OptionExists(const char* option) const; //returns true iff parse was called - either directly or by a ctor. //Will return false if parsing wasn't done or if it failed. bool IsParsed() const {return m_parsed;} //parse a command line according to Format object, and store results in this Options object. bool Parse(int argc, const char* argv[], const Format &format); //parse a command line according to array of FormatNode's, and store results in this //Options object. bool Parse(int argc, const char* argv[], const FormatNode rawFormat[], int rawFormatSize); //validate use-name bool validateUserName(int len); //validate password bool validatePassword(int len); //validate certificate CN bool validateCertificateCN(int len); private: typedef std::map OptionsMap; OptionsMap m_optionsMap; //map that associates option names with their values. bool m_parsed; //was parse() called }; #endif //#ifndef _OPTIONS_H