pike.git / lib / modules / Arg.pike

version» Context lines:

pike.git/lib/modules/Arg.pike:1:   //   // Argument parser   // By Martin Nilsson - // $Id: Arg.pike,v 1.2 2007/09/16 13:07:48 nilsson Exp $ + // $Id: Arg.pike,v 1.3 2007/09/16 13:32:54 nilsson Exp $   //      #pike __REAL_VERSION__    - // Base class for parsing an argument. + //! Base class for parsing an argument. Inherit this class to create + //! custom made argument types.   class Arg   {    constant is_arg = 1;    static Arg next;    -  // Should return 1 for set options or a string containing the value -  // of the option. Returning 0 means the option was not set (or -  // matched). +  //! Should return 1 for set options or a string containing the value +  //! of the option. Returning 0 means the option was not set (or +  //! matched). To properly chain arguments parsers, return +  //! @expr{::get_value(argv, env)@} instead of @expr{0@}, unless you +  //! want to explicitly stop the chain and not set this option.    int(0..1)|string get_value(array(string) argv, mapping(string:string) env)    {    if(next) return next->get_value(argv, env);    return 0;    }    -  // +  //! Should return a list of arguments that is parsed. To properly +  //! chain argument parsers, return @expr{your_args + ::get_args()@}.    array(string) get_args()    {    return next->get_args();    }       static this_program `|(mixed thing)    {    if( !objectp(thing) || !thing->is_arg )    error("Can only or %O with another %O.\n",    this, this_program);
pike.git/lib/modules/Arg.pike:36:    if( next )    {    next = next | thing;    return this;    }       next = thing;    return this;    }    -  // +  //! This function will be called by @expr{_sprintf@}, which handles +  //! formatting of chaining between objects.    static string __sprintf()    {    return sprintf("%O()", this_program);    }       static string _sprintf(int t)    {    if( t!='O' ) return UNDEFINED;    if( !next )    return __sprintf();    else    return sprintf("%s|%O", __sprintf(), next);    }   }    - // Parses an argument without parameter. + //! Parses an argument without parameter, such as --help, -x or "x" + //! from -axb. + //! + //! @example + //! Arg verbose = NoArg("-v")|NoArg("--verbose");   class NoArg   {    inherit Arg;    static string arg;    static int double;       static void create(string _arg)    {    if( sizeof(_arg)>2 && has_prefix(_arg, "--") )    double = 1;
pike.git/lib/modules/Arg.pike:106:    {    return ({ arg }) + ::get_args();    }       static string __sprintf()    {    return sprintf("%O(%O)", this_program, arg);    }   }    - // Environment fallback for an argument. + //! Environment fallback for an argument. Can of course be used as + //! only Arg source. + //! + //! @example + //! Arg debug = NoArg("--debug")|Env("MY_DEBUG");   class Env   {    inherit Arg;    static string name;       static void create(string _name)    {    name = _name;    }   
pike.git/lib/modules/Arg.pike:129:    if( env[name] ) return env[name];    return ::get_value(argv, env);    }       static string __sprintf()    {    return sprintf("%O(%O)", this_program, name);    }   }    + //! Default value for a setting. + //! + //! @example + //! Arg output = HasArg("-o")|Default("a.out");   class Default   {    inherit Arg;    static string value;       static void create(string _value)    {    value = _value;    }   
pike.git/lib/modules/Arg.pike:150:    {    return value;    }       static string __sprintf()    {    return sprintf("%O(%O)", this_program, value);    }   }    + //! Parses an argument that may have a parameter. @tt{--foo@}, + //! @tt{-x@} and x in a sequence like @tt{-axb@} will set the variable + //! to @expr{1@}. @tt{--foo=bar@}, @tt{-x bar@} and @tt{-x=bar@} will + //! set the variable to @expr{bar@}. + //! + //! @example + //! Arg debug = MaybyArg("--debug");   class MaybyArg   {    inherit NoArg;       int(0..1)|string get_value(array(string) argv, mapping(string:string) env)    {    if( double )    { -  // --foo bar +  // --foo    if( argv[0]==arg )    {    argv[0] = 0;    return 1;    }       // --foo=bar    if( sscanf(argv[0], arg+"=%s", string ret)==1 )    {    argv[0] = 0;
pike.git/lib/modules/Arg.pike:208:    return parts[1..]*"=";    }       return ::get_value(argv, env);    }       return ::get_value(argv, env);    }   }    + //! Parses an argument that has a parameter. @tt{--foo=bar@}, @tt{-x + //! bar@} and @tt{-x=bar@} will set the variable to @expr{bar@}. + //! + //! @example + //! Arg user = HasArg("--user")|HasArg("-u");   class HasArg   {    inherit NoArg;       int(0..1)|string get_value(array(string) argv, mapping(string:string) env)    {    if( double )    {    // --foo bar    if( argv[0]==arg )
pike.git/lib/modules/Arg.pike:278:    argv[0] = parts[0];    return parts[1..]*"=";    }    }    }       return ::get_value(argv, env);    }   }    - // FIXME: Support for rc files? + // FIXME: Support for rc files? ( Arg x = Arg("--x")|INIFile(path, name); ) + // FIXME: Support for type casts? ( Arg level = Integer(Arg("--level"));      class LowOptions   {    static mapping(string:Arg) args = ([]);    static mapping(string:int(1..1)|string) values = ([]);    static array(string) argv;       static void create(array(string) _argv, void|mapping(string:string) env)    {    if(!env)
pike.git/lib/modules/Arg.pike:340:       }       static int(0..1) unhandled_argument(array(string) argv,    mapping(string:string) env)    {    return 0;    }   }    + //! The option parser class that contains all the argument objects. + //!   class Options   {    inherit LowOptions;       static string|int `[](string id)    {    return values[id];    }    static string|int `->(string id)    {