Search code examples
tclcmdline-args

cmdline argument parsing using tcl?


I am trying to pass the parameters to Spirent test center tool using command line arguments, where I am passing slots, ports, frame size and load. I want to store the Slots and ports in array, where number of ports are dynamic. I tried simple code with cmdline which can handle fixed ports

package require cmdline

set parameters {
    {s.arg ""   "Slot"}
    {p.arg ""   "Port"}
    {l.arg "100"   "Load"}
    {f.arg "256"   "Framesize"}
    {debug      "Turn on debugging, default=off"}
}
#set option(l) 100
set usage "- A simple script to demo cmdline parsing"

if {[catch {array set options [cmdline::getoptions ::argv $parameters $usage]}]} {
    puts [cmdline::usage $parameters $usage]
} else {
    parray options
}
#puts [array get options]
puts $options(l)
puts $options(f)

script Output:

C:\Tcl\bin>tclsh opt.tcl -s 1 -f 128
options(debug) = 0
options(f)     = 128
options(l)     = 100
options(p)     =
options(s)     = 1
100
128

Here I would like to pass all the ports for each slots onetime ,

tclsh opt.tcl -s 1 2 -p 11 12 13 14 -f 256 -l 100

Where slots are 1 and 2 and ports in each slot are 11,12,13,14 and need to create array of slot and ports. Could you please suggest some method to achieve this.


Solution

  • Try

    tclsh opt.tcl -s "1 2" -p "11 12 13 14" -f 256 -l 100
    

    It works for me under Windows 10, at least. The thing is that the lists of slots and ports need to be one value each: the quotes ensure that.