From: 尹剑虹 <yin...@16...> - 2014-11-27 04:28:50
|
tcllib/cmdline: add options format "[-]-opt[=val]" support. Q: Why add --options[=value] support? A: I want to rewrite some tools completed by bash with tcl, but cmdline can not support orig "--option=val" format, so created this patch. --- modules/cmdline/cmdline.tcl | 15 +++++++++++++-- 1 个文件被修改,插入 13 行(+),删除 2 行(-) diff --git a/modules/cmdline/cmdline.tcl b/modules/cmdline/cmdline.tcl index e191b52..4803950 100644 --- a/modules/cmdline/cmdline.tcl +++ b/modules/cmdline/cmdline.tcl @@ -120,8 +120,17 @@ proc ::cmdline::getKnownOpt {argvVar optstring optVar valVar} { set argsList [lrange $argsList 1 end] } - "-*" { + "-*" - + "--*" { set option [string range $arg 1 end] + if [string equal [string range $option 0 0] "-"] {set option [string range $arg 2 end]} + + # support for format: [-]-option=value + set idx [string first "=" $option 1] + if {$idx != -1} { + set _val [string range $option [expr $idx+1] end] + set option [string range $option 0 [expr $idx-1]] + } if {[lsearch -exact $optstring $option] != -1} { # Booleans are set to 1 when present @@ -131,7 +140,9 @@ proc ::cmdline::getKnownOpt {argvVar optstring optVar valVar} { } elseif {[lsearch -exact $optstring "$option.arg"] != -1} { set result 1 set argsList [lrange $argsList 1 end] - if {[llength $argsList] != 0} { + if {[info exists _val]} { + set value $_val + } elseif {[llength $argsList] != 0} { set value [lindex $argsList 0] set argsList [lrange $argsList 1 end] } else { -- 1.7.10.4 |