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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
<title>tclGetOpts - getopt</title>
</head>
<body>
<table cellpadding='0' cellspacing='0' border='0' width='535'>
<tr>
<td class='body' width='535' valign='top'>
<h3>tclGetOpts - getopt</h3>
<dl>
<dt>NAME</dt>
<dd><tt>getopt, optind, optindc</tt> - parse command-line options in TCL
<p></p></dd>
<dt>SYNOPSIS</dt>
<dd><tt>getopt arglist optstring optret argret</tt>
<p></p></dd>
<dt>DESCRIPTION</dt>
<dd><tt>getopt</tt> sets the variable <tt>optret</tt> to the next option letter in
<tt>arglist</tt> that matches a letter in <tt>optstring</tt>. <tt>optstring</tt> must
contain the option letters the command using <tt>getopt</tt> will
recognize; if a letter is followed by a colon, the option is
expected to have an argument. The variable <tt>argret</tt> will be
set to the option argument, if any.
<p><tt>getopt</tt> sets the global variables <tt>optind</tt> and <tt>optindc</tt> to point
to the next option letter in <tt>arglist</tt> to be processed; <tt>optind</tt>
holds the index of the option in the list, and <tt>optindc</tt> holds
the index of the option letter in the string.</p>
<p>When all options have been processed (that is, up to the
first non-option argument), <tt>getopt</tt> returns an empty string.
The special option <tt>--</tt> may be used to delimit the end of
the options; when it is encountered, <tt>optret</tt> will be set to
the empty string, and the <tt>--</tt> will be skipped.</p>
<p>If <tt>getopt</tt> encounters an option in <tt>arglist</tt> that is not
described in <tt>optstring</tt>, or it finds an option with no argument when the option requires one, it sets <tt>optret</tt> to a blank
string and <tt>argret</tt> to an error message.</p>
<p><tt>getopt</tt> returns <tt>1</tt> if an option was found, <tt>0</tt> if no more
options were found, and <tt>-1</tt> if an error occurred.</p>
<p></p></dd>
<dt>EXAMPLE</dt>
<dd>The following script accepts the exclusive options <tt>-a</tt> and <tt>-b</tt>, and the option <tt>-o</tt> with an argument.
<pre> #!/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 ""</pre>
<p></p></dd>
<dt>SEE ALSO</dt>
<dd><tt><a href="http://www.cse.psu.edu/cgi-bin/man2html?cgi_command=getopt(3C)">getopt(3C)</a>, <a href="typedopts.html">typedopts</a></tt><br>
<a href="http://www.waxandwane.com/toolbox/tclGetOpts/">http://www.waxandwane.com/toolbox/tclGetOpts/</a>
<p></p></dd>
<dt>AUTHOR</dt>
<dd><a href="mailto:RPMohn@panix.com?Subject=tclGetOpts - getopt">Ross Mohn, RPMohn@panix.com</a><br>
<a href="mailto:darkfox@springhaven.org">Johnson Earls</a>
<p></p></dd>
<dt>WARNING</dt>
<dd>Changing the value of the variable <tt>optind</tt>, or calling <tt>getopt</tt>
with a different <tt>arglist</tt>, may lead to unexpected results.
<p></p></dd>
</dl>
</td></tr>
</table>
</body>
</html>
|