From: Mikko Rasa Date: Tue, 7 May 2013 13:41:21 +0000 (+0300) Subject: Don't output the list of options in usage with full help X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=commitdiff_plain;h=9be92503cda27dffd8c3219ec4cfadaee37b6369;hp=844da6227b2b703a2180eae759d02809c6036cce Don't output the list of options in usage with full help --- 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) diff --git a/source/core/getopt.h b/source/core/getopt.h index 411515f..407d345 100644 --- a/source/core/getopt.h +++ b/source/core/getopt.h @@ -272,8 +272,10 @@ private: 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. */