Am 27.05.2005 um 18:44 schrieb Stephen Deasey:

Right, but the option list is type checking.  One of the syntaxes we
talked about was:

    ns_parseargs {-a:int -b:bool -s:switch} $args


Which seems really as the only meaningful, simple and easy
way to do this, I must admit, after thinking awhile.

So:

     ns_parseargs {{-varname?:type? ?default?} ...}

where -varname is going to set the variable "varname" and
it will first check, according to the ?:type? what it gets.
The best is to make some examples:

   -option

   would expect an argument and set "option" to argument value
   and if the -option is not given, will do nothing

   -option default

   if option is given, it will take it, otherwise will set the
   default value

   -option:flag

   if the option is given it will set it to the boolean true,
   otherwise to boolean false

   -option:oneof {a b c}

   if the option is given it will check the argument for being
   one of the a, b or c and set the variable accordingly.

Alternatively, one could use lists instead of ":" like this:

   {-option flag}
   {-option oneof {a b c}}

This is more Tcl-like and allows you freedom in the variable name.
Example:

ns_proc connect {{-eightbit flag} {{-speed oneof {1200 2400 4800}} 4800} {port /dev/ttya}} {
    # ...
  }

So I can say:

   connect
and it will use 7bit comm, will take speed 4800 to connect to / dev/ttya

   connect /dev/ttyb
     as above but will connect to /dev/ttyb

   connect -speed 2400 /dev/ttyb
     as above but will use 2400bps instead of 4800bps

   connect -speed 9600 /dev/ttyc
     will throw error

   connect -eightbit /dev/ttyc
     will use 8bit comm to connect to /dev/ttyc

Apart from "flag" and "oneof" we can later introduce "int", "bool", "wide" etc
in order to additionaly check the given value:

    {{-number_of_retries int} 4}

Will expect number_of_retries to be an interger and if not given will set it to 4.

What do you think?
Zoran



Reply via email to