| 1 |
'\" |
| 2 |
'\" Copyright (c) 2008 Peter Spjuth <[email protected]> |
| 3 |
'\" |
| 4 |
'\" See the file "license.terms" for information on usage and redistribution |
| 5 |
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 6 |
'\" |
| 7 |
.TH prefix n 8.6 Tcl "Tcl Built-In Commands" |
| 8 |
.so man.macros |
| 9 |
.BS |
| 10 |
'\" Note: do not modify the .SH NAME line immediately below! |
| 11 |
.SH NAME |
| 12 |
tcl::prefix \- facilities for prefix matching |
| 13 |
.SH SYNOPSIS |
| 14 |
.nf |
| 15 |
\fB::tcl::prefix all\fR \fItable\fR \fIstring\fR |
| 16 |
\fB::tcl::prefix longest\fR \fItable\fR \fIstring\fR |
| 17 |
\fB::tcl::prefix match\fR \fI?option ...?\fR \fItable\fR \fIstring\fR |
| 18 |
.fi |
| 19 |
.BE |
| 20 |
.SH DESCRIPTION |
| 21 |
.PP |
| 22 |
This document describes commands looking up a prefix in a list of strings. |
| 23 |
The following commands are supported: |
| 24 |
.TP |
| 25 |
\fB::tcl::prefix all\fR \fItable\fR \fIstring\fR |
| 26 |
. |
| 27 |
Returns a list of all elements in \fItable\fR that begin with the prefix |
| 28 |
\fIstring\fR. |
| 29 |
.TP |
| 30 |
\fB::tcl::prefix longest\fR \fItable\fR \fIstring\fR |
| 31 |
. |
| 32 |
Returns the longest common prefix of all elements in \fItable\fR that |
| 33 |
begin with the prefix \fIstring\fR. |
| 34 |
.TP |
| 35 |
\fB::tcl::prefix match\fR ?\fIoption ...\fR? \fItable string\fR |
| 36 |
. |
| 37 |
If \fIstring\fR equals one element in \fItable\fR or is a prefix to exactly |
| 38 |
one element, the matched element is returned. If not, the result depends |
| 39 |
on the \fB\-error\fR option. (It is recommended that the \fItable\fR be sorted |
| 40 |
before use with this subcommand, so that the list of matches presented in the |
| 41 |
error message also becomes sorted, though this is not strictly necessary for |
| 42 |
the operation of this subcommand itself.) The following options are supported: |
| 43 |
.RS |
| 44 |
.TP |
| 45 |
\fB\-exact\fR\0 |
| 46 |
. |
| 47 |
Accept only exact matches. |
| 48 |
.TP |
| 49 |
\fB\-message\0\fIstring\fR |
| 50 |
. |
| 51 |
Use \fIstring\fR in the error message at a mismatch. Default is |
| 52 |
.QW option . |
| 53 |
.TP |
| 54 |
\fB\-error\0\fIoptions\fR |
| 55 |
. |
| 56 |
The \fIoptions\fR are used when no match is found. If \fIoptions\fR is empty, |
| 57 |
no error is generated and an empty string is returned. Otherwise the |
| 58 |
\fIoptions\fR are used as \fBreturn\fR options when generating the error |
| 59 |
message. The default corresponds to setting |
| 60 |
.QW "\-level 0" . |
| 61 |
Example: If |
| 62 |
.QW "\fB\-error\fR {\-errorcode MyError \-level 1}" |
| 63 |
is used, an error would be generated as: |
| 64 |
.RS |
| 65 |
.PP |
| 66 |
.CS |
| 67 |
return \-errorcode MyError \-level 1 \-code error \e |
| 68 |
"ambiguous option ..." |
| 69 |
.CE |
| 70 |
.RE |
| 71 |
.RE |
| 72 |
.SH "EXAMPLES" |
| 73 |
.PP |
| 74 |
Basic use: |
| 75 |
.PP |
| 76 |
.CS |
| 77 |
namespace import ::tcl::prefix |
| 78 |
\fBprefix match\fR {apa bepa cepa} apa |
| 79 |
\fI\(-> apa\fR |
| 80 |
\fBprefix match\fR {apa bepa cepa} a |
| 81 |
\fI\(-> apa\fR |
| 82 |
\fBprefix match\fR \-exact {apa bepa cepa} a |
| 83 |
\fI\(-> bad option "a": must be apa, bepa, or cepa\fR |
| 84 |
\fBprefix match\fR \-message "switch" {apa ada bepa cepa} a |
| 85 |
\fI\(-> ambiguous switch "a": must be apa, ada, bepa, or cepa\fR |
| 86 |
\fBprefix longest\fR {fblocked fconfigure fcopy file fileevent flush} fc |
| 87 |
\fI\(-> fco\fR |
| 88 |
\fBprefix all\fR {fblocked fconfigure fcopy file fileevent flush} fc |
| 89 |
\fI\(-> fconfigure fcopy\fR |
| 90 |
.CE |
| 91 |
.PP |
| 92 |
Simplifying option matching: |
| 93 |
.PP |
| 94 |
.CS |
| 95 |
array set opts {\-apa 1 \-bepa "" \-cepa 0} |
| 96 |
foreach {arg val} $args { |
| 97 |
set opts([\fBprefix match\fR {\-apa \-bepa \-cepa} $arg]) $val |
| 98 |
} |
| 99 |
.CE |
| 100 |
.PP |
| 101 |
Creating a \fBswitch\fR that supports prefixes: |
| 102 |
.PP |
| 103 |
.CS |
| 104 |
switch [\fBprefix match\fR {apa bepa cepa} $arg] { |
| 105 |
apa { } |
| 106 |
bepa { } |
| 107 |
cepa { } |
| 108 |
} |
| 109 |
.CE |
| 110 |
.SH "SEE ALSO" |
| 111 |
lsearch(n), namespace(n), string(n), Tcl_GetIndexFromObj(3) |
| 112 |
.SH "KEYWORDS" |
| 113 |
prefix, table lookup |
| 114 |
'\" Local Variables: |
| 115 |
'\" mode: nroff |
| 116 |
'\" End: |