Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用tcl解析cmdline参数?_Tcl_Cmdline Args - Fatal编程技术网

使用tcl解析cmdline参数?

使用tcl解析cmdline参数?,tcl,cmdline-args,Tcl,Cmdline Args,我试图使用命令行参数将参数传递给Spirent测试中心工具,在这里我传递插槽、端口、帧大小和负载。我想在阵列中存储插槽和端口,其中端口数是动态的。 我尝试了使用cmdline编写的简单代码,它可以处理固定端口 package require cmdline set parameters { {s.arg "" "Slot"} {p.arg "" "Port"} {l.arg "100" "Load"} {f.arg "256" "Framesiz

我试图使用命令行参数将参数传递给Spirent测试中心工具,在这里我传递插槽、端口、帧大小和负载。我想在阵列中存储插槽和端口,其中端口数是动态的。 我尝试了使用cmdline编写的简单代码,它可以处理固定端口

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)
脚本输出:

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
在这里,我想一次性传递每个插槽的所有端口

tclsh opt.tcl -s 1 2 -p 11 12 13 14 -f 256 -l 100
其中插槽为1和2,每个插槽中的端口为11、12、13、14,需要创建插槽和端口阵列。你能建议一些方法来达到这个目的吗。

试试看

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

它至少在Windows10下对我有效。问题是插槽和端口的列表必须各有一个值:引号确保了这一点。

我尝试了以下方法,并做了一些更正:

     set arglen [llength $argv]
     while {$index < $arglen} {
     set arg [lindex $argv $index]
     #puts $arg
     switch -exact -- $arg {
          -s {
        set args($arg) [lindex $argv [incr index]]
        set slot($y) $args($arg)
        incr y
            }
        -p {
           set args($arg) [lindex $argv [incr index]]
            set port($z) $args($arg)
            incr z
            }
           -l {
                 set args($arg) [lindex $argv [incr index]]
                global Load
                set Load $args($arg)
                }
            -f {
                set args($arg) [lindex $argv [incr index]]
                set frameLength $args($arg)
                }   

          }
            incr index
          }
           C:\Tcl\bin>tclsh l1.tcl -s 1 -p 11 -p 12 -l 10 -f 1