From a7bba1bf486b812270a4ea8c45d79ff1984bb52d Mon Sep 17 00:00:00 2001 From: Lu Wang Date: Wed, 30 Jan 2013 03:22:11 +0800 Subject: [PATCH] clean ArgParser --- src/util/ArgParser.cc | 2 +- src/util/ArgParser.h | 25 +++++++++++++++---------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/util/ArgParser.cc b/src/util/ArgParser.cc index 433b579..9b66a20 100644 --- a/src/util/ArgParser.cc +++ b/src/util/ArgParser.cc @@ -163,7 +163,7 @@ ArgParser::ArgEntryBase::ArgEntryBase(const char * name, const char * descriptio } else { - cerr << "Warning: argument '" << this->name << "' may not be parsed correctly" << endl; + cerr << "Warning: argument '" << this->name << "' cannnot be parsed as a short option" << endl; } } } diff --git a/src/util/ArgParser.h b/src/util/ArgParser.h index d5aafde..a6c58c4 100644 --- a/src/util/ArgParser.h +++ b/src/util/ArgParser.h @@ -47,15 +47,14 @@ class ArgParser typedef void (*ArgParserCallBack) (const char * arg); /* - * optname: name of the argment, should be provided as --optname - * description: if description is "", the argument won't be shown in show_usage() + * The 1st is for arg without arguments (i.e. flags), and the 2nd is for general args. + * optname: + * - if not nullptr, it should be the name of the arg, should be in the format of "[,]", e.g. "help,h" + * - if nullptr, it denotes an optional arg, and description will be ignored + * description: + * - if description is nullptr or "", the argument won't be shown in show_usage() */ - ArgParser & add(const char * optname, const char * description, ArgParserCallBack callback = nullptr); - - /* - * location == nullptr means no argument is needed - */ template ArgParser & add(const char * optname, T * location, const Tv & default_value, const char * description, ArgParserCallBack callback = nullptr, bool dont_show_default = false); @@ -70,6 +69,7 @@ class ArgParser class ArgEntryBase { public: + /* name or description cannot be nullptr */ ArgEntryBase(const char * name, const char * description, bool need_arg); virtual ~ArgEntryBase() { } char shortname; @@ -107,11 +107,16 @@ class ArgParser template ArgParser & ArgParser::add(const char * optname, T * location, const Tv & default_value, const char * description, ArgParserCallBack callback, bool dont_show_default) { - // use "" in case nullptr is provided + // ArgEntry does not accept nullptr as optname nor description if((!optname) || (!optname[0])) + { + // when optname is nullptr or "", it's optional, and description is dropped optional_arg_entries.push_back(new ArgEntry("", location, default_value, callback, "", dont_show_default)); + } else - arg_entries.push_back(new ArgEntry(optname, location, default_value, callback, description, dont_show_default)); + { + arg_entries.push_back(new ArgEntry(optname, location, default_value, callback, (description ? description : ""), dont_show_default)); + } return *this; } @@ -152,7 +157,7 @@ void ArgParser::ArgEntry::parse(const char * arg) const template void ArgParser::ArgEntry::show_usage(std::ostream & out) const { - if(description == "") + if(description.empty()) return; std::ostringstream sout;