Search code examples
tclitcl

tcl/itcl - foreach iteration on args input list with two arguments


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 }

Solution

  • Drop the in, you just need:

    foreach {option value} $args {
    

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