]> git.tdb.fi Git - libs/core.git/commitdiff
Don't output the list of options in usage with full help
authorMikko Rasa <tdb@tdb.fi>
Tue, 7 May 2013 13:41:21 +0000 (16:41 +0300)
committerMikko Rasa <tdb@tdb.fi>
Tue, 7 May 2013 13:41:21 +0000 (16:41 +0300)
source/core/getopt.cpp
source/core/getopt.h

index e42ceb2143fb28265277c534af6c536878f16a03..f88d4a92ac5a2c972eb0fba77cede64766d50126 100644 (file)
@@ -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)
index 411515f5dd9c1d837e47131ca3c2ee1d08f21e91..407d3459984fce67c42ffd92f1c9ffe7faae4b23 100644 (file)
@@ -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. */