Below is a template script based on the notions that been introduced earlier. It illustrates:
- Usage of a private namespace to avoid polluting the global namespace (
lshift
is only available inside the namespacefoo
). - Handling of command line arguments (including
-help
and-version
to provide a version of the script). - Usage of
return -error
(orerror
) command to generate Tcl errors when it is needed.namespace eval foo { namespace export myproc variable version 1.0 } proc foo::lshift listVar { upvar 1 $listVar L set r [lindex $L 0] set L [lreplace $L [set L 0] 0] return $r } proc foo::myproc { args } { #------------------------------------------------------- # Process command line arguments #------------------------------------------------------- set error 0 set help 0 set verbose 0 set ports {} # if {[llength $args] == 0} { incr help }; # Uncomment if necessary while {[llength $args]} { set flag [lshift args] switch -exact -- $flag { -p - -ports { set ports [lshift args] } -v - -verbose { set verbose 1 } -h - -help { incr help } -version { variable version return $version } default { if {[string match "-*" $flag]} { puts " ERROR - option '$flag' is not a valid option." incr error } else { puts "ERROR - option '$flag' is not a valid option." incr error } } } } if {$help} { set callerflag [lindex [info level [expr [info level] -1]] 0] # <-- HELP puts [format { Usage: %s [-ports|-p <listOfPorts>] [-verbose|-v] [-version] [-help|-h] Description: xxxxxxxxxxxxxxxxxxx. xxxxxxxxxxxxxxxxxxx. Example: %s -port xxxxxxxxxxxxxxx } $callerflag $callerflag ] # HELP --> return -code ok {} } # Check validity of arguments. Increment $error to generate an error if {$error} { return -code error {Oops, something is not correct} } # Do something return -code ok {} }