1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-03-09 15:50:02 +00:00

libast: optget: improve usage messages, adding help info line

For example, this changes 'typeset -Q' (a bad option) from:

| ksh: typeset: -Q: unknown option
| Usage: typeset [-bflmnprstuxACHS] [-a[type]] [-i[base]] [-E[n]]
|                [-F[n]] [-L[n]] [-M[mapping]] [-R[n]] [-X[n]]
|                [-h string] [-T[tname]] [-Z[n]] [name[=value]...]
|    Or: typeset [ options ] -f [name...]

to:

| ksh: typeset: -Q: unknown option
| Usage: typeset [-bflmnprstuxACHS] [-a[type]] [-i[base]] [-E[n]]
|                [-F[n]] [-L[n]] [-M[mapping]] [-R[n]] [-X[n]]
|                [-h string] [-T[tname]] [-Z[n]] [name[=value]...]
|    Or: typeset -f [name...]
|  Help: typeset [ --help | --man ] 2>&1

src/lib/libast/misc/optget.c: args():
- Revert the changes done in 6916a873 and ae92cd89. The --help and
  --man labels weren't added consistently (they did not show up in
  the example above) whereas they did show up unnecessarily in the
  manual page itself.
- In the usage section and usage messges, only show an [ options ]
  label on the first usage line; don't redundantly repeat on second
  and further ("Or:") lines.
- In usage and --help (but not --man), add a new "Help:" line
  telling the user about the --help and --man options. This
  replaces the reverted changes. Show the 2>&1 redirection as a
  reminder that you need to do this to pipe it into a pager, as
  everything is written to standard error!
- Add some comments clarifying what I think this code does...

src/cmd/ksh93/tests/builtins.sh:
- Update to match changes in getopts usage output.
This commit is contained in:
Martijn Dekker 2021-01-21 00:02:01 +00:00
parent 6445573d24
commit e21a053e19
2 changed files with 34 additions and 14 deletions

View file

@ -907,21 +907,24 @@ function testusage {
actual=$(testusage -\?)
expect='Usage: testusage [-xyz] [ name=value ... ]
Or: testusage [ options ] -y [ name ... ]'
Or: testusage -y [ name ... ]
Help: testusage [ --help | --man ] 2>&1'
[[ $actual == "$expect" ]] || err_exit "getopts: '-?' output" \
"(expected $(printf %q "$expect"), got $(printf %q "$actual"))"
actual=$(testusage --\?x)
expect='Usage: testusage [ options | --help | --man ] [ name=value ... ]
Or: testusage [ options ] -y [ name ... ]
expect='Usage: testusage [ options ] [ name=value ... ]
Or: testusage -y [ name ... ]
Help: testusage [ --help | --man ] 2>&1
OPTIONS
-x, --xylophone Lorem.'
[[ $actual == "$expect" ]] || err_exit "getopts: '--?x' output" \
"(expected $(printf %q "$expect"), got $(printf %q "$actual"))"
actual=$(testusage --help)
expect='Usage: testusage [ options | --help | --man ] [ name=value ... ]
Or: testusage [ options ] -y [ name ... ]
expect='Usage: testusage [ options ] [ name=value ... ]
Or: testusage -y [ name ... ]
Help: testusage [ --help | --man ] 2>&1
OPTIONS
-x, --xylophone Lorem.
-y, --ypsilon Ipsum.
@ -934,8 +937,8 @@ expect='NAME
foo - bar
SYNOPSIS
foo [ options | --help | --man ] [ name=value ... ]
foo [ options | --help | --man ] -y [ name ... ]
foo [ options ] [ name=value ... ]
foo -y [ name ... ]
DESC
Baz.