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
00048 #ifndef STLSOFT_INCL_STLSOFT_SYSTEM_HPP_CMDARGS
00049 #define STLSOFT_INCL_STLSOFT_SYSTEM_HPP_CMDARGS
00050
00051 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
00052 # define STLSOFT_VER_STLSOFT_SYSTEM_HPP_CMDARGS_MAJOR 3
00053 # define STLSOFT_VER_STLSOFT_SYSTEM_HPP_CMDARGS_MINOR 0
00054 # define STLSOFT_VER_STLSOFT_SYSTEM_HPP_CMDARGS_REVISION 3
00055 # define STLSOFT_VER_STLSOFT_SYSTEM_HPP_CMDARGS_EDIT 30
00056 #endif
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072 #ifndef STLSOFT_INCL_STLSOFT_H_STLSOFT
00073 # include <stlsoft/stlsoft.h>
00074 #endif
00075 #ifdef STLSOFT_SYSTEM_CMDARGS_USE_STD_STRING
00076 # include <string>
00077 #else
00078 # ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_SIMPLE_STRING
00079 # include <stlsoft/string/simple_string.hpp>
00080 # endif
00081 #endif
00082 #ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_SPLIT_FUNCTIONS
00083 # include <stlsoft/string/split_functions.hpp>
00084 #endif
00085 #ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_STRING_VIEW
00086 # include <stlsoft/string/string_view.hpp>
00087 #endif
00088
00089 #ifndef STLSOFT_INCL_UTILITY
00090 # define STLSOFT_INCL_UTILITY
00091 # include <utility>
00092 #endif
00093 #ifndef STLSOFT_INCL_VECTOR
00094 # define STLSOFT_INCL_VECTOR
00095 # include <vector>
00096 #endif
00097
00098
00099
00100
00101
00102 #ifndef _STLSOFT_NO_NAMESPACE
00103 namespace stlsoft
00104 {
00105 #endif
00106
00107
00108
00109
00110
00123 class cmdargs
00124 {
00125 public:
00126 #ifdef STLSOFT_SYSTEM_CMDARGS_USE_STD_STRING
00127 typedef std::string string_type;
00128 #else
00129 typedef simple_string string_type;
00130 #endif
00131 typedef basic_string_view<char> string_view_type;
00132
00133 enum
00134 {
00135 singleDash = 1
00136 , doubleDash = 2
00137 , tripleDash = 3
00138 };
00139
00145 struct option
00146 {
00147 string_type name;
00148 string_type value;
00149 int type;
00150 int index;
00151 string_view_type original;
00152
00153 option()
00154 : name()
00155 , value()
00156 , type(-1)
00157 , index(-1)
00158 , original()
00159 {}
00160 option(string_type const& n, string_type const& v, int t, int i, string_view_type const& o)
00161 : name(n)
00162 , value(v)
00163 , type(t)
00164 , index(i)
00165 , original(o)
00166 {}
00167 };
00168
00174 struct value
00175 {
00176 string_view_type name;
00177 int index;
00178
00179 value()
00180 : name()
00181 , index(-1)
00182 {}
00183 value(string_view_type const& v, int i)
00184 : name(v)
00185 , index(i)
00186 {}
00187 };
00188
00189 typedef option options_value_type;
00190 typedef value values_value_type;
00191
00192 typedef stlsoft_ns_qual_std(vector)<options_value_type> options_type;
00193 typedef stlsoft_ns_qual_std(vector)<values_value_type> values_type;
00194
00195 typedef options_type::const_iterator const_options_iterator;
00196 typedef values_type::const_iterator const_values_iterator;
00197
00198 typedef ss_size_t size_type;
00199 typedef ss_bool_t bool_type;
00200
00203 public:
00205 cmdargs(int argc, char ** argv);
00207 ~cmdargs() stlsoft_throw_0();
00209
00212 public:
00214 options_type const& options() const;
00216 values_type const& values() const;
00217
00218 const_options_iterator options_begin() const;
00219 const_options_iterator options_end() const;
00220
00221 const_values_iterator values_begin() const;
00222 const_values_iterator values_end() const;
00223
00225 size_type options_size() const;
00227 size_type values_size() const;
00229 size_type size() const;
00231 bool_type empty() const;
00232
00239 template <ss_typename_param_k S>
00240 bool_type has_option(S const& optionName, int type = -1) const
00241 {
00242 return m_options.end() != has_option_(c_str_ptr(optionName));
00243 }
00252 template <ss_typename_param_k S>
00253 bool_type has_option(S const& optionName, int type, option& opt) const
00254 {
00255 options_type::const_iterator it = has_option_(c_str_ptr(optionName), type);
00256
00257 if(m_options.end() != it)
00258 {
00259 opt = *it;
00260
00261 return true;
00262 }
00263
00264 return false;
00265 }
00266
00267 template <ss_typename_param_k S>
00268 bool_type has_value(S const& valueName) const
00269 {
00270 return m_values.end() != has_value_(c_str_ptr(valueName));
00271 }
00272 template <ss_typename_param_k S>
00273 bool_type has_value(S const& valueName, value& val) const
00274 {
00275 values_type::const_iterator it = has_value_(c_str_ptr(valueName));
00276
00277 if(m_values.end() != it)
00278 {
00279 val = *it;
00280
00281 return true;
00282 }
00283
00284 return false;
00285 }
00287
00290 private:
00291 values_type::const_iterator has_value_(char const* valueName) const;
00292 options_type::const_iterator has_option_(char const* optionName, int type) const;
00294
00297 private:
00298 options_type m_options;
00299 values_type m_values;
00301 };
00302
00304
00305
00306 #ifdef STLSOFT_UNITTEST
00307 # include "./unittest/cmdargs_unittest_.h"
00308 #endif
00309
00311
00312
00313 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
00314
00315 inline cmdargs::cmdargs(int argc, char **argv)
00316 {
00317 for(int i = 1; i < argc; ++i)
00318 {
00319 char const *arg = argv[i];
00320
00321 if('-' == arg[0])
00322 {
00323 ++arg;
00324
00325 const int type = ('-' != arg[0]) ? singleDash : (++arg, doubleDash);
00326 string_type s0;
00327 string_type s1;
00328
00329 split(arg, '=', s0, s1);
00330
00331 m_options.push_back(option(s0, s1, type, i, argv[i]));
00332 }
00333 else
00334 {
00335 m_values.push_back(value(arg, i));
00336 }
00337 }
00338 }
00339
00340 inline cmdargs::~cmdargs() stlsoft_throw_0()
00341 {
00342 }
00343
00344 inline cmdargs::options_type const& cmdargs::options() const
00345 {
00346 return m_options;
00347 }
00348
00349 inline cmdargs::values_type const& cmdargs::values() const
00350 {
00351 return m_values;
00352 }
00353
00354 inline cmdargs::const_options_iterator cmdargs::options_begin() const
00355 {
00356 return m_options.begin();
00357 }
00358
00359 inline cmdargs::const_options_iterator cmdargs::options_end() const
00360 {
00361 return m_options.end();
00362 }
00363
00364 inline cmdargs::const_values_iterator cmdargs::values_begin() const
00365 {
00366 return m_values.begin();
00367 }
00368
00369 inline cmdargs::const_values_iterator cmdargs::values_end() const
00370 {
00371 return m_values.end();
00372 }
00373
00374 inline cmdargs::size_type cmdargs::options_size() const
00375 {
00376 return m_options.size();
00377 }
00378
00379 inline cmdargs::size_type cmdargs::values_size() const
00380 {
00381 return m_values.size();
00382 }
00383
00384 inline cmdargs::size_type cmdargs::size() const
00385 {
00386 return options_size() + values_size();
00387 }
00388
00389 inline cmdargs::bool_type cmdargs::empty() const
00390 {
00391 return 0 == size();
00392 }
00393
00394 inline cmdargs::values_type::const_iterator cmdargs::has_value_(char const* valueName) const
00395 {
00396 STLSOFT_ASSERT(NULL != valueName);
00397
00398 values_type::const_iterator b;
00399
00400 for(b = m_values.begin(); b != m_values.end(); ++b)
00401 {
00402 if((*b).name == valueName)
00403 {
00404 break;
00405 }
00406 }
00407
00408 return b;
00409 }
00410
00411 inline cmdargs::options_type::const_iterator cmdargs::has_option_(char const* optionName, int type) const
00412 {
00413 STLSOFT_ASSERT(NULL != optionName);
00414
00415 options_type::const_iterator b;
00416
00417 for(b = m_options.begin(); b != m_options.end(); ++b)
00418 {
00419 if( (*b).name == optionName &&
00420 ( -1 == type ||
00421 type == (*b).type))
00422 {
00423 break;
00424 }
00425 }
00426
00427 return b;
00428 }
00429
00430 #endif
00431
00432 template <ss_typename_param_k S>
00433 inline S& operator <<(S &stm, cmdargs::option const& option)
00434 {
00435 static const char s_dashes[] =
00436 {
00437 '-'
00438 , '-'
00439 , '\0'
00440 };
00441 char const *dashes = &s_dashes[(option.type == cmdargs::singleDash)];
00442
00443 if(option.value.empty())
00444 {
00445 stm << dashes << option.name;
00446 }
00447 else
00448 {
00449 stm << dashes << option.name << '=' << option.value;
00450 }
00451
00452 return stm;
00453 }
00454
00455 template <ss_typename_param_k S>
00456 inline S& operator <<(S &stm, cmdargs::value const& value)
00457 {
00458 stm << value.name;
00459
00460 return stm;
00461 }
00462
00463
00464
00465 #ifndef _STLSOFT_NO_NAMESPACE
00466 }
00467 #endif
00468
00469
00470
00471 #endif
00472
00473