Branch: Tag:

2008-05-02

2008-05-02 00:45:24 by Martin Nilsson <mani@lysator.liu.se>

Moving all Arg children into the Options scope to allow direct access without importing the namespace.

Rev: lib/modules/Arg.pmod:1.3

1:   //   // Argument parser   // By Martin Nilsson - // $Id: Arg.pmod,v 1.2 2008/05/01 21:07:35 nilsson Exp $ + // $Id: Arg.pmod,v 1.3 2008/05/02 00:45:24 nilsson Exp $   //      #pike __REAL_VERSION__    -  + class ArgLibrary + { +    //! Base class for parsing an argument. Inherit this class to create   //! custom made argument types.   class Arg
13:    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). 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. +  //! 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);
25:    }       //! Should return a list of arguments that is parsed. To properly -  //! chain argument parsers, return @expr{your_args + ::get_args()@}. +  //! chain argument parsers, return @expr{your_args + +  //! ::get_args()@}.    array(string) get_args()    {    return next->get_args();
47:    return this;    }    -  //! This function will be called by @expr{_sprintf@}, which handles -  //! formatting of chaining between objects. +  //! This function will be called by @expr{_sprintf@}, which +  //! handles formatting of chaining between objects.    static string __sprintf()    {    return sprintf("%O()", this_program);
86:       int(0..1)|string get_value(array(string) argv, mapping(string:string) env)    { +  if( !sizeof(argv) ) return ::get_value(argv, env); +     if( double )    {    if( argv[0]==arg )
175:   }      //! 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@}. +  //! @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 +  //! Arg debug = MaybeArg("--debug"); +  class MaybeArg    {    inherit NoArg;       int(0..1)|string get_value(array(string) argv, mapping(string:string) env)    { -  +  if( !sizeof(argv) ) return ::get_value(argv, env); +     if( double )    {    // --foo
250:       int(0..1)|string get_value(array(string) argv, mapping(string:string) env)    { +  if( !sizeof(argv) ) return ::get_value(argv, env); +     if( double )    {    // --foo bar
277:    if( sizeof(argv[0])>1 && argv[0][0]=='-' && argv[0][1]!='-' )    {    array parts = argv[0]/"="; -  if( sizeof(parts[0]) && parts[0][-1]==arg[1..1] ) +  if( sizeof(parts[0]) && parts[0][-1]==arg[1] )    {    if( sizeof(parts)==1 )    {
314:    }   }    + } // -- ArgLibrary +    object REST = class {    static string _sprintf(int t)    {
326:      class LowOptions   { +  static inherit ArgLibrary; +     static mapping(string:Arg) args = ([]);    static mapping(string:int(1..1)|string) values = ([]);    static array(string) argv;
344:       argv = _argv[1..];    mapping(string:Arg) unset = args+([]); +     while(1)    { -  if(!sizeof(argv)) break; -  +     int(0..1)|string value;    foreach(unset; string index; Arg arg)    {
368:    else    while( sizeof(argv) && argv[0] == 0 )    argv = argv[1..]; +  +  if(!sizeof(argv)) break;    }    if( sizeof(unset) )    {
411:    static int(0..1)|string unhandled_argument(array(string) argv,    mapping(string:string) env)    { -  if( argv[0]!="--help" ) return 0; +  if( !sizeof(argv) || argv[0]!="--help" ) return 0;       string s = index("help_pre");    if( s )
509:   {    inherit Options;    Arg verbose = NoArg("-v")|NoArg("--verbose")|Env("VERBOSE"); +  Arg name = HasArg("-n")|HasArg("--name")|Default("Donald"); +  Arg debug = MaybeArg("-d")|MaybeArg("--debug");   }         void main(int num, array args)   { -  // werror("%O\n", Getopt( args )->verbose ); -  werror("%O\n", parse(args)); +  Options o = Getopt(args); +  werror("%O\n", (mapping)o );   }