arg_parser.inl
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #ifndef LASS_GUARDIAN_OF_INCLUSION_IO_ARG_PARSER_INL
00044 #define LASS_GUARDIAN_OF_INCLUSION_IO_ARG_PARSER_INL
00045
00046 #include "io_common.h"
00047 #include "arg_parser.h"
00048
00049 #include "../util/string_cast.h"
00050
00051 namespace lass
00052 {
00053 namespace io
00054 {
00055
00056
00057
00058 template <typename T>
00059 ArgValue<T>::ArgValue(ArgParser& iParser,
00060 const std::string& iShortName,
00061 const std::string& iLongName,
00062 const std::string& iDescription,
00063 int iArgMode):
00064 ArgParameter(iParser, iShortName, iLongName, iArgMode),
00065 description_(iDescription)
00066 {
00067 checkMode(iArgMode);
00068 }
00069
00070
00071
00072 template <typename T>
00073 ArgValue<T>::ArgValue(ArgParser& iParser,
00074 const std::string& iShortName,
00075 const std::string& iLongName,
00076 const std::string& iDescription,
00077 int iArgMode,
00078 TParam iDefault):
00079 ArgParameter(iParser, iShortName, iLongName, iArgMode),
00080 default_(1, iDefault),
00081 description_(iDescription)
00082 {
00083 checkMode(iArgMode);
00084 }
00085
00086
00087
00088 template <typename T>
00089 ArgValue<T>::ArgValue(ArgParser& iParser,
00090 const ArgFormat& iFormat):
00091 ArgParameter(iParser, iFormat.shortName, iFormat.longName, iFormat.argMode),
00092 description_(iFormat.description)
00093 {
00094 if (iFormat.hasDefault)
00095 {
00096 default_.push_back(util::stringCast<T>(iFormat.defaultValue));
00097 }
00098 checkMode(iFormat.argMode);
00099 }
00100
00101
00102
00103
00104
00105 template <typename T>
00106 const typename ArgValue<T>::TValues& ArgValue<T>::all() const
00107 {
00108 return !values_.empty() ? values_ : default_;
00109 }
00110
00111
00112
00113
00114
00115 template <typename T>
00116 size_t ArgValue<T>::size() const
00117 {
00118 return all().size();
00119 }
00120
00121
00122
00123
00124
00125 template <typename T>
00126 typename ArgValue<T>::TConstReference ArgValue<T>::operator[](size_t iIndex) const
00127 {
00128 LASS_ASSERT(iIndex < size());
00129 return all()[iIndex];
00130 }
00131
00132
00133
00134
00135
00136 template <typename T>
00137 typename ArgValue<T>::TConstReference ArgValue<T>::at(size_t iIndex) const
00138 {
00139 return all().at(iIndex);
00140 }
00141
00142
00143
00144
00145
00146 template <typename T>
00147 typename ArgValue<T>::TValueIterator ArgValue<T>::begin() const
00148 {
00149 return all().begin();
00150 }
00151
00152
00153
00154
00155
00156 template <typename T>
00157 typename ArgValue<T>::TValueIterator ArgValue<T>::end() const
00158 {
00159 return all().end();
00160 }
00161
00162
00163
00164
00165
00166
00167 template <typename T>
00168 void ArgValue<T>::checkMode(int iArgMode) const
00169 {
00170 if (iArgMode & amNoValue)
00171 {
00172 LASS_THROW("You can't set the mode 'amNoValue' for a ArgValue '" << names() << "'.");
00173 }
00174 const int mask = amOptional | amRequired;
00175 const int field = iArgMode & mask;
00176 if (field != amOptional && field != amRequired)
00177 {
00178 LASS_THROW("For a ArgValue '" << names() << "' you must choose between mode 'amOptional' "
00179 << "or 'amRequired'. At least one of these must be set, but not both.");
00180 }
00181 }
00182
00183
00184
00185 template <typename T>
00186 const std::string ArgValue<T>::doFormat() const
00187 {
00188 LASS_ASSERT(!(mode() & amNoValue));
00189
00190 std::ostringstream result;
00191 result << "[" << names() << " ";
00192
00193 if (mode() & amOptional)
00194 {
00195 result << "[";
00196 }
00197 result << "<" << (description_.empty() ? "value" : description_) << ">";
00198 if (mode() & amMultiple)
00199 {
00200 result << " ...";
00201 }
00202 if (mode() & amOptional)
00203 {
00204 result << "]";
00205 }
00206
00207 result << "]";
00208 return result.str();
00209 }
00210
00211
00212
00213 template <typename T>
00214 const bool ArgValue<T>::doSetValue(const std::string& iValue)
00215 {
00216 LASS_ASSERT(!(mode() & amNoValue));
00217
00218 if (iValue == "")
00219 {
00220 if (mode() & amRequired)
00221 {
00222 if (!parserIsQuiet())
00223 {
00224 LASS_THROW_EX(ArgBadArgument,
00225 "Bad program arguments: you didn't provide a value for the parameter '"
00226 << names() << "' as required.\n");
00227 }
00228 return false;
00229 }
00230
00231
00232 }
00233 else
00234 {
00235 if (values_.size() != 0 && !(mode() & amMultiple))
00236 {
00237 if (!parserIsQuiet())
00238 {
00239 LASS_THROW_EX(ArgBadArgument,
00240 "Bad program arguments: parameter '" << names() << "' already has a value '"
00241 << values_[0] << "' assigned, you can't assign another value '" << iValue
00242 << "' because this parameter cannot be multiple valued.\n");
00243 }
00244 return false;
00245 }
00246 else
00247 {
00248 try
00249 {
00250 values_.push_back(util::stringCast<T>(iValue));
00251 }
00252 catch (const util::BadStringCast&)
00253 {
00254 if (!parserIsQuiet())
00255 {
00256 LASS_THROW_EX(ArgBadArgument,
00257 "Bad program arguments: could not interpret '" << iValue
00258 << "' as a value of type '" << typeid(T).name()
00259 << "' for parameter '" << names() << ".\n");
00260 }
00261 return false;
00262 }
00263
00264
00265 }
00266 }
00267
00268 set();
00269 return true;
00270 }
00271
00272
00273
00274 }
00275
00276 }
00277
00278 #endif
00279
00280