]> git.tdb.fi Git - libs/core.git/blobdiff - source/core/getopt.cpp
Make help message printing automatic
[libs/core.git] / source / core / getopt.cpp
index 74f14761c8636c19c7612cd27972ae5ac683832d..9b8b84cdee323bc1c83f2ebb35dd69915b2f9a00 100644 (file)
@@ -11,6 +11,12 @@ using namespace std;
 
 namespace Msp {
 
+GetOpt::GetOpt():
+       help(false)
+{
+       add_option("help", help, NO_ARG).set_help("Displays this help");
+}
+
 GetOpt::~GetOpt()
 {
        for(list<OptBase *>::iterator i=opts.begin(); i!=opts.end(); ++i)
@@ -22,7 +28,7 @@ GetOpt::OptBase &GetOpt::get_option(char s)
        for(list<OptBase *>::iterator i=opts.begin(); i!=opts.end(); ++i)
                if((*i)->get_short()==s)
                        return **i;
-       throw UsageError(string("Unknown option -")+s);
+       throw usage_error(string("Unknown option -")+s);
 }
 
 GetOpt::OptBase &GetOpt::get_option(const string &l)
@@ -30,32 +36,42 @@ GetOpt::OptBase &GetOpt::get_option(const string &l)
        for(list<OptBase *>::iterator i=opts.begin(); i!=opts.end(); ++i)
                if((*i)->get_long()==l)
                        return **i;
-       throw UsageError(string("Unknown option --")+l);
+       throw usage_error(string("Unknown option --")+l);
 }
 
 void GetOpt::operator()(unsigned argc, const char *const *argv)
 {
-       unsigned i = 1;
-       for(; i<argc;)
+       try
        {
-               if(argv[i][0]=='-')
+               unsigned i = 1;
+               for(; i<argc;)
                {
-                       if(argv[i][1]=='-')
+                       if(argv[i][0]=='-')
                        {
-                               if(!argv[i][2])
-                                       break;
-
-                               i += process_long(argv+i);
+                               if(argv[i][1]=='-')
+                               {
+                                       if(!argv[i][2])
+                                               break;
+
+                                       i += process_long(argv+i);
+                               }
+                               else
+                                       i += process_short(argv+i);
                        }
                        else
-                               i += process_short(argv+i);
+                               args.push_back(argv[i++]);
                }
-               else
-                       args.push_back(argv[i++]);
+               
+               for(; i<argc; ++i)
+                       args.push_back(argv[i]);
        }
-       
-       for(; i<argc; ++i)
-               args.push_back(argv[i]);
+       catch(const usage_error &e)
+       {
+               throw usage_error(e.what(), "Usage: "+generate_usage(argv[0]));
+       }
+
+       if(help)
+               throw usage_error(string("Help for ")+argv[0]+":", generate_help());
 }
 
 unsigned GetOpt::process_long(const char *const *argp)
@@ -75,7 +91,7 @@ unsigned GetOpt::process_long(const char *const *argp)
        else if(opt.get_arg_type()==REQUIRED_ARG)
        {
                if(!argp[1])
-                       throw UsageError("Premature end of arguments");
+                       throw usage_error("--"+string(arg)+" requires an argument");
 
                // Process the next argument as option argument
                opt.process(argp[1]);
@@ -106,7 +122,7 @@ unsigned GetOpt::process_short(const char *const *argp)
                else if(opt.get_arg_type()==REQUIRED_ARG)
                {
                        if(!argp[1])
-                               throw UsageError("Premature end of arguments");
+                               throw usage_error("-"+string(1, *arg)+" requires an argument");
                        
                        // Use the next argument as option argument
                        opt.process(argp[1]);
@@ -226,7 +242,7 @@ GetOpt::OptBase &GetOpt::OptBase::set_help(const string &h, const string &m)
 void GetOpt::OptBase::process()
 {
        if(arg_type==REQUIRED_ARG)
-               throw UsageError("--"+lng+" requires an argument");
+               throw usage_error("--"+lng+" requires an argument");
        ++seen_count;
 
        store();
@@ -235,7 +251,7 @@ void GetOpt::OptBase::process()
 void GetOpt::OptBase::process(const string &arg)
 {
        if(arg_type==NO_ARG)
-               throw UsageError("--"+lng+" takes no argument");
+               throw usage_error("--"+lng+" takes no argument");
        ++seen_count;
 
        store(arg);