Debian Codegraph @

doc/switch.n

raw
1 '\"
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 .TH switch n 8.5 Tcl "Tcl Built-In Commands"
9 .so man.macros
10 .BS
11 '\" Note: do not modify the .SH NAME line immediately below!
12 .SH NAME
13 switch \- Evaluate one of several scripts, depending on a given value
14 .SH SYNOPSIS
15 \fBswitch \fR?\fIoptions\fR?\fI string pattern body \fR?\fIpattern body \fR...?
16 .sp
17 \fBswitch \fR?\fIoptions\fR?\fI string \fR{\fIpattern body \fR?\fIpattern body \fR...?}
18 .BE
19 .SH DESCRIPTION
20 .PP
21 The \fBswitch\fR command matches its \fIstring\fR argument against each of
22 the \fIpattern\fR arguments in order.
23 As soon as it finds a \fIpattern\fR that matches \fIstring\fR it
24 evaluates the following \fIbody\fR argument by passing it recursively
25 to the Tcl interpreter and returns the result of that evaluation.
26 If the last \fIpattern\fR argument is \fBdefault\fR then it matches
27 anything.
28 If no \fIpattern\fR argument
29 matches \fIstring\fR and no default is given, then the \fBswitch\fR
30 command returns an empty string.
31 .PP
32 If the initial arguments to \fBswitch\fR start with \fB\-\fR then
33 they are treated as options
34 unless there are exactly two arguments to \fBswitch\fR (in which case the
35 first must the \fIstring\fR and the second must be the
36 \fIpattern\fR/\fIbody\fR list).
37 The following options are currently supported:
38 .TP 10
39 \fB\-exact\fR
40 .
41 Use exact matching when comparing \fIstring\fR to a pattern. This
42 is the default.
43 .TP 10
44 \fB\-glob\fR
45 .
46 When matching \fIstring\fR to the patterns, use glob-style matching
47 (i.e. the same as implemented by the \fBstring match\fR command).
48 .TP 10
49 \fB\-regexp\fR
50 .
51 When matching \fIstring\fR to the patterns, use regular
52 expression matching
53 (as described in the \fBre_syntax\fR reference page).
54 .TP 10
55 \fB\-nocase\fR
56 .
57 Causes comparisons to be handled in a case-insensitive manner.
58 .TP 10
59 \fB\-matchvar\fR \fIvarName\fR
60 .
61 This option (only legal when \fB\-regexp\fR is also specified)
62 specifies the name of a variable into which the list of matches
63 found by the regular expression engine will be written. The first
64 element of the list written will be the overall substring of the input
65 string (i.e. the \fIstring\fR argument to \fBswitch\fR) matched, the
66 second element of the list will be the substring matched by the first
67 capturing parenthesis in the regular expression that matched, and so
68 on. When a \fBdefault\fR branch is taken, the variable will have the
69 empty list written to it. This option may be specified at the same
70 time as the \fB\-indexvar\fR option.
71 .TP 10
72 \fB\-indexvar\fR \fIvarName\fR
73 .
74 This option (only legal when \fB\-regexp\fR is also specified)
75 specifies the name of a variable into which the list of indices
76 referring to matching substrings
77 found by the regular expression engine will be written. The first
78 element of the list written will be a two-element list specifying the
79 index of the start and index of the first character after the end of
80 the overall substring of the input
81 string (i.e. the \fIstring\fR argument to \fBswitch\fR) matched, in a
82 similar way to the \fB\-indices\fR option to the \fBregexp\fR can
83 obtain. Similarly, the second element of the list refers to the first
84 capturing parenthesis in the regular expression that matched, and so
85 on. When a \fBdefault\fR branch is taken, the variable will have the
86 empty list written to it. This option may be specified at the same
87 time as the \fB\-matchvar\fR option.
88 .TP 10
89 \fB\-\|\-\fR
90 .
91 Marks the end of options. The argument following this one will
92 be treated as \fIstring\fR even if it starts with a \fB\-\fR.
93 This is not required when the matching patterns and bodies are grouped
94 together in a single argument.
95 .PP
96 Two syntaxes are provided for the \fIpattern\fR and \fIbody\fR arguments.
97 The first uses a separate argument for each of the patterns and commands;
98 this form is convenient if substitutions are desired on some of the
99 patterns or commands.
100 The second form places all of the patterns and commands together into
101 a single argument; the argument must have proper list structure, with
102 the elements of the list being the patterns and commands.
103 The second form makes it easy to construct multi-line switch commands,
104 since the braces around the whole list make it unnecessary to include a
105 backslash at the end of each line.
106 Since the \fIpattern\fR arguments are in braces in the second form,
107 no command or variable substitutions are performed on them; this makes
108 the behavior of the second form different than the first form in some
109 cases.
110 .PP
111 If a \fIbody\fR is specified as
112 .QW \fB\-\fR
113 it means that the \fIbody\fR
114 for the next pattern should also be used as the body for this
115 pattern (if the next pattern also has a body of
116 .QW \fB\-\fR
117 then the body after that is used, and so on).
118 This feature makes it possible to share a single \fIbody\fR among
119 several patterns.
120 .PP
121 Beware of how you place comments in \fBswitch\fR commands. Comments
122 should only be placed \fBinside\fR the execution body of one of the
123 patterns, and not intermingled with the patterns.
124 .SH "EXAMPLES"
125 .PP
126 The \fBswitch\fR command can match against variables and not just
127 literals, as shown here (the result is \fI2\fR):
128 .PP
129 .CS
130 set foo "abc"
131 \fBswitch\fR abc a \- b {expr {1}} $foo {expr {2}} default {expr {3}}
132 .CE
133 .PP
134 Using glob matching and the fall-through body is an alternative to
135 writing regular expressions with alternations, as can be seen here
136 (this returns \fI1\fR):
137 .PP
138 .CS
139 \fBswitch\fR \-glob aaab {
140 a*b \-
141 b {expr {1}}
142 a* {expr {2}}
143 default {expr {3}}
144 }
145 .CE
146 .PP
147 Whenever nothing matches, the \fBdefault\fR clause (which must be
148 last) is taken. This example has a result of \fI3\fR:
149 .PP
150 .CS
151 \fBswitch\fR xyz {
152 a \-
153 b {
154 # Correct Comment Placement
155 expr {1}
156 }
157 c {
158 expr {2}
159 }
160 default {
161 expr {3}
162 }
163 }
164 .CE
165 .PP
166 When matching against regular expressions, information about what
167 exactly matched is easily obtained using the \fB\-matchvar\fR option:
168 .PP
169 .CS
170 \fBswitch\fR \-regexp \-matchvar foo \-\- $bar {
171 a(b*)c {
172 puts "Found [string length [lindex $foo 1]] 'b's"
173 }
174 d(e*)f(g*)h {
175 puts "Found [string length [lindex $foo 1]] 'e's and\e
176 [string length [lindex $foo 2]] 'g's"
177 }
178 }
179 .CE
180 .SH "SEE ALSO"
181 for(n), if(n), regexp(n)
182 .SH KEYWORDS
183 switch, match, regular expression
184 .\" Local Variables:
185 .\" mode: nroff
186 .\" End: