blocxx
CmdLineParser.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright (C) 2005, Vintela, Inc. All rights reserved.
3 * Copyright (C) 2006, Novell, Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of
14 * Vintela, Inc.,
15 * nor Novell, Inc.,
16 * nor the names of its contributors or employees may be used to
17 * endorse or promote products derived from this software without
18 * specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 *******************************************************************************/
32 
33 
38 #ifndef BLOCXX_CMD_LINE_PARSER_HPP_INCLUDE_GUARD_
39 #define BLOCXX_CMD_LINE_PARSER_HPP_INCLUDE_GUARD_
40 #include "blocxx/BLOCXX_config.h"
41 #include "blocxx/String.hpp"
43 #include "blocxx/Array.hpp"
44 #include "blocxx/Exception.hpp"
45 
46 namespace BLOCXX_NAMESPACE
47 {
48 
50 
51 
58 class BLOCXX_COMMON_API CmdLineParser
59 {
60 public:
62  {
65  E_OPTIONAL_ARG
66  };
67 
68  // errors codes that may be specified when a CmdLineParserException is thrown
70  {
74  E_MISSING_OPTION
75  };
76 
77  struct Option
78  {
79  int id;
80  char shortopt;
81  const char* longopt;
83  const char* defaultValue;
84  const char* description;
85  };
86 
88  {
90  E_NON_OPTION_ARGS_INVALID
91  };
92 
102  CmdLineParser(int argc, char const* const* const argv, const Option* options, EAllowNonOptionArgsFlag allowNonOptionArgs);
103 
112  String getOptionValue(int id, const char* defaultValue = "") const;
113 
125  String mustGetOptionValue(int id, const char* exceptionMessage = "") const;
126 
133  StringArray getOptionValueList(int id) const;
134 
144  StringArray mustGetOptionValueList(int id, const char* exceptionMessage = "") const;
145 
149  bool isSet(int id) const;
150 
154  size_t getNonOptionCount () const;
155 
160  String getNonOptionArg(size_t n) const;
161 
165  StringArray getNonOptionArgs() const;
166 
185  static String getUsage(const Option* options, unsigned int maxColumns = 80);
186 
187 
188 private:
189 
190 #ifdef BLOCXX_WIN32
191 #pragma warning (push)
192 #pragma warning (disable: 4251)
193 #endif
194 
195  // key is Option::id, value is the value(s) specified by the user
197  optionsMap_t m_parsedOptions;
199 
200 #ifdef BLOCXX_WIN32
201 #pragma warning (pop)
202 #endif
203 
204 };
205 
206 } // end namespace BLOCXX_NAMESPACE
207 
208 #endif
209 
210 
Array<> wraps std::vector<> in COWReference<> adding ref counting and copy on write capability...
Definition: Array.hpp:65
char shortopt
short option char. Set to '\0' for none.
const char * longopt
long option string. Set to 0 for none.
Taken from RFC 1321.
int id
unique option id, used to retrieve option values
This String class is an abstract data type that represents as NULL terminated string of characters...
Definition: String.hpp:66
an option for which argtype == E_REQUIRED_ARG did not have an argument
a non-option argument was specified, but they are not allowed
the option does not take an argument
Do command line parsing.
SortedVectorMap< int, StringArray > optionsMap_t
const char * description
description used by getUsage(). May be 0.
#define BLOCXX_DECLARE_APIEXCEPTION(NAME, LINKAGE_SPEC)
Declare a new exception class named Exception that derives from Exception This macro is typical...
Definition: Exception.hpp:396
const char * defaultValue
if argtype == E_OPTIONAL_ARG and no argument is specified, this value will be returned. Set to 0 for none.
EArgumentTypeFlag argtype
specifies constraints for the option's argument