SandBag_1996
SandBag_1996

Reputation: 1601

passing and calling multiple arguments in tcl

I want to make a tcl procedure that can take N number of arguments. And I want to call it from a bat file. This is the procedure I wrote:

proc multi_argu {args} {
foreach {path rev_start rev_end} $args {
        puts "path-> $path"
        puts "rev_start-> $rev_start"
        puts "rev_end-> $rev_end"
    }
}
multi_argu $argv

Now I call it from my bat file as

tclsh multi_argu.tcl 1 2 3

But the out is

path-> 1 2 3
rev_start->
rev_end->

At the end of my tcl file, I am calling multi_argu $argv which I think is the culprit. However, I do not know how to do this step for multiple arguments. can anyone provide any input?

Upvotes: 1

Views: 4788

Answers (2)

Marlon Schmelling
Marlon Schmelling

Reputation: 1

Do you have to define multi_argu as a proc? Your .bat file could just call multi_argu.tcl that contains:

puts "Calling $argv0 with $argc arguments: $argv..."

foreach {path rev_start rev_end} $argv {
        puts "path-> $path"
        puts "rev_start-> $rev_start"
        puts "rev_end-> $rev_end"
    }

Now when you call it from your bat file as

tclsh multi_argu.tcl 1 2 3

The output is:

Calling multi_argu.tcl with 3 arguments: 1 2 3...
path-> 1
rev_start-> 2
rev_end-> 3

Upvotes: 0

Jerry
Jerry

Reputation: 71538

Since $argv is a list, you are passing a list variable to your proc, which means $args becomes a list, containing one list element.

In tcl 8.5, you can make a minor change to the way you call the proc:

multi_argu {*}$argv

The {*} will enumerate the items of the list, so $args becomes a list of items.

Otherwise, I guess you could use something like:

foreach {path rev_start rev_end} [lindex $args 0] { ... }

This kind of defeats the purpose of using args as the arguments of the proc though. In that case, you could use another variable name too.

Upvotes: 5

Related Questions