TCL:使用命令行包中的 "help" 选项
TCL: use "help" option from cmdline package
在我的 TCL 脚本中,我试图提供将使用 'cmdline' 包解析的输入参数。我在脚本中定义了以下内容:
set run_options {
{input_1.arg 8 "first input argument"}
{input_2.arg 1 "second input argument"}
{msb "choose it if input data has msb first"}
{lsb "choose it if input data has lsb first"}
}
set my_usage ": \n tclsh my_src \[options] ...\noptions are:"
if {[catch {array set params [::cmdline::getoptions argv $run_options $my_usage]} msg]} {
puts "Error while parsing user options of $msg"
exit 1
}
一切看起来都很好,除了当 运行我的脚本时我发现 'cmdline' 包默认定义了“帮助”选项,但是当我 运行 我的脚本带有“ -help”选项下面是输出:
Error while parsing user options of my_src :
tclsh my_src [options] ...
options are:
-input_1 value first input argument <8>
-input_2 value second input argument <1>
-msb choose it if input data has msb first
-lsb choose it if input data has lsb first
-- Forcibly stop option processing
-help Print this message
-? Print this message
所以,有谁知道如何使用这个自动添加的“帮助”选项而不给出错误消息?
cmdline::getoptions
不区分 -help
和未知选项(无法识别的选项被视为 -?
)。如果你想对这两种情况有不同的消息,你需要直接在循环中使用一个较低级别的函数,或者像下面的 cmdline::getoptions
的增强版本:
#!/usr/bin/env tclsh
package require try ;# In case you're still using tcl 8.5 for some reason
package require cmdline 1.5
proc better_getoptions {arglistVar optlist usage} {
upvar 1 $arglistVar argv
# Warning: Internal cmdline function
set opts [::cmdline::GetOptionDefaults $optlist result]
while {[set err [::cmdline::getopt argv $opts opt arg]]} {
if {$err < 0} {
return -code error -errorcode {CMDLINE ERROR} $arg
}
set result($opt) $arg
}
if {[info exists result(?)] || [info exists result(help)]} {
return -code error -errorcode {CMDLINE USAGE} \
[::cmdline::usage $optlist $usage]
}
return [array get result]
}
set run_options {
{input_1.arg 8 "first input argument"}
{input_2.arg 1 "second input argument"}
{msb "choose it if input data has msb first"}
{lsb "choose it if input data has lsb first"}
}
set my_usage ": \n tclsh my_src \[options] ...\noptions are:"
try {
array set params [better_getoptions argv $run_options $my_usage]
puts "All options valid"
} trap {CMDLINE USAGE} {msg} {
puts stderr $msg
exit 0
} trap {CMDLINE ERROR} {msg} {
puts stderr "Error: $msg"
exit 1
}
用法示例:
$ ./example.tcl --help
cmd :
tclsh my_src [options] ...
options are:
-input_1 value first input argument <8>
-input_2 value second input argument <1>
-msb choose it if input data has msb first
-lsb choose it if input data has lsb first
-- Forcibly stop option processing
-help Print this message
-? Print this message
$ ./example.tcl -foo
Error: Illegal option "-foo"
$ ./example.tcl -input_1
Error: Option "input_1" requires an argument
$ ./example.tcl -msb
All options valid
在我的 TCL 脚本中,我试图提供将使用 'cmdline' 包解析的输入参数。我在脚本中定义了以下内容:
set run_options {
{input_1.arg 8 "first input argument"}
{input_2.arg 1 "second input argument"}
{msb "choose it if input data has msb first"}
{lsb "choose it if input data has lsb first"}
}
set my_usage ": \n tclsh my_src \[options] ...\noptions are:"
if {[catch {array set params [::cmdline::getoptions argv $run_options $my_usage]} msg]} {
puts "Error while parsing user options of $msg"
exit 1
}
一切看起来都很好,除了当 运行我的脚本时我发现 'cmdline' 包默认定义了“帮助”选项,但是当我 运行 我的脚本带有“ -help”选项下面是输出:
Error while parsing user options of my_src :
tclsh my_src [options] ...
options are:
-input_1 value first input argument <8>
-input_2 value second input argument <1>
-msb choose it if input data has msb first
-lsb choose it if input data has lsb first
-- Forcibly stop option processing
-help Print this message
-? Print this message
所以,有谁知道如何使用这个自动添加的“帮助”选项而不给出错误消息?
cmdline::getoptions
不区分 -help
和未知选项(无法识别的选项被视为 -?
)。如果你想对这两种情况有不同的消息,你需要直接在循环中使用一个较低级别的函数,或者像下面的 cmdline::getoptions
的增强版本:
#!/usr/bin/env tclsh
package require try ;# In case you're still using tcl 8.5 for some reason
package require cmdline 1.5
proc better_getoptions {arglistVar optlist usage} {
upvar 1 $arglistVar argv
# Warning: Internal cmdline function
set opts [::cmdline::GetOptionDefaults $optlist result]
while {[set err [::cmdline::getopt argv $opts opt arg]]} {
if {$err < 0} {
return -code error -errorcode {CMDLINE ERROR} $arg
}
set result($opt) $arg
}
if {[info exists result(?)] || [info exists result(help)]} {
return -code error -errorcode {CMDLINE USAGE} \
[::cmdline::usage $optlist $usage]
}
return [array get result]
}
set run_options {
{input_1.arg 8 "first input argument"}
{input_2.arg 1 "second input argument"}
{msb "choose it if input data has msb first"}
{lsb "choose it if input data has lsb first"}
}
set my_usage ": \n tclsh my_src \[options] ...\noptions are:"
try {
array set params [better_getoptions argv $run_options $my_usage]
puts "All options valid"
} trap {CMDLINE USAGE} {msg} {
puts stderr $msg
exit 0
} trap {CMDLINE ERROR} {msg} {
puts stderr "Error: $msg"
exit 1
}
用法示例:
$ ./example.tcl --help
cmd :
tclsh my_src [options] ...
options are:
-input_1 value first input argument <8>
-input_2 value second input argument <1>
-msb choose it if input data has msb first
-lsb choose it if input data has lsb first
-- Forcibly stop option processing
-help Print this message
-? Print this message
$ ./example.tcl -foo
Error: Illegal option "-foo"
$ ./example.tcl -input_1
Error: Option "input_1" requires an argument
$ ./example.tcl -msb
All options valid