1
0
mirror of https://github.com/pdf2htmlEX/pdf2htmlEX.git synced 2024-07-05 01:28:39 +00:00

clean ArgParser

This commit is contained in:
Lu Wang 2013-01-30 03:22:11 +08:00
parent b2ae01a2fa
commit a7bba1bf48
2 changed files with 16 additions and 11 deletions

View File

@ -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;
}
}
}

View File

@ -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 "<long name>[,<short char>]", 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 <class T, class Tv>
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<class T, class Tv>
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<T, Tv>("", location, default_value, callback, "", dont_show_default));
}
else
arg_entries.push_back(new ArgEntry<T, Tv>(optname, location, default_value, callback, description, dont_show_default));
{
arg_entries.push_back(new ArgEntry<T, Tv>(optname, location, default_value, callback, (description ? description : ""), dont_show_default));
}
return *this;
}
@ -152,7 +157,7 @@ void ArgParser::ArgEntry<T, Tv>::parse(const char * arg) const
template<class T, class Tv>
void ArgParser::ArgEntry<T, Tv>::show_usage(std::ostream & out) const
{
if(description == "")
if(description.empty())
return;
std::ostringstream sout;