pike.git / lib / modules / Arg.pmod

version» Context lines:

pike.git/lib/modules/Arg.pmod:1: - // +    // Argument parser   // By Martin Nilsson   //      #pike __REAL_VERSION__      class OptLibrary -  + //!   {       //! Base class for parsing an argument. Inherit this class to create    //! custom made option types.    class Opt    {    constant is_opt = 1;    protected Opt next;       //! Should return 1 for set options or a string containing the
pike.git/lib/modules/Arg.pmod:339:      } // -- OptLibrary      object REST = class {    protected string _sprintf(int t)    {    return "Arg.REST";    }    }();    + //! @decl constant REST = REST(); + //! + //! Constant used by Arg.parse() to indicate the remaining objects. +  +    // FIXME: Support for rc files? ( Opt x = Opt("--x")|INIFile(path, name); )   // FIXME: Support for type casts? ( Opt level = Integer(Opt("--level"));      class LowOptions -  +  //!   {    protected inherit OptLibrary;    -  +  //!    protected mapping(string:Opt) opts = ([]); -  +  +  //!    protected mapping(string:int(1..1)|string) values = ([]); -  +  +  //!    protected array(string) argv; -  +  +  //!    protected string application;    -  +  //!    protected void create(array(string) _argv, void|mapping(string:string) env)    {    if(!env)    env = getenv();       // Make a list of all the arguments we can parse.    foreach(::_indices(2), string index)    {    mixed val = ::`[](index, this, 0);    if(objectp(val) && val->is_opt) opts[index]=val;
pike.git/lib/modules/Arg.pmod:403:    foreach(unset; string index; Opt arg)    {    value = arg->get_value(({}), env, values[index]);    if(value)    values[index] = value;    }    }       }    +  //!    protected int(0..1) unhandled_argument(array(string) argv,    mapping(string:string) env)    {    return 0;    }       protected mixed cast(string to)    {    switch( to )    {
pike.git/lib/modules/Arg.pmod:432:   //! The option parser class that contains all the argument objects.   //!   class Options   {    inherit LowOptions;       protected string|int `[](string id)    {    return values[id];    } +     protected string|int `->(string id)    {    return values[id];    }    -  +  //!    protected int(0..1)|string unhandled_argument(array(string) argv,    mapping(string:string) env)    {    if( !sizeof(argv) || argv[0]!="--help" ) return 0;       string s = index("help_pre");    if( s )    write( s+"\n" );       foreach(opts; string i; Opt opt)
pike.git/lib/modules/Arg.pmod:478:    return s;    }    return 0;    }   }         // --- Simple interface      class SimpleOptions + //! Options parser with a unhandled_argument implementation that + //! parses most common argument formats.   {    inherit LowOptions;    -  +  //! Handles arguments as described in @[Arg.parse]    int(0..1) unhandled_argument(array(string) argv, mapping(string:string) env)    {    string arg = argv[0];    if(!sizeof(arg) || arg[0]!='-') return 0;       string name,value;       if( has_prefix(arg, "--") )    {    sscanf( arg, "--%s=%s", name, value ) || sscanf( arg, "--%s", name );
pike.git/lib/modules/Arg.pmod:531:   // --foo x --bar -> "foo":1 (?)   // -foo -> "f":1,"o":2   // -x -x -x -> "x":3   //   // void main(int n, array argv)   // {   // mapping opts = Arg.parse(argv);   // argv = opts[Arg.REST];   // }    -  - mapping(string:string|int(1..1)) parse(array(string) argv) + //! Convenience function for simple argument parsing. + //! + //! Handles the most common cases. + //! + //! The return value is a mapping from option name to the option value. + //! + //! The special index Arg.REST will be set to the remaining arguments + //! after all options have been parsed. + //! + //! The following argument syntaxes are supported: + //! + //! @code + //! --foo -> "foo":1 + //! --foo=bar -> "foo":"bar" + //! -bar -> "b":1,"a":1,"r":1 + //! -bar=foo -> "b":1,"a":1,"r":"foo" (?) + //! --foo --bar -> "foo":1,"bar":1 + //! --foo - --bar -> "foo":1 + //! --foo x --bar -> "foo":1 (?) + //! -foo -> "f":1,"o":2 + //! -x -x -x -> "x":3 + //! @endcode + //! + //! @example + //! @code + //! void main(int n, array argv) + //! { + //! mapping opts = Arg.parse(argv); + //! argv = opts[Arg.REST]; + //! if( opts->help ) /*... */ + //! } + //! @endcode + mapping(string:string|int(1..)) parse(array(string) argv)   {    return (mapping)SimpleOptions(argv);   }