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
37
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
46namespace BLOCXX_NAMESPACE
47{
48
50
51
58class BLOCXX_COMMON_API CmdLineParser
59{
60public:
67
68 // errors codes that may be specified when a CmdLineParserException is thrown
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
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
188private:
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
199
200#ifdef BLOCXX_WIN32
201#pragma warning (pop)
202#endif
203
204};
205
206} // end namespace BLOCXX_NAMESPACE
207
208#endif
209
210
#define BLOCXX_DECLARE_APIEXCEPTION(NAME, LINKAGE_SPEC)
Declare a new exception class named <NAME>Exception that derives from Exception This macro is typical...
Do command line parsing.
CmdLineParser(int argc, char const *const *const argv, const Option *options, EAllowNonOptionArgsFlag allowNonOptionArgs)
@ E_REQUIRED_ARG
the option requires an argument
@ E_NO_ARG
the option does not take an argument
@ E_OPTIONAL_ARG
the option might have an argument
SortedVectorMap< int, StringArray > optionsMap_t
@ E_INVALID_OPTION
an unknown option was specified
@ E_INVALID_NON_OPTION_ARG
a non-option argument was specified, but they are not allowed
@ E_MISSING_ARGUMENT
an option for which argtype == E_REQUIRED_ARG did not have an argument
@ E_MISSING_OPTION
the option wasn't specified
@ E_NON_OPTION_ARGS_ALLOWED
Non-option arguments are allowed.
@ E_NON_OPTION_ARGS_INVALID
Non-option arguments are invalid.
This String class is an abstract data type that represents as NULL terminated string of characters.
Definition String.hpp:67
Taken from RFC 1321.
Array< String > StringArray
Definition CommonFwd.hpp:73
char shortopt
short option char. Set to '\0' for none.
EArgumentTypeFlag argtype
specifies constraints for the option's argument
const char * longopt
long option string. Set to 0 for none.
const char * description
description used by getUsage(). May be 0.
const char * defaultValue
if argtype == E_OPTIONAL_ARG and no argument is specified, this value will be returned....
int id
unique option id, used to retrieve option values