|
|
|
- NAME
-
getopt, optind, optindc - parse command-line
options in TCL
- SYNOPSIS
-
getopt arglist optstring optret argret
- DESCRIPTION
-
getopt sets the variable optret to
the next option letter in arglist that matches
a letter in optstring. optstring must
contain the option letters the command using
getopt will recognize; if a letter is followed
by a colon, the option is expected to have an argument.
The variable argret will be set to the option
argument, if any.
getopt sets the global variables
optind and optindc to point to the
next option letter in arglist to be processed;
optind holds the index of the option in the
list, and optindc holds the index of the
option letter in the string.
When all options have been processed (that is, up to
the first non-option argument), getopt returns
an empty string. The special option -- may be
used to delimit the end of the options; when it is
encountered, optret will be set to the empty
string, and the -- will be skipped.
If getopt encounters an option in
arglist that is not described in
optstring, or it finds an option with no
argument when the option requires one, it sets
optret to a blank string and argret
to an error message.
getopt returns 1 if an option was
found, 0 if no more options were found, and
-1 if an error occurred.
- EXAMPLE
-
The following script accepts the exclusive options
-a and -b, and the option -o
with an argument.
#!/usr/local/bin/tclsh
set opts(a) 0
set opts(b) 0
set opts(o) ""
proc usage {} {
puts stderr "Usage: $argv0 [ -a | -b ] [ -o <string> ]"
exit 22
}
while { [ set err [ getopt $argv "abo:" opt arg ]] } {
if { $err < 0 } then {
puts stderr "$argv0: $arg"
usage
} else {
switch -exact $opt {
a {
if { $found(b) } then {
puts stderr "$argv0: Only one of -a and -b may be specified!"
usage
} else {
set found(a) 1
}
}
b {
if { $found(a) } then {
puts stderr "$argv0: Only one of -a and -b may be specified!"
usage
} else {
set found(b) 1
}
}
o {
set found(o) $optarg
}
}
}
}
set argv [ lrange $argv $optind end ]
if { $found(a) } then {
puts stdout "Found option -a"
}
if { $found(b) } then {
puts stdout "Found option -b"
}
if { [ string length $found(o) ] } then {
puts stdout "Found option -o:
}
puts -nonewline stdout "The rest of the arguments are: "
set prefix ""
foreach arg $argv {
puts -nonewline stdout "$prefix
set prefix ", "
}
puts stdout ""
- SEE ALSO
-
getopt(3C),
typedopts
http://www.waxandwane.com/toolbox/tclGetOpts/
- AUTHOR
-
Ross Mohn,
rpmohn@panix.com
Johnson
Earls
- WARNING
-
Changing the value of the variable optind, or
calling getopt with a different
arglist, may lead to unexpected results.
|