18#ifndef __B2CMDLINE_H__
19#define __B2CMDLINE_H__
34#include "b2ppconfig.h"
151namespace b2000 {
namespace cmdline {
159 std::string name_, std::string short_desc_, std::string long_desc_,
160 bool multiple_ =
false)
162 short_desc(short_desc_),
163 long_desc(long_desc_),
165 multiple(multiple_) {}
169 bool is_set() {
return is_set_; }
173 virtual void parse_arg(
const std::string& arg) = 0;
174 std::string get_short_desc()
const;
176 std::string short_desc;
177 std::string long_desc;
184template <
typename T = std::
string>
187 CmdOpt(std::string name_, std::string short_desc_, std::string long_desc_, T default_ = T())
188 :
CmdOptBase(name_, short_desc_, long_desc_), value(default_) {}
189 operator T&() {
return value; }
192 void parse_arg(
const std::string& arg)
override {
193 std::istringstream i(arg);
196 Exception() <<
"Cannot convert the command-line argument " << arg
197 <<
" to the specific type of the option -" << name <<
"." <<
THROW;
202 Exception() <<
"Cannot convert the command-line argument " << arg
203 <<
" to the specific type of the option -" << name <<
"." <<
THROW;
213template <
typename T = std::
string>
216 CmdOptList(std::string name_, std::string short_desc_, std::string long_desc_)
217 :
CmdOptBase(name_, short_desc_, long_desc_,
true) {}
220 void parse_arg(
const std::string& arg)
override {
222 std::istringstream i(arg);
225 Exception() <<
"Cannot convert the command-line argument " << arg
226 <<
" to the specific type of the option -" << name <<
"." <<
THROW;
231 Exception() <<
"Cannot convert the command-line argument " << arg
232 <<
" to the specific type of the option -" << name <<
"." <<
THROW;
234 std::vector<T>::push_back(value);
244 CmdOptRTable(std::string name_, std::string short_desc_, std::string long_desc_)
245 :
CmdOptBase(name_, (short_desc_ ==
"" ?
"key=value" : short_desc_), long_desc_,
true) {}
248 void parse_arg(
const std::string& arg)
override {
249 int pos_equal = arg.find(
'=');
250 if (pos_equal == -1) {
251 Exception() <<
"Cannot convert the command-line argument " << arg
252 <<
" to the specific type of the option -" << name <<
"."
253 <<
"The = character is missing." <<
THROW;
255 std::string key = arg.substr(0, pos_equal);
256 std::string val = arg.substr(pos_equal + 1);
259 std::istringstream iss(val);
262 if (iss && iss.eof()) {
269 std::istringstream iss(val);
272 if (iss && iss.eof()) {
288 CmdOptDictionary(std::string name_, std::string short_desc_, std::string long_desc_)
289 :
CmdOptBase(name_, (short_desc_ ==
"" ?
"key=value" : short_desc_), long_desc_,
true),
292 bool has_key(
const std::string& key)
const override {
293 logger <<
logging::debug <<
"Try to use the key " << key <<
" of the command line option -"
294 << name <<
"." << logging::LOGFLUSH;
295 return list_key.find(key) != list_key.end();
298 bool get_bool(
const std::string& key)
const override {
299 list_key_t::const_iterator i = list_key.find(key);
301 const_cast<bool&
>(i->second.second) =
true;
302 logger <<
logging::debug <<
"Using the key " << key <<
" of the command-line option -"
303 << name <<
" has boolean." << logging::LOGFLUSH;
304 const std::string& s = i->second.first;
305 if (s ==
"1" || s ==
"yes" || s ==
"YES" || s ==
"true" || s ==
"TRUE") {
307 }
else if (s ==
"0" || s ==
"no" || s ==
"NO" || s ==
"false" || s ==
"FALSE") {
310 Exception() <<
"Cannot convert the string " << i->second.first <<
" of key " << key
311 <<
" to a value of type bool." <<
THROW;
315 int get_int(
const std::string& key)
const override {
316 list_key_t::const_iterator i = list_key.find(key);
318 const_cast<bool&
>(i->second.second) =
true;
319 std::istringstream o(i->second.first);
320 std::istream_iterator<int> oo(o);
321 std::vector<int> v(oo, std::istream_iterator<int>());
322 if (v.size() != 1 || !o.eof()) {
323 Exception() <<
"Cannot convert the string " << i->second.first <<
" of key " << key
324 <<
" to an integer value." <<
THROW;
326 logger <<
logging::debug <<
"Using the key " << key <<
" of the command line option -"
327 << name <<
" has integer." << logging::LOGFLUSH;
332 list_key_t::const_iterator i = list_key.find(key);
334 const_cast<bool&
>(i->second.second) =
true;
335 std::istringstream o(i->second.first);
336 std::istream_iterator<double> oo(o);
337 std::vector<double> v(oo, std::istream_iterator<double>());
338 if (v.size() != 1 || !o.eof()) {
339 Exception() <<
"Cannot convert the string " << i->second.first <<
" of key " << key
340 <<
" to a floating point value." <<
THROW;
342 logger <<
logging::debug <<
"Using the key " << key <<
" of the command line option -"
343 << name <<
" has floating point value." << logging::LOGFLUSH;
348 list_key_t::const_iterator i = list_key.find(key);
350 const_cast<bool&
>(i->second.second) =
true;
351 std::istringstream o(i->second.first);
352 std::istream_iterator<b2000::csda<double> > oo(o);
353 std::vector<b2000::csda<double> > v(oo, std::istream_iterator<b2000::csda<double> >());
354 if (v.size() != 1 || !o.eof()) {
355 Exception() <<
"Cannot convert the string " << i->second.first <<
" of key " << key
356 <<
" to a csda value." <<
THROW;
358 logger <<
logging::debug <<
"Using the key " << key <<
" of the command line option -"
359 << name <<
" has csda value." << logging::LOGFLUSH;
364 list_key_t::const_iterator i = list_key.find(key);
366 const_cast<bool&
>(i->second.second) =
true;
367 std::istringstream o(i->second.first);
368 std::istream_iterator<std::complex<double> > oo(o);
369 std::vector<std::complex<double> > v(oo, std::istream_iterator<std::complex<double> >());
370 if (v.size() != 1 || !o.eof()) {
371 Exception() <<
"Cannot convert the string " << i->second.first <<
" of key " << key
372 <<
" to a complex value." <<
THROW;
374 logger <<
logging::debug <<
"Using the key " << key <<
" of the command line option -"
375 << name <<
" has complex value." << logging::LOGFLUSH;
379 std::string
get_string(
const std::string& key)
const override {
380 list_key_t::const_iterator i = list_key.find(key);
382 const_cast<bool&
>(i->second.second) =
true;
383 logger <<
logging::debug <<
"Using the key " << key <<
" of the command line option -"
384 << name <<
" has string." << logging::LOGFLUSH;
385 return i->second.first;
388 void get(
const std::string& key, std::vector<int>& v)
const override {
389 list_key_t::const_iterator i = list_key.find(key);
391 const_cast<bool&
>(i->second.second) =
true;
392 std::istringstream o(i->second.first);
393 std::istream_iterator<int> oo(o);
394 std::vector<int> vv(oo, std::istream_iterator<int>());
396 Exception() <<
"Cannot convert the string " << i->second.first <<
" of key " << key
397 <<
" to an array of integers." <<
THROW;
399 logger <<
logging::debug <<
"Using the key " << key <<
" of the command line option -"
400 << name <<
" has array of integers." << logging::LOGFLUSH;
404 void get(
const std::string& key, std::vector<double>& v)
const override {
405 list_key_t::const_iterator i = list_key.find(key);
407 const_cast<bool&
>(i->second.second) =
true;
408 std::istringstream o(i->second.first);
409 std::istream_iterator<double> oo(o);
410 std::vector<double> vv(oo, std::istream_iterator<double>());
412 Exception() <<
"Cannot convert the string " << i->second.first <<
" of key " << key
413 <<
" to an array of floating point values." <<
THROW;
415 logger <<
logging::debug <<
"Using the key " << key <<
" of the command line option -"
416 << name <<
" has array of double." << logging::LOGFLUSH;
420 void get(
const std::string& key, std::vector<b2000::csda<double> >& v)
const override {
421 list_key_t::const_iterator i = list_key.find(key);
423 const_cast<bool&
>(i->second.second) =
true;
424 std::istringstream o(i->second.first);
425 std::istream_iterator<b2000::csda<double> > oo(o);
426 std::vector<b2000::csda<double> > vv(oo, std::istream_iterator<b2000::csda<double> >());
428 Exception() <<
"Cannot convert the string " << i->second.first <<
" of key " << key
429 <<
" to an array of csda values." <<
THROW;
431 logger <<
logging::debug <<
"Using the key " << key <<
" of the command line option -"
432 << name <<
" has array of csda values." << logging::LOGFLUSH;
436 void get(
const std::string& key, std::vector<std::complex<double> >& v)
const override {
437 list_key_t::const_iterator i = list_key.find(key);
439 const_cast<bool&
>(i->second.second) =
true;
440 std::istringstream o(i->second.first);
441 std::istream_iterator<std::complex<double> > oo(o);
442 std::vector<std::complex<double> > vv(oo, std::istream_iterator<std::complex<double> >());
444 Exception() <<
"Cannot convert the string " << i->second.first <<
" of key " << key
445 <<
" to an array of complex values." <<
THROW;
447 logger <<
logging::debug <<
"Using the key " << key <<
" of the command line option -"
448 << name <<
" has array of complex doubles." << logging::LOGFLUSH;
454 for (list_key_t::const_iterator i = list_key.begin(); i != list_key.end(); ++i) {
455 if (!i->second.second) {
456 if (!res.empty()) { res +=
", "; }
462 <<
"' of command line option ignored, because unknown" << logging::LOGFLUSH;
466 std::string has_string()
const override {
467 std::string res =
"{";
468 for (list_key_t::const_iterator i = list_key.begin(); i != list_key.end(); ++i) {
472 res += i->second.first;
480 void parse_arg(
const std::string& arg)
override {
481 int pos_equal = arg.find(
'=');
482 if (pos_equal == -1) {
483 Exception() <<
"Cannot convert the command-line argument " << arg
484 <<
" to the specific type of the option -" << name <<
"."
485 <<
"The = character is missing." <<
THROW;
487 std::string key = arg.substr(0, pos_equal);
488 std::string val = arg.substr(pos_equal + 1);
491 for (std::string::iterator i = key.begin(); i != key.end(); ++i) {
492 (*i) = std::toupper(*i);
494 for (std::string::iterator i = val.begin(); i != val.end(); ++i) {
495 (*i) = std::toupper(*i);
499 Exception() <<
"The key " << key
500 <<
" of the command-line "
502 << arg <<
" of the option -" << name <<
" already exists." <<
THROW;
504 list_key[key] = std::pair<std::string, bool>(val,
false);
508 typedef std::map<std::string, std::pair<std::string, bool> > list_key_t;
510 logging::Logger& logger;
516 CmdArgBase(std::string short_desc_, std::string long_desc_)
517 : short_desc(short_desc_), long_desc(long_desc_) {}
522 virtual void parse_arg(
const std::string& arg) = 0;
523 std::string short_desc;
524 std::string long_desc;
529template <
typename T = std::
string>
532 CmdArg(std::string short_desc_, std::string long_desc_) :
CmdArgBase(short_desc_, long_desc_) {}
534 operator T&() {
return value; }
538 void parse_arg(
const std::string& arg)
override {
539 std::istringstream i(arg);
542 Exception() <<
"Cannot convert the command-line argument " << arg
543 <<
" to the specific type of the argument " << short_desc <<
"." <<
THROW;
548 Exception() <<
"Cannot convert the command-line argument " << arg
549 <<
" to the specific type of the argument " << short_desc <<
"." <<
THROW;
559 : short_desc(short_desc_), long_desc(long_desc_) {}
564 virtual void parse_arg(
const std::string& arg) = 0;
565 std::string short_desc;
566 std::string long_desc;
571template <
typename T = std::
string>
574 CmdArgList(std::string short_desc_, std::string long_desc_)
578 void parse_arg(
const std::string& arg)
override {
580 std::istringstream i(arg);
583 Exception() <<
"Cannot convert the command-line argument " << arg
584 <<
" to the specific type of the argument " << short_desc <<
"." <<
THROW;
589 Exception() <<
"Cannot convert the command-line argument " << arg
590 <<
" to the specific type of the argument " << short_desc <<
"." <<
THROW;
592 std::vector<T>::push_back(value);
603inline void CmdOptList<std::string>::parse_arg(
const std::string& arg) {
604 std::vector<std::string>::push_back(arg);
609inline void CmdArg<std::string>::parse_arg(
const std::string& arg) {
614inline void CmdArgList<std::string>::parse_arg(
const std::string& arg) {
615 std::vector<std::string>::push_back(arg);
619inline void CmdOpt<bool>::parse_arg(
const std::string& arg) {
625inline void CmdOptList<bool>::parse_arg(
const std::string& arg) {
626 std::vector<bool>::push_back(
true);
631inline void CmdArg<bool>::parse_arg(
const std::string& arg) {
636inline void CmdArgList<bool>::parse_arg(
const std::string& arg) {
637 std::vector<bool>::push_back(
true);
652 : argc(argc_), argv(argv_), opt_parsed(false), arg_list(nullptr) {}
656 opts.push_back(&opt);
657 if (!name_opts.insert(name_opts_t::value_type(opt.name, &opt)).second) {
658 Exception() <<
"The command-line option -" << opt.name <<
" is already defined."
666 args.push_back(&opt);
672 if (arg_list !=
nullptr) {
673 Exception() <<
"Only one CmdArgList can by inserted in a CmdLine." <<
THROW;
681 void parse(
bool only_opt =
false);
696 static void format_print(std::ostream& out,
const std::string& a,
const std::string& b);
700 std::vector<CmdOptBase*> opts;
701 typedef std::map<std::string, CmdOptBase*> name_opts_t;
702 name_opts_t name_opts;
703 std::vector<CmdArgBase*> args;
#define THROW
Definition b2exception.H:198
Definition b2dictionary.H:48
Definition b2exception.H:131
Definition b2rtable.H:427
void set(const std::string &key, const T &v, const bool new_key=false)
Assign a single value. If new_key is true, the key must not exist.
Definition b2rtable.H:463
Definition b2cmdline.H:514
Definition b2cmdline.H:556
Definition b2cmdline.H:572
Definition b2cmdline.H:530
Definition b2cmdline.H:641
CmdLine(int argc_, char *argv_[])
Definition b2cmdline.H:651
CmdLine & operator<<(CmdOptBase &opt)
Definition b2cmdline.H:655
void print_short_desc(std::ostream &out) const
Definition b2cmdline.C:99
void parse(bool only_opt=false)
Definition b2cmdline.C:23
std::string get_command_name() const
Definition b2cmdline.C:147
CmdLine & operator<<(CmdArgBase &opt)
Definition b2cmdline.H:665
CmdLine & operator<<(CmdArgListBase &opt)
Definition b2cmdline.H:671
void print_long_desc(std::ostream &out) const
Definition b2cmdline.C:106
std::string get_command_line() const
Definition b2cmdline.C:149
Definition b2cmdline.H:156
Definition b2cmdline.H:286
b2000::csda< double > get_csda_double(const std::string &key) const override
Definition b2cmdline.H:347
double get_double(const std::string &key) const override
Definition b2cmdline.H:331
std::complex< double > get_complex_double(const std::string &key) const override
Definition b2cmdline.H:363
void get(const std::string &key, std::vector< std::complex< double > > &v) const override
Definition b2cmdline.H:436
int get_int(const std::string &key) const override
Definition b2cmdline.H:315
void get(const std::string &key, std::vector< int > &v) const override
Definition b2cmdline.H:388
std::string get_string(const std::string &key) const override
Definition b2cmdline.H:379
bool has_key(const std::string &key) const override
Definition b2cmdline.H:292
void warn_on_non_used_key() const override
Definition b2cmdline.H:452
bool get_bool(const std::string &key) const override
Definition b2cmdline.H:298
void get(const std::string &key, std::vector< b2000::csda< double > > &v) const override
Definition b2cmdline.H:420
void get(const std::string &key, std::vector< double > &v) const override
Definition b2cmdline.H:404
Definition b2cmdline.H:214
Definition b2cmdline.H:242
Definition b2cmdline.H:185
Logger & get_logger(const std::string &logger_name="")
Definition b2logging.H:829
Contains the base classes for implementing Finite Elements.
Definition b2boundary_condition.H:32
GenericException< KeyError_name > KeyError
Definition b2exception.H:320