string::token::shell(n)
β¨ string::token::shell β¨
okay bestie so string::token::shell is literally the manual page we're doing here, and it's all about parsing a shell command line. it lives in the tcllib world and honestly? it's giving tiny-but-mighty. π
πΈ NAME πΈ
string::token::shell β parsing of shell command line π
β¨ SYNOPSIS β¨
package require Tcl 8.5 9
package require string::token::shell ?1.3?
package require string::token ?1?
package require fileutil
::string token shell ?-indices? ?-partial? ?--? string
π DESCRIPTION π
so this package hands you one command that takes a line of text and parses it using basic sh-syntax into a list of words. that's it. that's the whole vibe. β¨
and the complete set of procedures is down below, no gatekeeping here π«Ά
::string token shell ?-indices? ?-partial? ?--? string
this command parses the input string while assuming it follows basic sh-syntax. the result? a list of words found in the string. π
if the input doesn't follow the allowed syntax, it throws an error β no cap, it will absolutely not let that slide. π
you can tweak the behaviour by specifying either of the two options, -indices and -partial.
--β when you drop this in, option parsing stops right there. you need this if your input string might start with a dash. in other words, this is pretty much required ifstringis user input. like, don't skip it bestie π-indicesβ when specified, the output isn't a list of words, it's a list of 4-tuples describing the words. each tuple holds the type of the word, its start- and end-indices in the input, and the actual text of the word. π¦
heads up: the length of the word given by the indices can differ from the length of the word in the last element of the tuple. why? because the indices describe the word's extent in the input β including delimiters, intra-word quoting, etc. β whereas the actual text has delimiters stripped, intra-word quoting decoded, etc. it's giving "same word, different fit" π
the possible token types are:
PLAINβ plain word, not quoted.D:QUOTEDβ word is delimited by double-quotes.S:QUOTEDβ word is delimited by single-quotes.D:QUOTED:PARTS:QUOTED:PARTβ like the previous types, but the word has no closing quote, i.e. it's incomplete. these token types can occur if and only if the option-partialwas specified, and only for the last word of the result. if-partialwasn't specified, such incomplete words cause the command to throw an error instead. periodt. π
-partialβ when specified, the parser will accept an incomplete quoted word (i.e. without a closing quote) at the end of the line as valid, instead of throwing an error. it's the "we're still typing, chill" option π«Ά
π the basic shell syntax accepted here
the syntax it accepts is: unquoted, single-quoted and double-quoted words, separated by whitespace. leading and trailing whitespace are possible too, and they get stripped. β¨
shell variables in their various forms? not recognized. sub-shells? also not recognized. nope, not happening. π
as for the recognized forms of words, the detailed spec is right below π
single-quoted word
a single-quoted word begins with a single-quote character, i.e. ' (ASCII 39), followed by zero or more unicode characters that aren't a single-quote, and then it's closed by a single-quote.
the word must be followed by either the end of the string, or whitespace. a word cannot directly follow the word. π«
double-quoted word
a double-quoted word begins with a double-quote character, i.e. " (ASCII 34), followed by zero or more unicode characters that aren't a double-quote, and then it's closed by a double-quote.
but β and this is the plot twist β contrary to single-quoted words, a double-quote can be embedded into the word by prefacing it, i.e. escaping it, i.e. quoting it, with a backslash character \ (ASCII 92). π₯
similarly, a backslash character must be quoted with itself to be inserted literally. yes, backslash is that friend. π
unquoted word
unquoted words are not delimited by quotes, and thus cannot contain whitespace or single-quote characters. π
double-quote and backslash characters can be put into unquoted words, by quoting them like for double-quoted words. slay. β¨
whitespace
whitespace is any unicode space character. this is equivalent to string is space, or the regular expression \s. π
whitespace may occur before the first word, or after the last word. whitespace must occur between adjacent words. no squeezing them together, bestie, they need their space π
π BUGS, IDEAS, FEEDBACK π
okay so real talk: this document, and the package it describes, will undoubtedly contain bugs and other problems. it's not a vibe, but it's the truth. π
please report such in the category textutil of the Tcllib Trackers at [http://core.tcl.tk/tcllib/reportlist]. please also report any ideas for enhancements you may have for either the package and/or the documentation. we want the glow-up π«Άβ¨
when proposing code changes, please provide unified diffs, i.e. the output of diff -u. that's the format, no substitutes.
note further that attachments are strongly preferred over inlined patches. attachments can be made by going to the Edit form of the ticket immediately after its creation, and then using the left-most button in the secondary navigation bar. iykyk π
π KEYWORDS π
bash, lexing, parsing, shell, string, tokenization
πΈ CATEGORY πΈ
Text processing π
and that's a wrap bestie!! π¦ this whole thing comes to you from tcllib, version 1.3, and yeah β the page itself goes by string::token::shell(n). no date on the tin, just vibes and a version number. stay parsing, stay iconic πβ¨π
π¦ where this page comes from
- fedora 43: tcllib 2.0-3.fc43 Β· TCL
- fedora 44: tcllib 2.0-4.fc44 Β· TCL
- nixos 26.05: tcllib 2.0
- nixpkgs unstable: tcllib 2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms