1212.SH NAME
1313switch \- Evaluate one of several scripts, depending on a given value
1414.SH SYNOPSIS
15- \fB switch \fR ?\fI options \fR ?\fI string pattern body \fR ?\fI pattern body \fR ...?
15+ \fB switch \fR ?\fI options \fR ?\fI value pattern body \fR ?\fI pattern body \fR ...?
1616.sp
17- \fB switch \fR ?\fI options \fR ?\fI string \fR {\fI pattern body \fR ?\fI pattern body \fR ...?}
17+ \fB switch \fR ?\fI options \fR ?\fI value \fR {\fI pattern body \fR ?\fI pattern body \fR ...?}
1818.BE
1919.SH DESCRIPTION
2020.PP
21- The \fB switch \fR command matches its \fI string \fR argument against each of
21+ The \fB switch \fR command matches its \fI value \fR argument against each of
2222the \fI pattern \fR arguments in order.
23- As soon as it finds a \fI pattern \fR that matches \fI string \fR it
23+ As soon as it finds a \fI pattern \fR that matches \fI value \fR it
2424evaluates the following \fI body \fR argument by passing it recursively
2525to the Tcl interpreter and returns the result of that evaluation.
2626If the last \fI pattern \fR argument is \fB default \fR then it matches
2727anything.
2828If no \fI pattern \fR argument
29- matches \fI string \fR and no default is given, then the \fB switch \fR
29+ matches \fI value \fR and no default is given, then the \fB switch \fR
3030command returns an empty string.
3131.PP
3232If the initial arguments to \fB switch \fR start with \fB \- \fR then
3333they are treated as options
3434unless there are exactly two arguments to \fB switch \fR (in which case the
35- first must the \fI string \fR and the second must be the
35+ first must the \fI value \fR and the second must be the
3636\fI pattern \fR /\fI body \fR list).
3737The following options are currently supported:
3838.\" OPTION: -exact
3939.TP 10
4040\fB \- exact \fR
4141.
42- Use exact matching when comparing \fI string \fR to a pattern. This
42+ Use exact matching when comparing \fI value \fR to a pattern. This
4343is the default.
4444.\" OPTION: -glob
4545.TP 10
4646\fB \- glob \fR
4747.
48- When matching \fI string \fR to the patterns, use glob-style matching
48+ When matching \fI value \fR to the patterns, use glob-style matching
4949(i.e. the same as implemented by the \fB string match \fR command).
50+ .\" OPTION: -integer
51+ .TP 10
52+ \fB \- integer \fR
53+ .VS 9.1
54+ .\" TIP #730
55+ When matching \fI value \fR to the patterns, use integer comparisons. Note
56+ that this makes using a non-integer \fI value \fR or \fI pattern \fR (other
57+ than a final \fB default \fR ) into an error.
58+ .VE 9.1
5059.\" OPTION: -regexp
5160.TP 10
5261\fB \- regexp \fR
5362.
54- When matching \fI string \fR to the patterns, use regular
63+ When matching \fI value \fR to the patterns, use regular
5564expression matching
5665(as described in the \fB re_syntax \fR reference page).
5766.\" OPTION: -nocase
5867.TP 10
5968\fB \- nocase \fR
6069.
6170Causes comparisons to be handled in a case-insensitive manner.
71+ Not supported with the \fB \- integer \fR option.
6272.\" OPTION: -matchvar
6373.TP 10
6474\fB \- matchvar \fI varName \fR
@@ -67,7 +77,7 @@ This option (only legal when \fB\-regexp\fR is also specified)
6777specifies the name of a variable into which the list of matches
6878found by the regular expression engine will be written. The first
6979element of the list written will be the overall substring of the input
70- string (i.e. the \fI string \fR argument to \fB switch \fR ) matched, the
80+ string (i.e. the \fI value \fR argument to \fB switch \fR ) matched, the
7181second element of the list will be the substring matched by the first
7282capturing parenthesis in the regular expression that matched, and so
7383on. When a \fB default \fR branch is taken, the variable will have the
@@ -84,7 +94,7 @@ found by the regular expression engine will be written. The first
8494element of the list written will be a two-element list specifying the
8595index of the start and index of the first character after the end of
8696the overall substring of the input
87- string (i.e. the \fI string \fR argument to \fB switch \fR ) matched, in a
97+ string (i.e. the \fI value \fR argument to \fB switch \fR ) matched, in a
8898similar way to the \fB \- indices \fR option to the \fB regexp \fR can
8999obtain. Similarly, the second element of the list refers to the first
90100capturing parenthesis in the regular expression that matched, and so
@@ -96,7 +106,7 @@ time as the \fB\-matchvar\fR option.
96106\fB \- \| \- \fR
97107.
98108Marks the end of options. The argument following this one will
99- be treated as \fI string \fR even if it starts with a \fB \- \fR .
109+ be treated as \fI value \fR even if it starts with a \fB \- \fR .
100110This is not required when the matching patterns and bodies are grouped
101111together in a single argument.
102112.PP
@@ -184,6 +194,26 @@ exactly matched is easily obtained using the \fB\-matchvar\fR option:
184194 }
185195}
186196.CE
197+ .PP
198+ .VS 9.1
199+ Deciding what to do with a procedure based on the number of arguments:
200+ .PP
201+ .CS
202+ proc example args {
203+ \fB switch \fR -integer -- [llength $args] {
204+ 0 {
205+ puts "no arguments"
206+ }
207+ 1 {
208+ puts "one argument: [lindex $args 0]"
209+ }
210+ default {
211+ puts "many arguments: $args"
212+ }
213+ }
214+ }
215+ .CE
216+ .VE 9.1
187217.SH "SEE ALSO"
188218for(n), if(n), regexp(n)
189219.SH KEYWORDS
0 commit comments