]> git.tdb.fi Git - libs/core.git/blobdiff - source/core/getopt.cpp
Don't output the list of options in usage with full help
[libs/core.git] / source / core / getopt.cpp
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)