From: Christian P. <cp...@us...> - 2005-01-20 19:56:42
|
Update of /cvsroot/pclasses/pclasses2/include/pclasses/App In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv9042/include/pclasses/App Modified Files: CmdLine.h Log Message: Finished advanced command line parsing. Index: CmdLine.h =================================================================== RCS file: /cvsroot/pclasses/pclasses2/include/pclasses/App/CmdLine.h,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- CmdLine.h 20 Jan 2005 11:01:59 -0000 1.3 +++ CmdLine.h 20 Jan 2005 19:56:29 -0000 1.4 @@ -22,8 +22,9 @@ #define P_App_CmdLine_h #include <pclasses/Export.h> +#include <pclasses/Exception.h> #include <string> -#include <map> +#include <vector> namespace P { @@ -33,30 +34,48 @@ class PAPP_EXPORT CmdLineOption { public: CmdLineOption(const std::string& shortOpt, const std::string& longOpt, - bool required); + const std::string& helpText, bool required, bool needsValue); - ~CmdLineOption(); + virtual ~CmdLineOption(); const std::string& shortName() const throw(); const std::string& longName() const throw(); + const std::string& helpText() const throw(); + bool required() const throw(); + bool isset() const throw(); + + bool needValue() const throw(); + + virtual void setValue(const std::string& val) = 0; + virtual std::string value() const throw() = 0; + + protected: + bool _isset; + private: std::string _shortOpt; std::string _longOpt; + std::string _helpText; bool _required; + bool _needValue; }; //! Command-line option flag class PAPP_EXPORT CmdLineFlag: public CmdLineOption { public: CmdLineFlag(const std::string& shortOpt, const std::string& longOpt, - bool defaultVal, bool required = false); + const std::string& helpText, bool defaultVal = false, + bool required = false); ~CmdLineFlag(); + void setValue(const std::string& val); + std::string value() const throw(); + private: bool _defaultVal; }; @@ -65,12 +84,25 @@ class PAPP_EXPORT CmdLineValue: public CmdLineOption { public: CmdLineValue(const std::string& shortOpt, const std::string& longOpt, - const std::string& defaultVal, bool required = false); + const std::string& helpText, const std::string& defaultVal); + + CmdLineValue(const std::string& shortOpt, const std::string& longOpt, + const std::string& helpText, bool requird = false); ~CmdLineValue(); + void setValue(const std::string& val); + std::string value() const throw(); + private: std::string _defaultVal; + std::string _value; +}; + +class PAPP_EXPORT CmdLineError: public RuntimeError { + public: + CmdLineError(const std::string& what, const SourceInfo& si); + ~CmdLineError(); }; //! Advanced command-line parser @@ -79,15 +111,22 @@ CmdLineParser(CmdLineOption* opts[] = 0); ~CmdLineParser(); - bool parse(int argc, char* argv[]); - bool parse(const std::string& cmdline); + void parse(int argc, char* argv[]) throw(CmdLineError); + void parse(const std::string& cmdline) throw(CmdLineError); + + //! Returns a unnamed command line value + const std::string& value(unsigned int index) const throw(OutOfBounds); + + //! Returns the number of unnamed command line values + unsigned int valueCount() const throw(); + + void dumpHelp(std::ostream& os) const; private: - typedef std::map<std::string, - CmdLineOption*> ParsedCmdOptMap; + typedef std::vector<std::string> StringVector; - CmdLineOption** _opts; - ParsedCmdOptMap _parsedOpts; + CmdLineOption** _opts; + StringVector _unnamedValues; }; } // !namespace App |