Library of Assembled Shared Sources
arg_parser.inl
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-2011 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#ifndef LASS_GUARDIAN_OF_INCLUSION_IO_ARG_PARSER_INL
44#define LASS_GUARDIAN_OF_INCLUSION_IO_ARG_PARSER_INL
45
46#include "io_common.h"
47#include "arg_parser.h"
48
49#include "../util/string_cast.h"
50
51namespace lass
52{
53namespace io
54{
55
56// --- ArgValue ------------------------------------------------------------------------------------
57
58template <typename T>
59ArgValue<T>::ArgValue(ArgParser& iParser,
60 const std::string& iShortName,
61 const std::string& iLongName,
62 const std::string& iDescription,
63 int iArgMode):
64 ArgParameter(iParser, iShortName, iLongName, iArgMode),
65 description_(iDescription)
66{
67 checkMode(iArgMode);
68}
69
70
71
72template <typename T>
73ArgValue<T>::ArgValue(ArgParser& iParser,
74 const std::string& iShortName,
75 const std::string& iLongName,
76 const std::string& iDescription,
77 int iArgMode,
78 TParam iDefault):
79 ArgParameter(iParser, iShortName, iLongName, iArgMode),
80 default_(1, iDefault),
81 description_(iDescription)
82{
83 checkMode(iArgMode);
84}
85
86
87
88template <typename T>
89ArgValue<T>::ArgValue(ArgParser& iParser,
90 const ArgFormat& iFormat):
91 ArgParameter(iParser, iFormat.shortName, iFormat.longName, iFormat.argMode),
92 description_(iFormat.description)
93{
94 if (iFormat.hasDefault)
95 {
96 default_.push_back(util::stringCast<T>(iFormat.defaultValue));
97 }
98 checkMode(iFormat.argMode);
99}
100
101
102
103/** return all values
104 */
105template <typename T>
106const typename ArgValue<T>::TValues& ArgValue<T>::all() const
107{
108 return !values_.empty() ? values_ : default_;
109}
110
111
112
113/** return number of values that are stored for this argument.
114 */
115template <typename T>
116size_t ArgValue<T>::size() const
117{
118 return all().size();
119}
120
121
122
123/** return iIndex'ed value.
124 */
125template <typename T>
126typename ArgValue<T>::TConstReference ArgValue<T>::operator[](size_t iIndex) const
127{
128 LASS_ASSERT(iIndex < size());
129 return all()[iIndex];
130}
131
132
133
134/** return iIndex'ed value with range check
135 */
136template <typename T>
137typename ArgValue<T>::TConstReference ArgValue<T>::at(size_t iIndex) const
138{
139 return all().at(iIndex);
140}
141
142
143
144/** return iterator to first value
145 */
146template <typename T>
147typename ArgValue<T>::TValueIterator ArgValue<T>::begin() const
148{
149 return all().begin();
150}
151
152
153
154/** return iterator to last value
155 */
156template <typename T>
157typename ArgValue<T>::TValueIterator ArgValue<T>::end() const
158{
159 return all().end();
160}
161
162
163
164/** check if mode is valid
165 * amNoValue can't be set, and exactly one of amOptional or amRequired must be set.
166 */
167template <typename T>
168void ArgValue<T>::checkMode(int iArgMode) const
169{
170 if (iArgMode & amNoValue)
171 {
172 LASS_THROW("You can't set the mode 'amNoValue' for a ArgValue '" << names() << "'.");
173 }
174 const int mask = amOptional | amRequired;
175 const int field = iArgMode & mask;
176 if (field != amOptional && field != amRequired)
177 {
178 LASS_THROW("For a ArgValue '" << names() << "' you must choose between mode 'amOptional' "
179 << "or 'amRequired'. At least one of these must be set, but not both.");
180 }
181}
182
183
184
185template <typename T>
186const std::string ArgValue<T>::doFormat() const
187{
188 LASS_ASSERT(!(mode() & amNoValue));
189
190 std::ostringstream result;
191 result << "[" << names() << " ";
192
193 if (mode() & amOptional)
194 {
195 result << "[";
196 }
197 result << "<" << (description_.empty() ? "value" : description_) << ">";
198 if (mode() & amMultiple)
199 {
200 result << " ...";
201 }
202 if (mode() & amOptional)
203 {
204 result << "]";
205 }
206
207 result << "]";
208 return result.str();
209}
210
211
212
213template <typename T>
214bool ArgValue<T>::doSetValue(const std::string& iValue)
215{
216 LASS_ASSERT(!(mode() & amNoValue));
217
218 if (iValue == "")
219 {
220 if (mode() & amRequired)
221 {
222 if (!parserIsQuiet())
223 {
224 LASS_THROW_EX(ArgBadArgument,
225 "Bad program arguments: you didn't provide a value for the parameter '"
226 << names() << "' as required.\n");
227 }
228 return false;
229 }
230
231 //LASS_LOG("parameter '" << names() << "' is set without value");
232 }
233 else
234 {
235 if (values_.size() != 0 && !(mode() & amMultiple))
236 {
237 if (!parserIsQuiet())
238 {
239 LASS_THROW_EX(ArgBadArgument,
240 "Bad program arguments: parameter '" << names() << "' already has a value '"
241 << values_[0] << "' assigned, you can't assign another value '" << iValue
242 << "' because this parameter cannot be multiple valued.\n");
243 }
244 return false;
245 }
246 else
247 {
248 try
249 {
250 values_.push_back(util::stringCast<T>(iValue));
251 }
252 catch (const util::BadStringCast&)
253 {
254 if (!parserIsQuiet())
255 {
256 LASS_THROW_EX(ArgBadArgument,
257 "Bad program arguments: could not interpret '" << iValue
258 << "' as a value of type '" << typeid(T).name()
259 << "' for parameter '" << names() << ".\n");
260 }
261 return false;
262 }
263
264 //LASS_LOG("parameter '" << names() << "' is set, value == '" << values_.back() << "'");
265 }
266 }
267
268 set();
269 return true;
270}
271
272
273
274}
275
276}
277
278#endif
279
280// EOF
the parser itself
Definition arg_parser.h:99
@ amOptional
if argument is used, it can take a value, but is not required
Definition arg_parser.h:72
@ amRequired
if argument is used, a value is required
Definition arg_parser.h:71
streams, binary streams, vrmlstreams, ...
Library for Assembled Shared Sources.
Definition config.h:53