-void GetOpt::operator()(unsigned argc, const char *const *argv)
-{
- unsigned i=1;
- for(; i<argc;)
- {
- if(argv[i][0]=='-')
- {
- if(argv[i][1]=='-')
- {
- if(!argv[i][2])
- break;
-
- i+=process_long(argv+i);
- }
- else
- i+=process_short(argv+i);
- }
- else
- args.push_back(argv[i++]);
- }
-
- for(; i<argc; ++i)
- args.push_back(argv[i]);
-}
-
-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);
-}
-
-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);
-}
-
-/**
-Processes the given argument as a long option.
-
-@param argp Pointer to the argument
-
-@return The number of arguments eaten (1 or 2)
-*/
-unsigned GetOpt::process_long(const char *const *argp)
-{
- // Skip the --
- const char *arg=argp[0]+2;
-
- // See if the argument contains an =
- unsigned equals=0;
- for(; arg[equals] && arg[equals]!='='; ++equals) ;
-
- OptBase &opt=get_option(string(arg, equals));
-
- if(arg[equals])
- // Process the part after the = as option argument
- opt.process(arg+equals+1);
- else if(opt.get_arg_type()==REQUIRED_ARG)
- {
- if(!argp[1])
- throw UsageError("Premature end of arguments");
-
- // Process the next argument as option argument
- opt.process(argp[1]);
- return 2;
- }
- else
- opt.process();
-
- return 1;
-}
-
-/**
-Processes short options from the given argument.
-
-@param argp Pointer to the argument
-
-@return The number of arguments eaten (1 or 2)
-*/
-unsigned GetOpt::process_short(const char *const *argp)
-{
- // Skip the -
- const char *arg=argp[0]+1;
-
- // Loop through all characters in the argument
- for(; *arg; ++arg)
- {
- OptBase &opt=get_option(*arg);
-
- if(arg[1] && opt.get_arg_type()!=NO_ARG)
- {
- // Need an option argument and we have characters left - use them
- opt.process(arg+1);
- return 1;
- }
- else if(opt.get_arg_type()==REQUIRED_ARG)
- {
- if(!argp[1])
- throw UsageError("Premature end of arguments");
-
- // Use the next argument as option argument
- opt.process(argp[1]);
- return 2;
- }
- else
- opt.process();
- }
-
- return 1;
-}
-