prefix(n)
prefix(n) π β the tcl::prefix era
okay bestie, sit down, hydrate, because today we're talking about tcl::prefix β the Tcl Built-In Commands manual page that is genuinely doing the most for prefix matching. she's in section n, she's iconic, she's lowkey the reason your option parsing isn't a mess. let's get into it π
β¨ NAME β¨
tcl::prefix β facilities for prefix matching π¦
β¨ SYNOPSIS β¨
::tcl::prefix all table string
::tcl::prefix longest table string
::tcl::prefix match ?option ...? table string
β¨ DESCRIPTION β¨
so this whole doc is about commands that go looking up a prefix in a list of strings. main character energy, honestly. here's what's on the menu π
::tcl::prefix all table stringβ bestie this one hands you back a list of all the elements intablethat begin with the prefixstring. that's it. that's the whole vibe.::tcl::prefix longest table stringβ she returns the longest common prefix of all the elements intablethat begin with the prefixstring. like, the shared opening that every single match has in common. chef's kiss.::tcl::prefix match ?option ...? table stringβ the famous one. here's the tea: ifstringequals one element intable, or if it's a prefix to exactly one element, then boom β the matched element gets returned. π
if that's not the case? the result depends on the -error option. no cap.
ngl, it is recommended that you sort the table before using it with this subcommand, so that the list of matches shown off in the error message also comes out sorted. it's not strictly necessary for the subcommand itself to actually work, but it's the polite thing to do, and we love good manners β¨
these are the options she supports, and yes we're going through all of them:
-exactβ accept only exact matches. no partial credit, no vibes-based guessing.-message stringβ usestringin the error message when there's a mismatch. the default is"option".-error optionsβ theseoptionsget used when no match is found. ifoptionsis empty, then no error is generated at all and you just get an empty string back. otherwise, theoptionsare used as return options when generating that error message. the default corresponds to setting-level 0.
example time π β if you use -error {-errorcode MyError -level 1}, an error would be generated like this:
return -errorcode MyError -level 1 -code error \
"ambiguous option ..."
β¨ EXAMPLES β¨
basic use π
namespace import ::tcl::prefix
prefix match {apa bepa cepa} apa
β apa
prefix match {apa bepa cepa} a
β apa
prefix match -exact {apa bepa cepa} a
β bad option "a": must be apa, bepa, or cepa
prefix match -message "switch" {apa ada bepa cepa} a
β ambiguous switch "a": must be apa, ada, bepa, or cepa
prefix longest {fblocked fconfigure fcopy file fileevent flush} fc
β fco
prefix all {fblocked fconfigure fcopy file fileevent flush} fc
β fconfigure fcopy
look at that. she ate and left no crumbs π
simplifying option matching πΈ
array set opts {-apa 1 -bepa "" -cepa 0}
foreach {arg val} $args {
set opts([prefix match {-apa -bepa -cepa} $arg]) $val
}
literally so clean. your $args don't have to be typed out fully, the prefix does the heavy lifting π«Ά
creating a switch that supports prefixes π§
switch [prefix match {apa bepa cepa} $arg] {
apa { }
bepa { }
cepa { }
}
a switch that understands abbreviations?? we love to see it π
β¨ SEE ALSO β¨
β¨ KEYWORDS β¨
prefix, table lookup
okay that's the whole page, bestie π this was Tcl 8.6, from the Tcl Built-In Commands manual set, page prefix(n) β go forth and match your prefixes responsibly, and remember: sorted tables make for sorted error messages, and sorted error messages make for a happy little group chat πβ¨ mwah π
π¦ where this page comes from
- fedora 43: tcl-doc 1:9.0.2-1.fc43 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- fedora 44: tcl-doc 1:9.0.2-1.fc44 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- Copyright (c) 2008 Peter Spjuth <pspjuth@users.sourceforge.net>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms