如何使用连字符标志参数创建 tcl proc [英] how to create tcl proc with hyphen flag arguments

查看:29
本文介绍了如何使用连字符标志参数创建 tcl proc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在整个互联网上搜索,我想我没有搜索到正确的关键字我尝试了其中的大部分 :)

Im searching all over the internet , i guess im searching not the right keywords i tried most of them :)

我想在 tcl/bash 中创建一个带有连字符标志的过程,以从用户那里获取带有标志的参数

i want to create in tcl/bash a proc with hyphen flags to get arguments with flags from the user

例如

proc_name -color red -somethingselse black

推荐答案

其实很容易做到.此代码允许使用缩写的选项名称、标志选项(示例中的 -quxwoo)以及使用 -- 标记或非选项参数停止读取选项的能力出现.在示例中,未知的选项名称会引发错误.传递选项解析循环后,args 包含剩余的命令行参数(不包括 -- 标记,如果它被使用).

It's very easy to do, actually. This code allows abbreviated option names, flag options (-quxwoo in the example) and the ability to stop reading options either with a -- token or with a non-option argument appearing. In the example, unknown option names raise errors. After passing the option-parsing loop, args contains the remaining command-line arguments (not including the -- token if it was used).

proc foo args {
    array set options {-bargle {} -bazout vampires -quxwoo 0}
    while {[llength $args]} {
        switch -glob -- [lindex $args 0] {
            -bar*   {set args [lassign $args - options(-bargle)]}
            -baz*   {set args [lassign $args - options(-bazout)]}
            -qux*   {set options(-quxwoo) 1 ; set args [lrange $args 1 end]}
            --      {set args [lrange $args 1 end] ; break}
            -*      {error "unknown option [lindex $args 0]"}
            default break
        }
    }
    puts "options: [array get options]"
    puts "other args: $args"
}

foo -barg 94 -quxwoo -- abc def
# => options: -quxwoo 1 -bazout vampires -bargle 94
# => other args: abc def

这就是它的工作原理.首先为选项设置默认值:

This is how it works. First set default values for the options:

array set options {-bargle {} -bazout vampires -quxwoo 0}

然后进入处理参数的循环,如果有的话(左).

Then enter a loop that processes the arguments, if there are any (left).

while {[llength $args]} {

在每次迭代中,查看参数列表中的第一个元素:

During each iteration, look at the first element in the argument list:

switch -glob -- [lindex $args 0] {

字符串匹配(glob")匹配用于使具有缩写的选项名称成为可能.

String-match ("glob") matching is used to make it possible to have abbreviated option names.

如果找到值选项,则使用 lassign 将该值复制到 options 数组的相应成员,并删除参数列表中的前两个元素.

If a value option is found, use lassign to copy the value to the corresponding member of the options array and to remove the first two elements in the argument list.

-bar*   {set args [lassign $args - options(-bargle)]}

如果找到标志选项,则将 options 数组的相应成员设置为 1 并删除参数列表中的第一个元素.

If a flag option is found, set the corresponding member of the options array to 1 and remove the first element in the argument list.

-qux*   {set options(-quxwoo) 1 ; set args [lrange $args 1 end]}

如果找到特殊的 -- 标记,则将其从参数列表中删除并退出选项处理循环.

If the special -- token is found, remove it from the argument list and exit the option-processing loop.

--      {set args [lrange $args 1 end] ; break}

如果发现尚未处理的选项名称,则引发错误.

If an option name is found that hasn't already been dealt with, raise an error.

-*      {error "unknown option [lindex $args 0]"}

如果第一个参数与上述任何一个都不匹配,我们似乎已经用完了选项参数:退出循环.

If the first argument doesn't match any of the above, we seem to have run out of option arguments: just exit the loop.

default break

文档:array, break, error, lassign, lindex, llength, proc, puts, 设置, switch, while

Documentation: array, break, error, lassign, lindex, llength, proc, puts, set, switch, while

这篇关于如何使用连字符标志参数创建 tcl proc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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