https://wiki.tcl-lang.org/page/alternative+getopt

alternative getopt

sbron 7-Dec-2008: There are several pieces of code available (on the wiki,
in tcllib) for parsing command lines, but they all feel a little awkward
to me. For that reason I came up with the code below. It allows the user
to specify the allowed command line options in a similar fashion to the
switch command. Only in this case there's an implied loop that repeats
as long as command line options are available.

The switch patterns serve as the specification of which command line
options are allowed. A colon in a long option pattern, or after a
character in a short option pattern, indicates that the option requires
an argument.

Command line options can appear in two forms: long options and
short options. Long options start with -- and may be specified on the
command line as a unique prefix. Short options start with just a single
-. Multiple short options may be specified as a single string of option
characters prefixed with a -. If one of the characters represents an
option that requires an argument, the rest of the string is taken as the
argument. An argument of exactly -- will cause all remaining arguments
to be treated as regular arguments, not as options, even if they start
with a -. A - on its own (usually signifying that input should be taken
from standard input instead of a file) will not be considered an option
but is treated as a regular argument.

At the end of the loop the arglist branch is executed with the arguments
remaining after all options have been processed. It will do this
even if there are no remaining arguments. Two other special branches
are available for handling invalid options: missing and unknown. The
missing branch is executed when there is no argument for an option that
requires an argument. The unknown branch is executed when an unknown or
ambiguous option is encountered. In these last two cases the argument
variable will contain the offending option. Under normal circumstances
it won't be necessary to provide these last two branches as reasonable
defaults are provided by the package (a short description of the problem,
followed by the suggestion to try the --help option).

Unless specified otherwise, the --help option will produce a help message
that is automatically generated from the specified options and the first
line of comments in the body for each option. Any comments at the top of
the file (unless starting with #! or ##) are included in the help message
between the usage line and the explanation of the options. If desired,
the same message can be generated by calling the help command.
