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