TCL中用于解析参数的程序包 [英] package for parsing argument in TCL

查看:153
本文介绍了TCL中用于解析参数的程序包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道tcl可以轻松解析输入参数的标准软件包吗?还是准备好了? (我只有3个标志,但一般情况更可取。)。

Does anyone know a standard package for tcl to easily parse the input arguments ? or a ready proc ? ( I have only 3 flags but something general is preferable ).

推荐答案

文档包含一个示例。这是一个简单的示例:

The documentation includes an example. Here is a simple example:

package require cmdline

set parameters {
    {server.arg ""   "Which server to search"}
    {debug           "Turn on debugging, default=off"}
}

set usage "- A simple script to demo cmdline parsing"
array set options [cmdline::getoptions ::argv $parameters $usage]
parray options

示例运行:

$ tclsh simple.tcl 
options(debug)  = 0
options(server) = 

$ tclsh simple.tcl -server google.com
options(debug)  = 0
options(server) = google.com

$ tclsh simple.tcl -server google.com -debug
options(debug)  = 1
options(server) = google.com

$ tclsh simple.tcl -help
simple - A simple script to demo cmdline parsing
 -server value        Which server to search <>
 -debug               Turn on debugging, default=off
 -help                Print this message
 -?                   Print this message

    while executing
"error [usage $optlist $usage]"
    (procedure "cmdline::getoptions" line 15)
    invoked from within
"cmdline::getoptions ::argv $parameters $usage"
    invoked from within
"array set options [cmdline::getoptions ::argv $parameters $usage]"
    (file "simple.tcl" line 11)



讨论




  • 与大多数Linux实用程序不同,TCL在命令行选项中使用单破折号代替双破折号

  • 当标志以 .arg结尾时,则该标志要求后面跟随一个参数,例如 server.arg

  • 调试标志不以 .arg 结尾,因此它不需要任何参数

  • 用户通过列表列表定义命令行参数。每个子列表包含2或3个部分:


    • 标志(例如 debug

    • 仅当参数接受参数(标志以 .arg 结尾)时,默认值(例如0)。

    • 帮助消息

    • Discussion

      • Unlike most Linux utilities, TCL uses single dash instead of double dashes for command-line options
      • When a flags ends with .arg, then that flag expects an argument to follow, such as in the case of server.arg
      • The debug flag does not end with .arg, therefore it does not expect any argument
      • The user defines the command-line parameters by a list of lists. Each sub-list contains 2 or 3 parts:
        • The flag (e.g. debug)
        • The default value (e.g. 0), only if the parameter takes an argument (flag ends with .arg).
        • And the help message
        • 我一直在考虑用户调用帮助时的消息输出(请参阅上面运行的最后一个示例)。要解决此问题,您需要自己捕获错误:

          I have been thinking about the message output when the user invoke help (see the last sample run above). To get around that, you need to trap the error yourself:

          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
          }
          

          示例运行2:

          $ tclsh simple.tcl -?
          simple - A simple script to demo cmdline parsing
           -server value        Which server to search <>
           -debug               Turn on debugging, default=off
           -help                Print this message
           -?                   Print this message
          

          这篇关于TCL中用于解析参数的程序包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆