1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
#!/bin/sh
# \
exec tclsh8.3 "$0" ${1+"$@"}
########################################################################
#
# An example program and kind of a manual for ::clig.
#
# (C) 1999-2001 Harald Kirsch (kirschh@lionbioscience.com)
#
# $Revision: 1.3 $, $Date: 2001/01/21 15:06:08 $
########################################################################
package require clig
namespace import ::clig::\[A-Za-z\]*
########################################################################
##
## Declare the command line of this script. All except the `Rest'
## entry are only examples and serve no real purpose.
##
## Dictated by backwards compatibility, the declaration-functions
## below cannot take the name of their database (tcl-array) as a
## parameter. It has to be set globally, and it must be a
## namespace-absolute.
setSpec ::main
## Declare a usage-oneliner for this script.
Usage {
Demonstrate some features of clig and show usage of functions in
package clig. All options are just for playing with them. But you
can also request a usage-message for the functions of clig.
}
## Declare option -s to accept between 4 and 7 args
String -s sort \
{will print its args lsorted} \
-c 4 7
## Declare option -l to accept exactly 6 integer args in the range [1,49]
Int -l lotto \
{tell me your lotto numbers} \
-c 6 6 \
-r 1 49
## Declare option -pi to accept exactly one float arg in the range
## [3.1,3.2] with a default of 3.14.
Float -pi pi \
{you may specify your private version of the magic number pi} \
-c 1 1 \
-r 3.1 3.2 \
-d 3.14
## Allow for at most one command line argument not being an argument
## for one of the options declared.
Rest func \
"show usage for clig-function func, where func is one of
Flag, String, Float, Int, Rest, Usage or parseCmdline" \
-c 0 1
## a convenience shortcut
set Program [file tail $argv0]
## parse the command line along the specification (main). Use $Program
## to tag error-message, if necessary.
if {[catch {parseCmdline main $Program $argc $argv} err]} {
#puts stderr $errorInfo
puts stderr $err
exit 1
}
## Run little useless code sippets as requested on the command line
if {[info exist sort]} {
puts "-s: [lsort $sort]"
}
if {[info exist fun]} {
puts "-f: yes, we're having fun"
}
if {[info exist lotto]} {
puts "-l: thanks for choosing $lotto"
}
if {![info exist func]} {
puts "Ok, obviously pi=$pi"
exit 0
}
## Obviously $func is set. Print its usage-message. Of course this
## does not work for functions not instrumented.
switch -exact -- $func {
Flag {
catch {::clig::usage ::clig::FlagSpec ::clig::Flag {}} err
}
parseCmdline {
catch {::clig::usage ::clig::parseCmdlineSpec \
::clig::parseCmdline {}} err
}
Usage {
catch {::clig::usage ::clig::UsageSpec \
::clig::Usage {}} err
}
default {
setSpec ::dummy
if {[info exist ::clig::${func}Spec]} {
catch {$func d blurb adf -h} err
} else {
set err "don't know anything about `$func'"
}
}
}
if {[regexp {usage.*} $err msg]} {
puts $msg
} else {
puts stderr $err
}
########################################################################
## Local Variables: ##
## mode:tcl ##
## End: ##
|