On Sat, Jul 31, 2021 at 11:57 AM Paul Winalski <paul.winalski(a)gmail.com>
wrote:
On 7/31/21, Michael Siegel <msi(a)malbolge.net>
wrote:
While doing that, I learned that there is a better way to approach
this problem – beyond using getopt(s) (which never really made sense to
me) and having to write case statements in loops every time: Define a
grammar, let a pre-built parser do the work, and have the parser
provide the results to the program.
This method for handling command lines dates back at least to the
1970s. The COMND JSYS (system call) in TOPS-20 operated this way, as
does the DCL command line interface in OpenVMS. As you pointed out it
can greatly simplify the code in the application. It also permits
command completion. If the command has a long-winded option, such as
-supercalifragilisticexpialidocious, I can type -super then hit the
TAB key and as long as there is only one option that starts with
-super the parser will fill in the rest of the long keyword. It also
means that you can provide interactive help. At any point the user
can type a question mark and the command interpreter will say what
syntactic element is expected next. The TOPS-20 COMND JSYS
implemented both of these features, and I think that command
completion was eventually added to the VMS command interpreter, too.
This method of command line parsing also enforces a degree of
uniformity of syntax between the command lines of the various
utilities and applications.
There was someone posting here on TUHS a while back about leveraging a
special context-sensitive `--shell-help` or similar command line program
and synthesizing a protocol between the shell and a program to provide
TOPS-20 like command completion. It was nowhere near what you get from the
COMND JSYS, but seemed like a reasonable approximation.
This is verging on COFF territory, but one of the reasons such a mechanism
is unlike what you get from TOPS-20 is that, in that system, as soon as you
type the name of a command, you're effectively running that command; the
process model is quite different from that of Unix.
With respect to command line handling in general, I think there are some
attempts at making things more rational available in modern languages.
Command line parsing packages for Go and the `clap` package for Rust come
to mind (
https://rust-lang-nursery.github.io/rust-cookbook/cli/arguments.html) I've
used clap recently in a few places and it's very convenient.
- Dan C.