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 in table that begin with the prefix string. that's it. that's the whole vibe.
  • ::tcl::prefix longest table string β€” she returns the longest common prefix of all the elements in table that begin with the prefix string. 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: if string equals one element in table, 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 β€” use string in the error message when there's a mismatch. the default is "option".
  • -error options β€” these options get used when no match is found. if options is empty, then no error is generated at all and you just get an empty string back. otherwise, the options are 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
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms