string::token::shell(n)

πŸ“¦
version 2.0 Β· 2 versions
, in fedora 43, fedora 44, nixos 26.05, nixpkgs unstable

✨ 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 if string is 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:PART
  • S: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 -partial was specified, and only for the last word of the result. if -partial wasn'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