On Saturday 19 March 2005 15:02, Stephen Deasey wrote:
> ns_parseargs spec args
>
> e.g.
>
> proc foo args {
> ns_parseargs {-opt1 -opt2 -- arg1 {arg2 def} args} $args
> # do stuf with $opt1, $arg1 . . .
> }
>
> ns_parseargs would set variables in the callers environment, or throw
> an error if $args didn't matchs the spec.
>
> It's not very Tcl'ish, but some limited type checking is also
> possible. Perhaps -opt:int to specify an integer flag?
>
> I would create a new Tcl obj type to represent the 'spec'. The first
> time ns_parseargs is called, $spec would be compiled into a set of
> Ns_ObjcSpec options. Subsequent calls should be as fast as the C
> code.
I think this is a great idea. I know it's not tcl'ish but the
-opt:(int|wint|bool) would help a lot. Omitting it would default
to string, I assume. Not that this is strictly needed in a typeless
language, but it will often save me
if {[string is integer]} {
# dio things
}
types of constructs. What about:
% string is dummy dummy
bad class "dummy": must be alnum, alpha, ascii, control, boolean, digit,
double, false, graph, integer, lower, print, punct, space, true, upper,
wordchar, or xdigit
Would it make sense to also do like:
-opt:digit
-opt:print
etc ?