1 |
578 |
markom |
'\"
|
2 |
|
|
'\" Copyright (c) 1993 The Regents of the University of California.
|
3 |
|
|
'\" Copyright (c) 1994-1997 Sun Microsystems, Inc.
|
4 |
|
|
'\"
|
5 |
|
|
'\" See the file "license.terms" for information on usage and redistribution
|
6 |
|
|
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
|
7 |
|
|
'\"
|
8 |
|
|
'\" RCS: @(#) $Id: switch.n,v 1.1.1.1 2002-01-16 10:25:25 markom Exp $
|
9 |
|
|
'\"
|
10 |
|
|
.so man.macros
|
11 |
|
|
.TH switch n 7.0 Tcl "Tcl Built-In Commands"
|
12 |
|
|
.BS
|
13 |
|
|
'\" Note: do not modify the .SH NAME line immediately below!
|
14 |
|
|
.SH NAME
|
15 |
|
|
switch \- Evaluate one of several scripts, depending on a given value
|
16 |
|
|
.SH SYNOPSIS
|
17 |
|
|
\fBswitch \fR?\fIoptions\fR?\fI string pattern body \fR?\fIpattern body \fR...?
|
18 |
|
|
.sp
|
19 |
|
|
\fBswitch \fR?\fIoptions\fR?\fI string \fR{\fIpattern body \fR?\fIpattern body \fR...?}
|
20 |
|
|
.BE
|
21 |
|
|
|
22 |
|
|
.SH DESCRIPTION
|
23 |
|
|
.PP
|
24 |
|
|
The \fBswitch\fR command matches its \fIstring\fR argument against each of
|
25 |
|
|
the \fIpattern\fR arguments in order.
|
26 |
|
|
As soon as it finds a \fIpattern\fR that matches \fIstring\fR it
|
27 |
|
|
evaluates the following \fIbody\fR argument by passing it recursively
|
28 |
|
|
to the Tcl interpreter and returns the result of that evaluation.
|
29 |
|
|
If the last \fIpattern\fR argument is \fBdefault\fR then it matches
|
30 |
|
|
anything.
|
31 |
|
|
If no \fIpattern\fR argument
|
32 |
|
|
matches \fIstring\fR and no default is given, then the \fBswitch\fR
|
33 |
|
|
command returns an empty string.
|
34 |
|
|
.PP
|
35 |
|
|
If the initial arguments to \fBswitch\fR start with \fB\-\fR then
|
36 |
|
|
they are treated as options. The following options are
|
37 |
|
|
currently supported:
|
38 |
|
|
.TP 10
|
39 |
|
|
\fB\-exact\fR
|
40 |
|
|
Use exact matching when comparing \fIstring\fR to a pattern. This
|
41 |
|
|
is the default.
|
42 |
|
|
.TP 10
|
43 |
|
|
\fB\-glob\fR
|
44 |
|
|
When matching \fIstring\fR to the patterns, use glob-style matching
|
45 |
|
|
(i.e. the same as implemented by the \fBstring match\fR command).
|
46 |
|
|
.TP 10
|
47 |
|
|
\fB\-regexp\fR
|
48 |
|
|
When matching \fIstring\fR to the patterns, use regular
|
49 |
|
|
expression matching
|
50 |
|
|
(i.e. the same as implemented by the \fBregexp\fR command).
|
51 |
|
|
.TP 10
|
52 |
|
|
\fB\-\|\-\fR
|
53 |
|
|
Marks the end of options. The argument following this one will
|
54 |
|
|
be treated as \fIstring\fR even if it starts with a \fB\-\fR.
|
55 |
|
|
.PP
|
56 |
|
|
Two syntaxes are provided for the \fIpattern\fR and \fIbody\fR arguments.
|
57 |
|
|
The first uses a separate argument for each of the patterns and commands;
|
58 |
|
|
this form is convenient if substitutions are desired on some of the
|
59 |
|
|
patterns or commands.
|
60 |
|
|
The second form places all of the patterns and commands together into
|
61 |
|
|
a single argument; the argument must have proper list structure, with
|
62 |
|
|
the elements of the list being the patterns and commands.
|
63 |
|
|
The second form makes it easy to construct multi-line switch commands,
|
64 |
|
|
since the braces around the whole list make it unnecessary to include a
|
65 |
|
|
backslash at the end of each line.
|
66 |
|
|
Since the \fIpattern\fR arguments are in braces in the second form,
|
67 |
|
|
no command or variable substitutions are performed on them; this makes
|
68 |
|
|
the behavior of the second form different than the first form in some
|
69 |
|
|
cases.
|
70 |
|
|
.PP
|
71 |
|
|
If a \fIbody\fR is specified as ``\fB\-\fR'' it means that the \fIbody\fR
|
72 |
|
|
for the next pattern should also be used as the body for this
|
73 |
|
|
pattern (if the next pattern also has a body of ``\fB\-\fR''
|
74 |
|
|
then the body after that is used, and so on).
|
75 |
|
|
This feature makes it possible to share a single \fIbody\fR among
|
76 |
|
|
several patterns.
|
77 |
|
|
.PP
|
78 |
|
|
Below are some examples of \fBswitch\fR commands:
|
79 |
|
|
.CS
|
80 |
|
|
\fBswitch\0abc\0a\0\-\0b\0{format 1}\0abc\0{format 2}\0default\0{format 3}\fR
|
81 |
|
|
.CE
|
82 |
|
|
will return \fB2\fR,
|
83 |
|
|
.CS
|
84 |
|
|
\fBswitch\0\-regexp\0aaab {
|
85 |
|
|
^a.*b$\0\-
|
86 |
|
|
b\0{format 1}
|
87 |
|
|
a*\0{format 2}
|
88 |
|
|
default\0{format 3}
|
89 |
|
|
}\fR
|
90 |
|
|
.CE
|
91 |
|
|
will return \fB1\fR, and
|
92 |
|
|
.CS
|
93 |
|
|
\fBswitch\0xyz {
|
94 |
|
|
a
|
95 |
|
|
\-
|
96 |
|
|
b
|
97 |
|
|
{format 1}
|
98 |
|
|
a*
|
99 |
|
|
{format 2}
|
100 |
|
|
default
|
101 |
|
|
{format 3}
|
102 |
|
|
}\fR
|
103 |
|
|
.CE
|
104 |
|
|
will return \fB3\fR.
|
105 |
|
|
|
106 |
|
|
.SH KEYWORDS
|
107 |
|
|
switch, match, regular expression
|