]> git.tdb.fi Git - libs/core.git/blobdiff - source/core/getopt.cpp
Add support for positional arguments in GetOpt
[libs/core.git] / source / core / getopt.cpp
index fdeb2f8827b2a48075068d153d1cea2597f6ccb1..2561c9a3e51f0acfc5bca50c099a2a8d1a3cce53 100644 (file)
@@ -15,6 +15,8 @@ GetOpt::~GetOpt()
 {
        for(OptionList::iterator i=opts.begin(); i!=opts.end(); ++i)
                delete *i;
+       for(ArgumentList::iterator i=args.begin(); i!=args.end(); ++i)
+               delete *i;
 }
 
 GetOpt::OptionImpl &GetOpt::add_option(char s, const string &l, const Store &t, ArgType a)
@@ -39,6 +41,30 @@ GetOpt::OptionImpl &GetOpt::add_option(char s, const string &l, const Store &t,
        return *opts.back();
 }
 
+GetOpt::ArgumentImpl &GetOpt::add_argument(const string &n, const Store &t, ArgType a)
+{
+       if(a==NO_ARG)
+               throw invalid_argument("GetOpt::add_argument");
+
+       bool have_list = false;
+       bool have_optional = false;
+       for(ArgumentList::const_iterator i=args.begin(); i!=args.end(); ++i)
+       {
+               if((*i)->is_list_store())
+                       have_list = true;
+               else if((*i)->get_type()==OPTIONAL_ARG)
+                       have_optional = true;
+       }
+
+       if(have_optional && (t.is_list() || a!=OPTIONAL_ARG))
+               throw invalid_argument("GetOpt::add_argument");
+       if(have_list && (t.is_list() || a==OPTIONAL_ARG))
+               throw invalid_argument("GetOpt::add_argument");
+
+       args.push_back(new ArgumentImpl(n, t, a));
+       return *args.back();
+}
+
 GetOpt::OptionImpl &GetOpt::get_option(char s)
 {
        for(OptionList::iterator i=opts.begin(); i!=opts.end(); ++i)
@@ -59,6 +85,8 @@ void GetOpt::operator()(unsigned argc, const char *const *argv)
 {
        try
        {
+               /* Arguments must first be collected into an array to handle the case
+               where a variable-length argument list is followed by fixed arguments. */
                unsigned i = 1;
                for(; i<argc;)
                {
@@ -75,11 +103,37 @@ void GetOpt::operator()(unsigned argc, const char *const *argv)
                                        i += process_short(argv+i);
                        }
                        else
-                               args.push_back(argv[i++]);
+                               args_raw.push_back(argv[i++]);
                }
                
                for(; i<argc; ++i)
-                       args.push_back(argv[i]);
+                       args_raw.push_back(argv[i]);
+
+               i = 0;
+               for(ArgumentList::const_iterator j=args.begin(); j!=args.end(); ++j)
+               {
+                       if((*j)->is_list_store())
+                       {
+                               unsigned end = args_raw.size();
+                               for(ArgumentList::const_iterator k=j; ++k!=args.end(); )
+                                       --end;
+                               if(i==end && (*j)->get_type()==REQUIRED_ARG)
+                                       throw usage_error((*j)->get_name()+" is required");
+                               for(; i<end; ++i)
+                                       (*j)->process(args_raw[i]);
+                       }
+                       else
+                       {
+                               if(i<args_raw.size())
+                                       (*j)->process(args_raw[i++]);
+                               else if((*j)->get_type()==REQUIRED_ARG)
+                                       throw usage_error((*j)->get_name()+" is required");
+                       }
+               }
+
+               // XXX Enable this when get_args() is completely removed
+               /*if(i<args_raw.size())
+                       throw usage_error("Extra positional arguments");*/
        }
        catch(const usage_error &e)
        {
@@ -179,6 +233,18 @@ string GetOpt::generate_usage(const string &argv0) const
                result += ']';
        }
 
+       for(ArgumentList::const_iterator i=args.begin(); i!=args.end(); ++i)
+       {
+               result += ' ';
+               if((*i)->get_type()==OPTIONAL_ARG)
+                       result += '[';
+               result += format("<%s>", (*i)->get_name());
+               if((*i)->is_list_store())
+                       result += " ...";
+               if((*i)->get_type()==OPTIONAL_ARG)
+                       result += ']';
+       }
+
        return result;
 }
 
@@ -218,10 +284,26 @@ string GetOpt::generate_help() const
                maxw = max(maxw, swtch.size());
        }
 
+       list<string> pargs;
+       for(ArgumentList::const_iterator i=args.begin(); i!=args.end(); ++i)
+       {
+               string parg = format("<%s>", (*i)->get_name());
+               pargs.push_back(parg);
+               maxw = max(maxw, parg.size());
+       }
+
        string result;
+       result += "Options:\n";
        list<string>::const_iterator j = switches.begin();
        for(OptionList::const_iterator i=opts.begin(); i!=opts.end(); ++i, ++j)
                result += format("  %s%s%s\n", *j, string(maxw+2-j->size(), ' '), (*i)->get_help());
+       if(!pargs.empty())
+       {
+               result += "\nArguments:\n";
+               j = pargs.begin();
+               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;
 }
@@ -299,4 +381,34 @@ void GetOpt::OptionImpl::process(const string &arg)
        }
 }
 
+
+GetOpt::ArgumentImpl::ArgumentImpl(const string &n, const Store &t, ArgType a):
+       name(n),
+       type(a),
+       store(t.clone())
+{ }
+
+GetOpt::ArgumentImpl::~ArgumentImpl()
+{
+       delete store;
+}
+
+GetOpt::ArgumentImpl &GetOpt::ArgumentImpl::set_help(const string &h)
+{
+       help = h;
+       return *this;
+}
+
+void GetOpt::ArgumentImpl::process(const string &arg)
+{
+       try
+       {
+               store->store(arg);
+       }
+       catch(const exception &e)
+       {
+               throw usage_error("Invalid "+name+" ("+e.what()+")");
+       }
+}
+
 } // namespace Msp