1

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?

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.