简体   繁体   English

使用tcl的cmdline参数解析?

[英]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 我用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)

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. 其中插槽为1和2,每个插槽中的端口为11,12,13,14,需要创建插槽和端口阵列。 Could you please suggest some method to achieve this. 您能否提出一些实现此目标的方法?

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. 至少在Windows 10下它对我有用。 The thing is that the lists of slots and ports need to be one value each: the quotes ensure that. 关键是插槽和端口的列表每个必须是一个值:引号确保了这一点。

I tried the following method with some corrections: 我尝试了以下方法并进行了一些更正:

     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
          }

Command to run: 运行命令:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM