21 #ifndef mia_core_cmdlineparser_hh
22 #define mia_core_cmdlineparser_hh
103 template <
typename T>
117 const char *short_help,
bool flags =
false);
120 virtual void do_get_long_help_xml(std::ostream& os, xmlpp::Element& parent,
HandlerHelpMap& handler_map)
const;
121 virtual bool do_set_value(
const char *str_value);
122 virtual size_t do_get_needed_args()
const;
123 virtual void do_write_value(std::ostream& os)
const;
124 virtual const std::string do_get_value_as_string()
const;
147 const char *
long_help,
const char *short_help,
bool flags =
false);
149 virtual bool do_set_value(
const char *str_value);
150 virtual size_t do_get_needed_args()
const;
151 virtual void do_write_value(std::ostream& os)
const;
152 virtual void do_get_long_help(std::ostream& os)
const;
153 virtual const std::string do_get_value_as_string()
const;
181 virtual void print(std::ostream& os)
const = 0;
195 void print(std::ostream& os)
const;
198 std::unique_ptr<Callback> m_callback;
199 virtual bool do_set_value(
const char *str_value);
200 virtual size_t do_get_needed_args()
const;
201 virtual void do_write_value(std::ostream& os)
const;
203 virtual void do_get_long_help_xml(std::ostream& os, xmlpp::Element& parent,
213 template <
typename PluginHandler>
215 void print(std::ostream& os)
const{
216 PluginHandler::instance().print_help(os);
264 void add(
const std::string& group,
PCmdOption opt);
276 EHelpRequested parse(
size_t argc,
char *args[],
const std::string& additional_type,
278 __attribute__((warn_unused_result));
291 EHelpRequested parse(
size_t argc, const
char *args[], const std::
string& additional_type,
293 __attribute__((warn_unused_result));
300 EHelpRequested parse(
size_t argc,
char *args[]) __attribute__((warn_unused_result));
307 EHelpRequested parse(
size_t argc, const
char *args[]) __attribute__((warn_unused_result));
310 const std::vector<std::
string>& get_remaining() const;
321 void set_group(const std::
string& group);
327 void set_logstream(std::ostream& os);
329 EHelpRequested do_parse(
size_t argc, const
char *args[],
bool has_additional,
331 __attribute__((warn_unused_result));
332 int handle_shortargs(const
char *arg,
size_t argc, const
char *args[]);
333 struct CCmdOptionListData *m_impl;
351 template <typename T>
352 struct __dispatch_opt {
356 static void init(T& ){
364 static bool apply(
const char *svalue, T& value) {
365 std::istringstream sval(svalue);
368 while (isspace(sval.peek())) {
375 static size_t size(
const T ) {
384 static void apply(std::ostream& os,
const T& value,
bool ) {
385 os <<
"=" << value <<
" ";
393 static const std::string get_as_string(
const T& value) {
394 std::ostringstream os;
405 template <
typename T>
406 struct __dispatch_opt< std::vector<T> > {
407 static void init(std::vector<T>& ){
410 static bool apply(
const char *svalue, std::vector<T>& value) {
411 std::string h(svalue);
413 for(std::string::iterator hb = h.begin(); hb != h.end(); ++hb)
420 if (!value.empty()) {
421 if (n > value.size()) {
422 throw create_exception<std::invalid_argument>(
"Expect only ", value.size(),
423 " coma separated values, but '",
424 svalue,
"' provides ", n);
430 std::istringstream sval(h);
431 auto i = value.begin();
432 while (!sval.eof()) {
439 static size_t size(
const std::vector<T>& ) {
443 static void apply(std::ostream& os,
const std::vector<T>& value,
bool required) {
446 if (value.empty() && required)
449 for (
auto i = value.begin(); i != value.end(); ++i) {
450 if (i != value.begin())
458 static const std::string get_as_string(
const std::vector<T>& value) {
459 std::ostringstream os;
460 for (
auto i = value.begin(); i != value.end(); ++i) {
461 if (i != value.begin())
478 struct __dispatch_opt<bool> {
479 static void init(
bool& value) {
482 static bool apply(
const char *,
bool& value) {
486 static size_t size(
bool ) {
489 static void apply(std::ostream& ,
bool ,
bool ) {
491 static const std::string get_as_string(
const bool& value) {
492 return value ?
"true" :
"false";
506 struct __dispatch_opt<std::string> {
507 static void init(std::string& ) {
509 static bool apply(
const char *svalue, std::string& value) {
510 value = std::string(svalue);
513 static size_t size(std::string ) {
516 static void apply(std::ostream& os,
const std::string& value,
bool required) {
525 static const std::string get_as_string(
const std::string& value) {
535 template <
typename T>
537 const char *long_help,
const char *short_help,
bool flags):
538 CCmdOption(short_opt, long_opt, long_help, short_help, flags),
541 __dispatch_opt<T>::init(m_value);
544 template <
typename T>
547 return __dispatch_opt<T>::apply(svalue, m_value);
550 template <
typename T>
553 return __dispatch_opt<T>::size(m_value);
556 template <
typename T>
559 __dispatch_opt<T>::apply( os, m_value, is_required());
562 template <
typename T>
566 do_get_long_help(os);
567 parent.set_attribute(
"type", __type_descr<T>::value);
570 template <
typename T>
573 return __dispatch_opt<T>::get_as_string(m_value);
592 template <
typename T>
594 const char *help,
bool flags =
false)
616 template <
typename T,
typename Tmin,
typename Tmax>
618 const char *help,
bool flags =
false)
641 template <
typename T>
643 const char *help,
bool required =
false)
646 long_opt, required ));
662 PCmdOption make_opt(
bool& value,
const char *long_opt,
char short_opt,
const char *help);
679 template <
typename T>
681 const char *long_opt,
char short_opt,
const char *help)
705 char short_opt,
const char *long_help,
706 const char *short_help,
725 PCmdOption make_opt(std::string& value,
const char *long_opt,
char short_opt,
const char *long_help,
745 template <
typename T>
747 const char *long_opt,
char short_opt,
749 bool required =
false)
775 template <
typename T>
776 PCmdOption make_opt(
typename std::shared_ptr<T>& value,
const char *default_value,
const char *long_opt,
777 char short_opt,
const char *help,
bool required =
false)
779 typedef typename FactoryTrait<T>::type F;
802 template <
typename T>
803 PCmdOption make_opt(
typename std::unique_ptr<T>& value,
const char *default_value,
const char *long_opt,
804 char short_opt,
const char *help,
bool required =
false)
806 typedef typename FactoryTrait<T>::type F;