开发者

How can I safely deal with optional parameters

I am writing a proc to create a header in an output file.

Currently it needs to take an optional parameter, which is a possible comment for the header.

I have ended up coding this as a single optional parameter

proc dump_header { test des开发者_如何学运维cription {comment = ""}}

but would like to know how I can achieve the same using args

proc dump_header { test description args }

It's quite easy to check for args being a single blank parameter ($args == ""), but doesn't cope well if passing multiple parameters - and I need the negative check anyway.


Your proc definition is incorrect (you'd get the error message too many fields in argument specifier "comment = """). Should be:

proc dump_header { test description {comment ""}} {
    puts $comment
}

If you want to use args, you could examine the llength of it:

proc dump_header {test desc args} {
    switch -exact [llength $args] {
        0 {puts "no comment"}
        1 {puts "the comment is: $args"}
        default {
            puts "the comment is: [lindex $args 0]"
            puts "the other args are: [lrange $args 1 end]"
        }
    }
}

You might also want to pass name-value pairs in a list:

proc dump_header {test desc options} {
    # following will error if $options is an odd-length list
    array set opts $options

    if {[info exists opts(comment)]} {
        puts "the comment is: $opts(comment)"
    }
    puts "here are all the options given:"
    parray opts
}
dump_header "test" "description" {comment "a comment" arg1 foo arg2 bar}

Some prefer a combination of args and name-value pairs (a la Tk)

proc dump_header {test desc args} {
    # following will error if $args is an odd-length list
    array set opts $args
    if {[info exists opts(-comment)]} {
        puts "the comment is: $opts(-comment)"
    }
    parray opts
}
dump_header "test" "description" -comment "a comment" -arg1 foo -arg2 bar


I use tcllib's cmdline library to do option parsing.

This is the example from cmdline documentation:

set options {
    {a          "set the atime only"}
    {m          "set the mtime only"}
    {c          "do not create non-existent files"}
    {r.arg  ""  "use time from ref_file"}
    {t.arg  -1  "use specified time"}
}
set usage ": MyCommandName \[options] filename ...\noptions:"
array set params [::cmdline::getoptions argv $options $usage]

if {  $params(a) } { set set_atime "true" }
set has_t [expr {$params(t) != -1}]
set has_r [expr {[string length $params(r)] > 0}]
if {$has_t && $has_r} {
    return -code error "Cannot specify both -r and -t"
} elseif {$has_t} {
    ...
}

So, in your case, you'd just use args in place of argv in the above example.


It should be mentioned explicitly that args is a special word in Tcl that, when used at the end of the argument list, contains a list of all the remaining arguments. If no args are given, then no error is produced (unlike any other variable name, which would be considered a required argument).

I was looking for a way to have functionality similar to python's kwargs (optional key-value pair arguments), and something that works nicely is (similar to Glenn's last example):

proc my_proc {positional_required1 {positional_optional1 "a_string"} args} {
    # Two optional arguments can be given: "opt1" and "opt2"
    if {![string equal $args ""]} {
        # If one or more args is given, parse them or assign defaults.
        array set opts $args
        if {[info exists opts(opt1)]} { set opt1 $opts(opt1) } else { set opt1 0 } 
        if {[info exists opts(op2)]} { set opt2 $opts(opt2) } else { set opt2 -1 }
    } else {
        # If no args are given, assign default values. 
        set op1 0
        set op2 -1
    }   
    # DO STUFF HERE
}

And can be called like:

my_proc "positional_required1_argument" 
# OR
my_proc "positional_required1_argument" "a_string"
# OR
my_proc "positional_required1_argument" "a_string" opt1 7
# OR
my_proc "positional_required1_argument" "a_string" opt1 7 opt2 50
# etc.

A potential downside (as I've currently implemented it) is that if a user passes a non-approved key-value option, there is no error.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜