Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call TCL proc with named arguments

Tags:

tcl

Given this proc:

proc foo {{aa "a"} {bb "b"} cc} {
  echo $cc
}

Is it possible to call proc foo and only pass a value for cc? Also, is it possible to pass a value to cc explicitly by name?

Everything I see suggests that all arguments must be passed by position.

like image 680
Ray Salemi Avatar asked Feb 10 '23 15:02

Ray Salemi


1 Answers

I would do something like Tk does:

proc foo {cc args} {
    # following will error if $args is an odd-length list
    array set optional [list -aa "default a" -bb "default b" {*}$args]
    set aa $optional(-aa)
    set bb $optional(-bb)

    puts "aa: $aa"
    puts "bb: $bb"
    puts "cc: $cc"
}

then

% foo
wrong # args: should be "foo cc ..."
% foo bar
aa: default a
bb: default b
cc: bar
% foo bar -bb hello -aa world
aa: world
bb: hello
cc: bar
like image 106
glenn jackman Avatar answered Mar 04 '23 17:03

glenn jackman