Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用选项创建Tcl过程?_Tcl_Procedure_Options - Fatal编程技术网

如何使用选项创建Tcl过程?

如何使用选项创建Tcl过程?,tcl,procedure,options,Tcl,Procedure,Options,我想用一些选项创建tcl过程。我知道带有参数和可选参数的过程,但不知道选项。例如,如果我用以下三种方式调用我的过程arith(-add表示加法,-sub表示减法): 分别输出应为1)15(默认情况下应添加),2)15,3)5 如何在Tcl中编写此过程?我是tcl的新手,请给我推荐一些关于tcl的在线资料或书籍。说到选项,最好使用偶数个参数 -op add -values {10 5} -op sub -values {10 5} 这样,就可以将参数放入数组中,如下所示: array set

我想用一些选项创建tcl过程。我知道带有参数和可选参数的过程,但不知道选项。例如,如果我用以下三种方式调用我的过程arith(-add表示加法,-sub表示减法):

分别输出应为1)15(默认情况下应添加),2)15,3)5


如何在Tcl中编写此过程?我是tcl的新手,请给我推荐一些关于tcl的在线资料或书籍。

说到选项,最好使用偶数个参数

 -op add -values {10 5}
 -op sub -values {10 5}
这样,就可以将参数放入数组中,如下所示:

array set aArgs $args
其中,
args
只是传递给过程的参数

proc arith {args} {
        if {[catch {array set aArgs $args} err]} {
            puts "Error : $err"
             return 0
        }
    if {![info exists aArgs(-op)] || ![info exists aArgs(-values)] || [llength $aArgs(-values)]!=2} {
        puts "Please pass valid args"
        return 0
    }
    set x [lindex $aArgs(-values) 0]
    set y [lindex $aArgs(-values) 1]
    switch $aArgs(-op) { 
        "add" { 
            puts [expr {$x+$y}]
        }
        "sub" { 
            puts [expr {$x-$y}]     
        }
    }
}
arith -op add -values {10 5}

复杂参数解析可以使用完成,它是Tcllib的一部分。关键命令是
::cmdline::getoptions
,它从变量中提取选项并返回描述它们的字典。它还修改变量,使其仅包含剩余的参数

package require cmdline
proc arith args {
    set options {
        {op.arg "add" "operation to apply (defaults to 'add')"}
    }
    array set parsed [::cmdline::getoptions args $options]
    if {[llength $args] != 2} {
        return -code error "wrong # args: must be \"arith ?-op operation? x y\""
    }
    switch $parsed(op) {
        add {return [::tcl::mathop::+ {*}$args]}
        sub {return [::tcl::mathop::- {*}$args]}
        default {
            return -code error "Unknown -op \"$parsed(op)\": must be add or sub"
        }
    }
}
演示用法(包括一些错误案例):


需要注意的主要一点是,如果希望传递参数,选项描述符将采用选项名称,但不带前导的
-
,并且在末尾带有
.arg

在本例中。。。我假设操作符是第一个或最后一个选项。该值将是
-add
add
。只要做些改变

在计算中有一个优化。。。要使用lrange和::mathop::

proc arith args {
    if {[llength $args] != 3} {
        return -code error "wrong # args: must be \"arith ?-op operation? x y\""
    }
    set isadd [lsearch $args add]
    set issub [lsearch $args sub]
    if {$isadd != -1 && $issub == -1} {
        return [expr [lindex $args 1] + [lindex $args [expr $isadd == 0 ? 2: 0]]]
    }
    if {$issub != -1 && $isadd == -1} {
        return [expr [lindex $args [expr $issub == 0 ? 1: 0]] - [lindex $args [expr $issub == 0 ? 2: 1]]]
    }
    return -code error "Unknown -op must be add or sub"
}
例如:

#arith add 1 2 3
#arith add sub 2
puts [arith add 1 2]
puts [arith 1 2 add]

puts [arith sub 1 2]
puts [arith 1 2 sub]
这两个错误示例被注释掉了,因为我需要一个捕获。。。但这实际上取决于全局以及它的可重用性

proc arith args {
    if {[llength $args] != 3} {
        return -code error "wrong # args: must be \"arith ?-op operation? x y\""
    }
    set isadd [lsearch $args -add]
    set issub [lsearch $args -sub]
    if {$isadd != -1 && $issub == -1} {
        return [::tcl::mathop::+ {*}[lrange $args [expr $isadd == 0] [expr ($isadd == 0) + 1]]]
    }
    if {$issub != -1 && $isadd == -1} {
        return [::tcl::mathop::- {*}[lrange $args [expr $issub == 0] [expr ($issub == 0) + 1]]]
    }
    return -code error "Unknown -op must be add or sub"
}

例如,见。询问者想要完成标签,但不满意,但答案应该对你有帮助。或者,再想想,可能没有。你的计划有点离经叛道。通常在Tcl中使用子命令运算符(arith add…,arith sub…)或常规选项(arith…,arith-operator add…,arith-operator sub…)。不管怎样,上面提到的答案应该可以帮助你开始。谢谢彼得。。您的链接是有用的。考虑在调用解析<代码> > $ARGs<代码>之前调用<代码>数组设置AARGs {AOP添加值{ 0 0 } } /代码>(或类似的)。使用<代码> L指派< /代码>分配给X,我们不能单独采取参数来执行。Like:proc arith{op1 op2}。这不是proc arith{args},而是一个非常好的响应。我必须测试它,看看进程是否在前缀和后缀中工作。使用cmdline软件包有点酷,但实际上对性能等方面不是很理想。当然,问题出在这个问题上。
#arith add 1 2 3
#arith add sub 2
puts [arith add 1 2]
puts [arith 1 2 add]

puts [arith sub 1 2]
puts [arith 1 2 sub]
proc arith args {
    if {[llength $args] != 3} {
        return -code error "wrong # args: must be \"arith ?-op operation? x y\""
    }
    set isadd [lsearch $args -add]
    set issub [lsearch $args -sub]
    if {$isadd != -1 && $issub == -1} {
        return [::tcl::mathop::+ {*}[lrange $args [expr $isadd == 0] [expr ($isadd == 0) + 1]]]
    }
    if {$issub != -1 && $isadd == -1} {
        return [::tcl::mathop::- {*}[lrange $args [expr $issub == 0] [expr ($issub == 0) + 1]]]
    }
    return -code error "Unknown -op must be add or sub"
}