1 /************************************************************************/
\r
2 /*! \class CommandLine
\r
3 \brief Command-line opition parser.
\r
5 Copyright (c) 2005 Robin Davies.
\r
7 Permission is hereby granted, free of charge, to any person
\r
8 obtaining a copy of this software and associated documentation files
\r
9 (the "Software"), to deal in the Software without restriction,
\r
10 including without limitation the rights to use, copy, modify, merge,
\r
11 publish, distribute, sublicense, and/or sell copies of the Software,
\r
12 and to permit persons to whom the Software is furnished to do so,
\r
13 subject to the following conditions:
\r
15 The above copyright notice and this permission notice shall be
\r
16 included in all copies or substantial portions of the Software.
\r
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
\r
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
\r
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
\r
21 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
\r
22 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
\r
23 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
\r
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
26 /************************************************************************/
\r
34 #include <exception>
\r
39 class CommandLineException: public std::exception {
\r
41 CommandLineException(const std::string &error)
\r
45 const char*what() { return s.c_str(); }
\r
54 virtual ~CommandLine();
\r
56 void ProcessCommandLine(int argc, char**argv);
\r
57 void ProcessCommandLine(const std::vector<std::string>& cmdline);
\r
59 template <class TVAL> void AddOption(const char*name,TVAL*pResult, TVAL defaultValue)
\r
61 this->optionHandlers.push_back(new COptionHandler<TVAL>(name,pResult));
\r
62 *pResult = defaultValue;
\r
65 template <class TVAL> void AddOption(const char*name,TVAL*pResult)
\r
67 this->optionHandlers.push_back(new COptionHandler<TVAL>(name,pResult));
\r
69 const std::vector<std::string> &GetArguments() { return args; }
\r
70 template <class T> void GetArgument(size_t arg, T*pVal)
\r
72 if (arg >= args.size()) {
\r
73 std::stringstream os;
\r
74 os << "Argument " << (arg+1) << " not provided.";
\r
75 throw CommandLineException(os.str());
\r
77 std::stringstream is(args[arg]);
\r
80 if (!is.fail() && is.eof())
\r
84 std::stringstream os;
\r
85 os << "Argument " << (arg+1) << " was not in the correct format.";
\r
86 throw CommandLineException(os.str());
\r
89 void GetArgument(size_t arg, std::string*pVal)
\r
91 if (arg >= args.size()) {
\r
92 std::stringstream os;
\r
93 os << "Argument " << (arg+1) << " not provided.";
\r
94 throw CommandLineException(os.str());
\r
102 class COptionHandlerBase {
\r
104 COptionHandlerBase(const std::string & name) { this->name = name;}
\r
105 virtual ~COptionHandlerBase() { };
\r
106 const std::string &getName() const { return name; }
\r
107 virtual bool HasArgument()const = 0;
\r
108 virtual void Process(const char* value) const = 0;
\r
112 template <class T> class COptionHandler: public COptionHandlerBase {
\r
114 COptionHandler(const std::string &name,T *result, T defaultValue = T())
\r
115 : COptionHandlerBase(name)
\r
118 *_pResult = defaultValue;
\r
121 virtual bool HasArgument() const;
\r
123 virtual void Process(const char *strValue) const {
\r
124 std::stringstream is(strValue);
\r
127 if (!is.fail() && is.eof())
\r
131 std::stringstream os;
\r
132 os << "Invalid value provided for option '" << getName() << "'.";
\r
133 throw CommandLineException(os.str().c_str());
\r
138 std::string strName;
\r
141 const CommandLine::COptionHandlerBase*GetOptionHandler(const std::string &name) const;
\r
143 std::vector<std::string > args;
\r
144 std::vector<COptionHandlerBase*> optionHandlers;
\r
147 // Argument specializations for bool.
\r
148 template <class T> bool CommandLine::COptionHandler<T>::HasArgument()const {
\r
151 inline bool CommandLine::COptionHandler<bool>::HasArgument() const {
\r
154 inline void CommandLine::COptionHandler<bool>::Process(const char*strValue) const {
\r
155 if (strValue == NULL)
\r
170 throw CommandLineException("Please specify '+' or '-' for boolean options.");
\r
173 // specializations for std::string.
\r
174 inline void CommandLine::COptionHandler<std::string >::Process(const char*strValue)const {
\r
175 *_pResult = strValue;
\r
178 // specializations for std::vector<std::string>
\r
179 inline void CommandLine::COptionHandler<std::vector<std::string> >::Process(const char*strValue)const {
\r
180 _pResult->push_back(strValue);
\r