0

I have the following code I wrote that should do iteration on my args input list with reading two arguments every iteration. The problem that it isn't working the way I wrote. I looked upon the Tcl/Tk Wikipedia web page but couldn't find any helpful advice. Can I do it the way I wrote (without converting it into an array)?

itcl::body class::config {args} {
if {[llength $args] > 1} {
    foreach {option value} in $args {
        if {[string length $option] == 0 || [string length $value] == 0} {
            puts "Runtime error::Bad Input: option flag or value is missing"
            return
        }
        switch --$option {
            -a { 
                if { [string is integer $value -strict] } {
                    #do something
                }
            }
            -b { 
                    if { [string is integer $value -strict] } {
                    #do something
                }
            }
        }
return }
mrcalvin
  • 3,291
  • 12
  • 18
ms_stud
  • 361
  • 4
  • 18

2 Answers2

4

Drop the in, you just need:

foreach {option value} $args {

See documentation at https://www.tcl.tk/man/tcl/TclCmd/foreach.htm

Colin Macleod
  • 4,222
  • 18
  • 21
0

Some hints. Instead of:

[string length $option] == 0

you can also write:

$option == ""

The -strict option is redundant in this case, because you are already skipping the empty strings. Moreover, you should put the option right after "is integer":

string is integer -strict $value
HanT
  • 141
  • 4