#include <msp/strings/format.h>
+#include "algorithm.h"
#include "getopt.h"
using namespace std;
namespace Msp {
-GetOpt::GetOpt():
- help(false)
+GetOpt::GetOpt()
{
add_option("help", help, NO_ARG).set_help("Displays this help");
}
GetOpt::~GetOpt()
{
- for(OptionList::iterator i=opts.begin(); i!=opts.end(); ++i)
- delete *i;
- for(ArgumentList::iterator i=args.begin(); i!=args.end(); ++i)
- delete *i;
+ for(OptionImpl *i: opts)
+ delete i;
+ for(ArgumentImpl *i: args)
+ delete i;
}
GetOpt::OptionImpl &GetOpt::add_option(char s, const string &l, const Store &t, ArgType a)
if(t.is_list() && a!=REQUIRED_ARG)
throw invalid_argument("GetOpt::add_option");
- for(OptionList::iterator i=opts.begin(); i!=opts.end(); )
+ for(auto i=opts.begin(); i!=opts.end(); )
{
if((s!=0 && (*i)->get_short()==s) || (*i)->get_long()==l)
{
delete *i;
- opts.erase(i++);
+ i = opts.erase(i);
}
else
++i;
return *opts.back();
}
-GetOpt::ArgumentImpl &GetOpt::add_argument(const string &n, const Store &t, ArgType a)
+GetOpt::ArgumentImpl &GetOpt::add_argument(const string &n, const Store &t, ArgType y)
{
- if(a==NO_ARG)
+ if(y==NO_ARG)
throw invalid_argument("GetOpt::add_argument");
bool have_list = false;
bool have_optional = false;
- for(ArgumentList::const_iterator i=args.begin(); i!=args.end(); ++i)
+ for(const ArgumentImpl *a: args)
{
- if((*i)->is_list_store())
+ if(a->is_list_store())
have_list = true;
- else if((*i)->get_type()==OPTIONAL_ARG)
+ else if(a->get_type()==OPTIONAL_ARG)
have_optional = true;
}
- if(have_optional && (t.is_list() || a!=OPTIONAL_ARG))
+ if(have_optional && (t.is_list() || y!=OPTIONAL_ARG))
throw invalid_argument("GetOpt::add_argument");
- if(have_list && (t.is_list() || a==OPTIONAL_ARG))
+ if(have_list && (t.is_list() || y==OPTIONAL_ARG))
throw invalid_argument("GetOpt::add_argument");
- args.push_back(new ArgumentImpl(n, t, a));
+ args.push_back(new ArgumentImpl(n, t, y));
return *args.back();
}
GetOpt::OptionImpl &GetOpt::get_option(char s)
{
- for(OptionList::iterator i=opts.begin(); i!=opts.end(); ++i)
- if((*i)->get_short()==s)
- return **i;
+ auto i = find_if(opts, [s](const OptionImpl *o){ return o->get_short()==s; });
+ if(i!=opts.end())
+ return **i;
throw usage_error(string("Unknown option -")+s);
}
GetOpt::OptionImpl &GetOpt::get_option(const string &l)
{
- for(OptionList::iterator i=opts.begin(); i!=opts.end(); ++i)
- if((*i)->get_long()==l)
- return **i;
+ auto i = find_if(opts, [&l](const OptionImpl *o){ return o->get_long()==l; });
+ if(i!=opts.end())
+ return **i;
throw usage_error(string("Unknown option --")+l);
}
else
args_raw.push_back(argv[i++]);
}
-
+
for(; i<argc; ++i)
args_raw.push_back(argv[i]);
i = 0;
- for(ArgumentList::const_iterator j=args.begin(); j!=args.end(); ++j)
+ for(auto j=args.begin(); j!=args.end(); ++j)
{
if((*j)->is_list_store())
{
unsigned end = args_raw.size();
- for(ArgumentList::const_iterator k=j; ++k!=args.end(); )
+ for(auto k=j; ++k!=args.end(); )
--end;
if(i==end && (*j)->get_type()==REQUIRED_ARG)
throw usage_error((*j)->get_name()+" is required");
}
catch(const usage_error &e)
{
- throw usage_error(e.what(), "Usage: "+generate_usage(argv[0]));
+ if(!help)
+ throw usage_error(e.what(), "Usage: "+generate_usage(argv[0]));
}
if(help)
- throw usage_error(string("Help for ")+argv[0]+":", "\nUsage:\n "+generate_usage(argv[0])+"\n\n"+generate_help());
+ throw usage_error(string("Help for ")+argv[0]+":", "\nUsage:\n "+generate_usage(argv[0], true)+"\n\n"+generate_help());
}
unsigned GetOpt::process_long(const char *const *argp)
// See if the argument contains an =
unsigned equals = 0;
for(; arg[equals] && arg[equals]!='='; ++equals) ;
-
+
OptionImpl &opt = get_option(string(arg, equals));
-
+
if(arg[equals])
// Process the part after the = as option argument
opt.process(arg+equals+1);
}
else
opt.process();
-
+
return 1;
}
{
if(!argp[1])
throw usage_error("-"+string(1, *arg)+" requires an argument");
-
+
// Use the next argument as option argument
opt.process(argp[1]);
return 2;
return 1;
}
-string GetOpt::generate_usage(const string &argv0) const
+string GetOpt::generate_usage(const string &argv0, bool compact) const
{
string result = argv0;
- for(OptionList::const_iterator i=opts.begin(); i!=opts.end(); ++i)
+ if(compact)
+ result += " [options]";
+ else
{
- result += " [";
- if((*i)->get_short())
- {
- result += format("-%c", (*i)->get_short());
- if(!(*i)->get_long().empty())
- result += '|';
- else if((*i)->get_arg_type()==OPTIONAL_ARG)
- result += format("[%s]", (*i)->get_metavar());
- else if((*i)->get_arg_type()==REQUIRED_ARG)
- result += format(" %s", (*i)->get_metavar());
- }
- if(!(*i)->get_long().empty())
+ for(const OptionImpl *o: opts)
{
- result += format("--%s", (*i)->get_long());
+ result += " [";
+ if(o->get_short())
+ {
+ result += format("-%c", o->get_short());
+ if(!o->get_long().empty())
+ result += '|';
+ else if(o->get_arg_type()==OPTIONAL_ARG)
+ result += format("[%s]", o->get_metavar());
+ else if(o->get_arg_type()==REQUIRED_ARG)
+ result += format(" %s", o->get_metavar());
+ }
+ if(!o->get_long().empty())
+ {
+ result += format("--%s", o->get_long());
- if((*i)->get_arg_type()==OPTIONAL_ARG)
- result += format("[=%s]", (*i)->get_metavar());
- else if((*i)->get_arg_type()==REQUIRED_ARG)
- result += format("=%s", (*i)->get_metavar());
+ if(o->get_arg_type()==OPTIONAL_ARG)
+ result += format("[=%s]", o->get_metavar());
+ else if(o->get_arg_type()==REQUIRED_ARG)
+ result += format("=%s", o->get_metavar());
+ }
+ result += ']';
}
- result += ']';
}
- for(ArgumentList::const_iterator i=args.begin(); i!=args.end(); ++i)
+ for(const ArgumentImpl *a: args)
{
result += ' ';
- if((*i)->get_type()==OPTIONAL_ARG)
+ if(a->get_type()==OPTIONAL_ARG)
result += '[';
- result += format("<%s>", (*i)->get_name());
- if((*i)->is_list_store())
+ result += format("<%s>", a->get_name());
+ if(a->is_list_store())
result += " ...";
- if((*i)->get_type()==OPTIONAL_ARG)
+ if(a->get_type()==OPTIONAL_ARG)
result += ']';
}
string GetOpt::generate_help() const
{
- bool any_short = false;
- for(OptionList::const_iterator i=opts.begin(); (!any_short && i!=opts.end()); ++i)
- any_short = (*i)->get_short();
+ bool any_short = any_of(opts.begin(), opts.end(), [](const OptionImpl *o){ return o->get_short(); });
string::size_type maxw = 0;
- list<string> switches;
- for(OptionList::const_iterator i=opts.begin(); i!=opts.end(); ++i)
+ vector<string> switches;
+ for(const OptionImpl *o: opts)
{
string swtch;
- if((*i)->get_short())
+ if(o->get_short())
{
- swtch += format("-%c", (*i)->get_short());
- if(!(*i)->get_long().empty())
+ swtch += format("-%c", o->get_short());
+ if(!o->get_long().empty())
swtch += ", ";
- else if((*i)->get_arg_type()==OPTIONAL_ARG)
- swtch += format("[%s]", (*i)->get_metavar());
- else if((*i)->get_arg_type()==REQUIRED_ARG)
- swtch += format(" %s", (*i)->get_metavar());
+ else if(o->get_arg_type()==OPTIONAL_ARG)
+ swtch += format("[%s]", o->get_metavar());
+ else if(o->get_arg_type()==REQUIRED_ARG)
+ swtch += format(" %s", o->get_metavar());
}
else if(any_short)
swtch += " ";
- if(!(*i)->get_long().empty())
+ if(!o->get_long().empty())
{
- swtch += format("--%s", (*i)->get_long());
+ swtch += format("--%s", o->get_long());
- if((*i)->get_arg_type()==OPTIONAL_ARG)
- swtch += format("[=%s]", (*i)->get_metavar());
- else if((*i)->get_arg_type()==REQUIRED_ARG)
- swtch += format("=%s", (*i)->get_metavar());
+ if(o->get_arg_type()==OPTIONAL_ARG)
+ swtch += format("[=%s]", o->get_metavar());
+ else if(o->get_arg_type()==REQUIRED_ARG)
+ swtch += format("=%s", o->get_metavar());
}
switches.push_back(swtch);
maxw = max(maxw, swtch.size());
}
- list<string> pargs;
- for(ArgumentList::const_iterator i=args.begin(); i!=args.end(); ++i)
+ vector<string> pargs;
+ for(const ArgumentImpl *a: args)
{
- string parg = format("<%s>", (*i)->get_name());
+ string parg = format("<%s>", a->get_name());
pargs.push_back(parg);
maxw = max(maxw, parg.size());
}
string result;
result += "Options:\n";
- list<string>::const_iterator j = switches.begin();
- for(OptionList::const_iterator i=opts.begin(); i!=opts.end(); ++i, ++j)
+ auto j = switches.begin();
+ for(auto i=opts.begin(); i!=opts.end(); ++i, ++j)
result += format(" %s%s%s\n", *j, string(maxw+2-j->size(), ' '), (*i)->get_help());
if(!pargs.empty())
{
result += "\nArguments:\n";
j = pargs.begin();
- for(ArgumentList::const_iterator i=args.begin(); i!=args.end(); ++i, ++j)
+ for(auto i=args.begin(); i!=args.end(); ++i, ++j)
result += format(" %s%s%s\n", *j, string(maxw+2-j->size(), ' '), (*i)->get_help());
}
-
+
return result;
}
-GetOpt::OptionImpl::OptionImpl(char s, const std::string &l, const Store &t, ArgType a):
+GetOpt::OptionImpl::OptionImpl(char s, const string &l, const Store &t, ArgType a):
shrt(s),
lng(l),
arg_type(a),
- seen_count(0),
- ext_seen_count(0),
- metavar("ARG"),
store(t.clone())
{ }