+string GetOpt::generate_usage(const string &argv0, bool compact) const
+{
+ string result = argv0;
+ if(compact)
+ result += " [options]";
+ else
+ {
+ for(OptionList::const_iterator i=opts.begin(); i!=opts.end(); ++i)
+ {
+ 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())
+ {
+ result += format("--%s", (*i)->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());
+ }
+ result += ']';
+ }
+ }
+
+ for(ArgumentList::const_iterator i=args.begin(); i!=args.end(); ++i)
+ {
+ result += ' ';
+ if((*i)->get_type()==OPTIONAL_ARG)
+ result += '[';
+ result += format("<%s>", (*i)->get_name());
+ if((*i)->is_list_store())
+ result += " ...";
+ if((*i)->get_type()==OPTIONAL_ARG)
+ result += ']';
+ }
+
+ return 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();
+
+ string::size_type maxw = 0;
+ list<string> switches;
+ for(OptionList::const_iterator i=opts.begin(); i!=opts.end(); ++i)
+ {
+ string swtch;
+ if((*i)->get_short())
+ {
+ swtch += format("-%c", (*i)->get_short());
+ if(!(*i)->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(any_short)
+ swtch += " ";
+ if(!(*i)->get_long().empty())
+ {
+ swtch += format("--%s", (*i)->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());
+ }
+ switches.push_back(swtch);
+ maxw = max(maxw, swtch.size());
+ }
+
+ list<string> pargs;
+ for(ArgumentList::const_iterator i=args.begin(); i!=args.end(); ++i)
+ {
+ string parg = format("<%s>", (*i)->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)
+ 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)
+ 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):
+ shrt(s),
+ lng(l),
+ arg_type(a),
+ seen_count(0),
+ ext_seen_count(0),
+ metavar("ARG"),
+ store(t.clone())
+{ }
+
+GetOpt::OptionImpl::~OptionImpl()
+{
+ delete store;
+}
+
+GetOpt::OptionImpl &GetOpt::OptionImpl::set_help(const string &h)
+{
+ help = h;
+ return *this;
+}
+
+GetOpt::OptionImpl &GetOpt::OptionImpl::set_help(const string &h, const string &m)
+{
+ help = h;
+ metavar = m;
+ return *this;
+}
+
+GetOpt::OptionImpl &GetOpt::OptionImpl::bind_seen_count(unsigned &c)
+{
+ ext_seen_count = &c;
+ return *this;
+}
+
+void GetOpt::OptionImpl::process()
+{
+ if(arg_type==REQUIRED_ARG)
+ throw usage_error("--"+lng+" requires an argument");
+
+ ++seen_count;
+ if(ext_seen_count)
+ *ext_seen_count = seen_count;
+
+ try
+ {
+ store->store();
+ }
+ catch(const exception &e)
+ {
+ throw usage_error("Invalid argument for --"+lng+" ("+e.what()+")");
+ }
+}
+
+void GetOpt::OptionImpl::process(const string &arg)
+{
+ if(arg_type==NO_ARG)
+ throw usage_error("--"+lng+" takes no argument");
+
+ ++seen_count;
+ if(ext_seen_count)
+ *ext_seen_count = seen_count;
+
+ try
+ {
+ store->store(arg);
+ }
+ catch(const exception &e)
+ {
+ throw usage_error("Invalid argument for --"+lng+" ("+e.what()+")");
+ }
+}
+
+
+GetOpt::ArgumentImpl::ArgumentImpl(const string &n, const Store &t, ArgType a):
+ name(n),
+ type(a),
+ store(t.clone())
+{ }
+
+GetOpt::ArgumentImpl::~ArgumentImpl()
+{
+ delete store;
+}
+
+GetOpt::ArgumentImpl &GetOpt::ArgumentImpl::set_help(const string &h)
+{
+ help = h;
+ return *this;
+}
+
+void GetOpt::ArgumentImpl::process(const string &arg)
+{
+ try
+ {
+ store->store(arg);
+ }
+ catch(const exception &e)
+ {
+ throw usage_error("Invalid "+name+" ("+e.what()+")");
+ }
+}
+