想用tcl重写之前的一些shell工具,但是发现tcllib里面cmdline包的getoptions只支持tcl风格的选项
/>
-
package require cmdline
-
-
# Show argv before processing
-
puts "Before, argv = '$argv'"
-
-
# Process the command line
-
set parameters {
-
{server.arg "" "Which server to test"}
-
{port.arg 5551 "Port to send test cmd"}
-
{user.arg "" "Login name"}
-
{debug "Output extra debug info"}
-
}
-
array set arg [cmdline::getoptions argv $parameters]
-
-
# Verify required parameters
-
set requiredParameters {server user}
-
foreach parameter $requiredParameters {
-
if {$arg($parameter) == ""} {
-
puts stderr "Missing required parameter: -$parameter"
-
exit 1
-
}
-
}
-
-
# Displays the arguments
-
puts ""
-
parray arg
-
puts ""
-
-
# Show argv after processing
-
puts "After, argv = '$argv'"
加了一个补丁(), 但用着还是不太理想
自己动手写了一个,兼容GNU getopt风格的getOpt tcl lib,附件中的包解压到/usr/local/lib/下, 然后具体用法如下:
-
#!/bin/sh
-
# Jianhong Yin <yin-jianhong@163.com>
-
# -*- tcl -*-
-
# The next line is executed by /bin/sh, but not tcl \
-
exec tclsh "$0" ${1+"$@"}
-
-
lappend auto_path /usr/local/lib
-
package require getOpt 1.0
-
namespace import ::getOpt::*
-
-
# global var
-
array set Opt {}
-
array set InvalidOpt {}
-
set NotOptions [list]
-
set OptionList {
-
f {arg y help {#Specify a test list file}}
-
listf {link f}
-
cc {arg m help {#Notify additional e-mail address on job completion}}
-
kcov {arg n help {#insert kcov task for do the kernel test coverage check}}
-
kdump {arg o help {#insert kdump task for get core dump file if panic happen}}
-
}
-
-
# getUsage test
-
puts "Usage: $argv0 \[options\]"
-
puts "Option list:"
-
getUsage $OptionList
-
-
# _parse_ argument
-
getOptions $OptionList $::argv Opt InvalidOpt NotOptions
-
parray Opt
-
parray InvalidOpt
-
puts "NotOptions: $NotOptions"
代码 />
阅读(7829) | 评论(0) | 转发(0) |