Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
865 views
in Technique[技术] by (71.8m points)

how to create tcl proc with hyphen flag arguments

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

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

ex.

proc_name -color red -somethingselse black
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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] {

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

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)]}

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...