X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fcore%2Fgetopt.cpp;h=f88d4a92ac5a2c972eb0fba77cede64766d50126;hp=e42ceb2143fb28265277c534af6c536878f16a03;hb=9be92503cda27dffd8c3219ec4cfadaee37b6369;hpb=844da6227b2b703a2180eae759d02809c6036cce diff --git a/source/core/getopt.cpp b/source/core/getopt.cpp index e42ceb2..f88d4a9 100644 --- a/source/core/getopt.cpp +++ b/source/core/getopt.cpp @@ -141,7 +141,7 @@ void GetOpt::operator()(unsigned argc, const char *const *argv) } 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) @@ -205,32 +205,37 @@ unsigned GetOpt::process_short(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)