}
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)
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())
+ for(OptionList::const_iterator i=opts.begin(); i!=opts.end(); ++i)
{
- result += format("-%c", (*i)->get_short());
+ 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 += '|';
- 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());
+ {
+ 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());
+ 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 += ']';
}
- result += ']';
}
for(ArgumentList::const_iterator i=args.begin(); i!=args.end(); ++i)
unsigned process_short(const char *const *);
public:
- /** Generates a single line that describes known options and arguments. */
- std::string generate_usage(const std::string &) const;
+ /** Generates a single line that describes known options and arguments. If
+ compact is true, the options list is replaced with a placeholder. This
+ provides cleaner output if full help text is printed. */
+ std::string generate_usage(const std::string &, bool compact = false) const;
/** Generates help for known options and arguments in tabular format, one
item per line. The returned string will have a linefeed at the end. */