]> git.tdb.fi Git - libs/core.git/blobdiff - source/core/getopt.cpp
Remove unnecessary std:: qualifiers
[libs/core.git] / source / core / getopt.cpp
index e42ceb2143fb28265277c534af6c536878f16a03..0f8886ad746a542e3a40b36ed8e7c2ddcd0c1e19 100644 (file)
@@ -105,7 +105,7 @@ void GetOpt::operator()(unsigned argc, const char *const *argv)
                        else
                                args_raw.push_back(argv[i++]);
                }
-               
+
                for(; i<argc; ++i)
                        args_raw.push_back(argv[i]);
 
@@ -137,11 +137,12 @@ void GetOpt::operator()(unsigned argc, const char *const *argv)
        }
        catch(const usage_error &e)
        {
-               throw usage_error(e.what(), "Usage: "+generate_usage(argv[0]));
+               if(!help)
+                       throw usage_error(e.what(), "Usage: "+generate_usage(argv[0]));
        }
 
        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)
@@ -152,9 +153,9 @@ unsigned GetOpt::process_long(const char *const *argp)
        // See if the argument contains an =
        unsigned equals = 0;
        for(; arg[equals] && arg[equals]!='='; ++equals) ;
-       
+
        OptionImpl &opt = get_option(string(arg, equals));
-       
+
        if(arg[equals])
                // Process the part after the = as option argument
                opt.process(arg+equals+1);
@@ -169,7 +170,7 @@ unsigned GetOpt::process_long(const char *const *argp)
        }
        else
                opt.process();
-       
+
        return 1;
 }
 
@@ -193,7 +194,7 @@ unsigned GetOpt::process_short(const char *const *argp)
                {
                        if(!argp[1])
                                throw usage_error("-"+string(1, *arg)+" requires an argument");
-                       
+
                        // Use the next argument as option argument
                        opt.process(argp[1]);
                        return 2;
@@ -205,32 +206,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)
@@ -304,12 +310,12 @@ string GetOpt::generate_help() const
                for(ArgumentList::const_iterator i=args.begin(); i!=args.end(); ++i, ++j)
                        result += format("  %s%s%s\n", *j, string(maxw+2-j->size(), ' '), (*i)->get_help());
        }
-       
+
        return result;
 }
 
 
-GetOpt::OptionImpl::OptionImpl(char s, const std::string &l, const Store &t, ArgType a):
+GetOpt::OptionImpl::OptionImpl(char s, const string &l, const Store &t, ArgType a):
        shrt(s),
        lng(l),
        arg_type(a),