pike.git / lib / modules / Arg.pmod

version» Context lines:

pike.git/lib/modules/Arg.pmod:49:       //! Base class for parsing an argument. Inherit this class to create    //! custom made option types.    class Opt    {    constant is_opt = 1;    protected Opt next;       //! The overloading method should calculate the value of the    //! option and return it. Methods processing @[argv] should only -  //! look at argv[0] and if it matches, set it to 0. Returning 0 -  //! means the option was not set (or matched). To properly chain -  //! arguments parsers, return @expr{::get_value(argv, env, -  //! previous)@} instead of @expr{0@}, unless you want to -  //! explicitly stop the chain and not set this option. +  //! look at argv[0] and if it matches, set it to 0. Returning +  //! @expr{UNDEFINED@} means the option was not set (or +  //! matched). To properly chain arguments parsers, return +  //! @expr{::get_value(argv, env, previous)@} instead of +  //! @expr{UNDEFINED@}, unless you want to explicitly stop the +  //! chain and not set this option.    mixed get_value(array(string) argv, mapping(string:string) env,    mixed previous)    {    if(next) return next->get_value(argv, env, previous); -  return 0; +  return UNDEFINED;    }       //! Should return a list of options that are parsed. To properly    //! chain argument parsers, return @expr{your_opts +    //! ::get_opts()@}.    array(string) get_opts()    {    if(!next) return ({});    return next->get_opts();    }
pike.git/lib/modules/Arg.pmod:132:    {    error("OR:ing Int objects not supported.\n");    }       protected string _sprintf(int t)    {    return t=='O' && sprintf("%O(%O)", this_program, opt);    }    }    +  //! Wrapper class that allows multiple instances of an option. +  //! @example +  //! Opt filter = Multiple(HasOpt("--filter")); +  class Multiple(Opt opt) +  { +  inherit Opt; +  +  protected mixed values = ({}); +  +  mixed get_value(array(string) argv, mapping(string:string) env, +  mixed previous) +  { +  mixed v = opt->get_value(argv, env, previous); +  if(undefinedp(v)) return UNDEFINED; +  values += ({ v }); +  return values; +  } +  +  array(string) get_opts() +  { +  return opt->get_opts(); +  } +  +  protected this_program `|(mixed thing) +  { +  error("OR:ing Multiple objects not supported.\n"); +  } +  +  protected string _sprintf(int t) +  { +  return t=='O' && sprintf("%O(%O)", this_program, values); +  } +  } +     //! Parses an option without parameter, such as --help, -x or "x"    //! from -axb.    //!    //! @example    //! Opt verbose = NoOpt("-v")|NoOpt("--verbose");    class NoOpt    {    inherit Opt;    protected string opt;    protected int double;
pike.git/lib/modules/Arg.pmod:475:       application = _argv[0];    argv = _argv[1..];    mapping(string:Opt) unset = opts+([]);       while(sizeof(argv))    {    array(string) pre = argv+({});    foreach(opts; string index; Opt arg)    { -  int(0..1)|string value = arg->get_value(argv, env, values[index]); -  if(value) +  mixed value = arg->get_value(argv, env, values[index]); +  if(!undefinedp(value))    {    m_delete(unset, index);    values[index] = value;    argv -= ({ 0 });    }    }    argv -= ({ 0 });       if(equal(pre, argv))    {    unhandled_argument(argv, env);    if(equal(pre, argv))    break;    }    }       if( sizeof(unset) )    { -  int(0..1)|string value; +  mixed value;    foreach(unset; string index; Opt arg)    {    value = arg->get_value(({}), env, values[index]); -  if(value) +  if(!undefinedp(value))    values[index] = value;    }    }       }       protected string index(string i)    {    string s = ::`[](i, this, 1);    if( !s ) return 0;