TCL:use";“帮助”;来自cmdline包的选项

TCL:use";“帮助”;来自cmdline包的选项,tcl,cmdline-args,Tcl,Cmdline Args,在我的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 i

在我的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”选项外,其他一切看起来都很好,但当我使用“-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
分析my_src的用户选项时出错: Tcsh my_src[选项]。。。 选项包括: -input_1值第一个输入参数 -第二个输入参数的输入值 -msb如果输入数据先有msb,请选择它 -lsb如果输入数据先有lsb,则选择它 --强制停止选项处理 -帮助打印此邮件 -? 打印此邮件
那么,有人知道如何使用自动添加的“帮助”选项而不给出错误消息吗?

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
}
#/usr/bin/env tclsh
套餐需要试一试;#如果您出于某种原因仍在使用tcl 8.5
程序包需要cmdline 1.5
proc better_getoptions{arglistVar optlist用法}{
upvar 1$arglistVar argv
#警告:内部cmdline函数
set opts[::cmdline::GetOptionDefaults$optlist结果]
而{[set err[::cmdline::getopt argv$opts opt arg]}{
如果{$err<0}{
返回-代码错误-错误代码{CMDLINE error}$arg
}
设置结果($opt)$arg
}
如果{[info exists result(?)]| |[info exists result(help)]}{
返回-代码错误-错误代码{CMDLINE用法}\
[::cmdline::用法$optlist$usage]
}
返回[数组获取结果]
}
设置运行选项{
{input_1.arg 8“第一个输入参数”}
{input_2.arg 1“第二个输入参数”}
{msb“如果输入数据具有msb first,请选择它”}
{lsb“如果输入数据首先有lsb,请选择它”}
}
设置我的\u用法“:\n tclsh我的\u src\[选项]…\n选项包括:
试一试{
数组设置参数[更好的\u getoptions argv$run\u options$my\u用法]
将“所有选项都有效”
}陷阱{CMDLINE用法}{msg}{
放入stderr$msg
出口0
}陷阱{CMDLINE错误}{msg}{
将stderr放入“错误:$msg”
出口1
}
用法示例:

$。/example.tcl--帮助
命令:
Tcsh my_src[选项]。。。
选项包括:
-input_1值第一个输入参数
-第二个输入参数的输入值
-msb如果输入数据先有msb,请选择它
-lsb如果输入数据先有lsb,则选择它
--强制停止选项处理
-帮助打印此邮件
-?                   打印此邮件
$./example.tcl-foo
错误:非法选项“-foo”
$./example.tcl-输入_1
错误:选项“input_1”需要一个参数
$./example.tcl-msb
所有选项都有效

不要打印错误消息中的“Error while…”部分?对,只需
放入$msg
您可能会错过我在其他情况下使用“Error”的要点。例如:如果我提供了错误的选项,那么我应该得到这个“错误”。但是“帮助”不应该是错误。
cmdline::getoptions
不区分
-help
和未知选项;他们都犯了同样的错误。(如果查看源代码,未知选项的处理方式与
-?
完全相同)