Library of Assembled Shared Sources
arg_parser.h
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2023 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43
44/** @defgroup ArgParser ArgParser
45 * @brief library to parse GNU style program arguments
46 * @author Bram de Greve [BdG]
47 */
48
49
50
51#ifndef LASS_GUARDIAN_OF_INCLUSION_IO_ARG_PARSER_H
52#define LASS_GUARDIAN_OF_INCLUSION_IO_ARG_PARSER_H
53
54#include "io_common.h"
55#include "../util/call_traits.h"
56
57namespace lass
58{
59namespace io
60{
61
62class ArgParameter;
63
64
65
66/** @ingroup ArgParser
67 */
69{
70 amNoValue = 1, /**< argument takes no value (argument is flag) */
71 amRequired = 2, /**< if argument is used, a value is required */
72 amOptional = 4, /**< if argument is used, it can take a value, but is not required */
73 amMultiple = 8, /**< argument can have multiple values. */
74
75 amDefault = amRequired /**< default mode for arguments. */
76};
77
78
79
80/** @ingroup ArgParser
81 * @brief structure to store ArgFlag and ArgValue formats
82 */
84{
85 std::string shortName; /**< short name of ArgFlag or ArgValue */
86 std::string longName; /**< long name of ArgFlag or ArgValue */
87 std::string description; /**< set description of value, unused by ArgFlag */
88 int argMode; /**< set argument mode for value, unused by ArgFlag */
89 bool hasDefault; /**< set true if ArgValue has default value, unused by ArgFlag */
90 std::string defaultValue; /**< only valid if @e hasDefault is true, unused by ArgFlag */
91};
92
93
94
95/** @ingroup ArgParser
96 * @brief the parser itself
97 */
99{
100public:
101
102 typedef std::vector<std::string> TArguments;
103
104 ArgParser();
105 ArgParser(const std::string& iProgramName,
106 const std::string& iVersion = "",
107 const std::string& iPositionalArguments = "");
108
109 bool parse(const std::string& iArguments, TArguments* oPositionals = 0);
110 bool parse(const TArguments& iArguments, TArguments* oPositionals = 0);
111 bool parse(int iArgc, char* iArgv[], TArguments* oPositionals = 0);
112
113 std::string usage() const;
114
115private:
116
117 friend class ArgParameter;
118
119 typedef std::vector<ArgParameter*> TParameters;
120 typedef TParameters::size_type TSize;
121
122 void subscribe(ArgParameter& iArg);
123 bool parseShort(const TArguments& iArguments, TSize& ioIndex);
124 bool parseLong(const TArguments& iArguments, TSize iIndex);
125 bool isValidLongName(const std::string& iLongName) const;
126 bool writeVersionOrHelp(const std::string& iArgument) const;
127 void writeVersion() const;
128 void writeHelp() const;
129
130 std::string programName_;
131 std::string programVersion_;
132 std::string positionals_;
133 TParameters parameters_;
134 bool isQuiet_;
135};
136
137
138
139/** @ingroup ArgParser
140 */
141class LASS_DLL ArgParameter
142{
143public:
144
145 virtual ~ArgParameter();
146
147 const std::string& shortName() const;
148 const std::string& longName() const;
149 int mode() const;
150 const std::string format() const;
151
152 bool operator!() const;
153 explicit operator bool() const;
154
155protected:
156
157 ArgParameter(ArgParser& iParser,
158 const std::string& iShortName,
159 const std::string& iLongName,
160 int iArgMode = amDefault);
161
162 const std::string names() const;
163 bool parserIsQuiet() const;
164 bool setValue(const std::string& iValue);
165 void set();
166
167private:
168
169 friend class ArgParser;
170
171 virtual const std::string doFormat() const;
172 virtual bool doSetValue(const std::string& iValue);
173
174 ArgParser& parser_;
175 std::string shortName_;
176 std::string longName_;
177 int mode_;
178 bool isSet_;
179};
180
181
182
183/** @ingroup ArgParser
184 */
185class LASS_DLL ArgFlag: public ArgParameter
186{
187public:
188
189 ArgFlag(ArgParser& iParser,
190 const std::string& iShortName,
191 const std::string& iLongName);
192 ArgFlag(ArgParser& iParser,
193 const ArgFormat& iFormat);
194};
195
196
197
198/** @ingroup ArgParser
199 */
200template <typename T>
201class ArgValue: public ArgParameter
202{
203public:
204
205 typedef typename util::CallTraits<T>::TValue TValue;
206 typedef typename util::CallTraits<T>::TParam TParam;
207 typedef typename util::CallTraits<T>::TConstReference TConstReference;
208 typedef std::vector<TValue> TValues;
209 typedef typename TValues::const_iterator TValueIterator;
210
211 ArgValue(ArgParser& iParser,
212 const std::string& iShortName,
213 const std::string& iLongName,
214 const std::string& iDescription = "",
215 int iArgMode = amDefault);
216 ArgValue(ArgParser& iParser,
217 const std::string& iShortName,
218 const std::string& iLongName,
219 const std::string& iDescription,
220 int iArgMode,
221 TParam iDefault);
222 ArgValue(ArgParser& iParser,
223 const ArgFormat& iFormat);
224
225 const TValues& all() const;
226 size_t size() const;
227 typename ArgValue::TConstReference operator[](size_t iIndex) const;
228 typename ArgValue::TConstReference at(size_t iIndex) const;
229 TValueIterator begin() const;
230 TValueIterator end() const;
231
232private:
233
234
235 const std::string doFormat() const override;
236 bool doSetValue(const std::string& iValue) override;
237
238 void checkMode(int iArgMode) const;
239
240 TValues values_;
241 TValues default_;
242 std::string description_;
243};
244
245
246
247
248/** @ingroup ArgParser
249 * Exception thrown in case of bad arguments.
250 */
251class ArgBadArgument: public util::ExceptionMixin<ArgBadArgument>
252{
253public:
254 ArgBadArgument(std::string msg, std::string loc): util::ExceptionMixin<ArgBadArgument>(std::move(msg), std::move(loc)) {}
255 ~ArgBadArgument() noexcept {}
256};
257
258
259
260
261}
262
263}
264
265#include "arg_parser.inl"
266
267#endif
268
269// EOF
the parser itself
Definition arg_parser.h:99
bool parse(const std::string &iArguments, TArguments *oPositionals=0)
parse arguments that come by a string.
ArgParser()
Construct a quiet parser.
@ amNoValue
argument takes no value (argument is flag)
Definition arg_parser.h:70
@ amOptional
if argument is used, it can take a value, but is not required
Definition arg_parser.h:72
@ amDefault
default mode for arguments.
Definition arg_parser.h:75
@ amRequired
if argument is used, a value is required
Definition arg_parser.h:71
@ amMultiple
argument can have multiple values.
Definition arg_parser.h:73
#define LASS_DLL
DLL interface: import or export symbols?
streams, binary streams, vrmlstreams, ...
Library for Assembled Shared Sources.
Definition config.h:53
structure to store ArgFlag and ArgValue formats
Definition arg_parser.h:84
std::string shortName
short name of ArgFlag or ArgValue
Definition arg_parser.h:85
std::string longName
long name of ArgFlag or ArgValue
Definition arg_parser.h:86
std::string defaultValue
only valid if hasDefault is true, unused by ArgFlag
Definition arg_parser.h:90
std::string description
set description of value, unused by ArgFlag
Definition arg_parser.h:87
bool hasDefault
set true if ArgValue has default value, unused by ArgFlag
Definition arg_parser.h:89
int argMode
set argument mode for value, unused by ArgFlag
Definition arg_parser.h:88