Search code examples
cgnu

Printing --help text on GNU/Linux with the standard format


On GNU/Linux terminal, when I add --help to commands from GNU packages, I get a help text that is formatted in a very consistent way. These help texts list the options accepted by that command with this format:

option characters, long options ............ [aligned] explanation of the option

For example, this is a part of the help text of the man command:

Usage: man [OPTION...] [SECTION] PAGE...

  -C, --config-file=FILE     use this user configuration file
  -d, --debug                emit debugging messages
  -D, --default              reset all options to their default values
      --warnings[=WARNINGS]  enable warnings from groff

 Main modes of operation:
  -f, --whatis               equivalent to whatis
  -k, --apropos              equivalent to apropos
  -K, --global-apropos       search for text in all pages
  -l, --local-file           interpret PAGE argument(s) as local filename(s)
  -w, --where, --path, --location
                             print physical location of man page(s)
  -W, --where-cat, --location-cat
                             print physical location of cat file(s)

I was wondering if there was a standard way (possibly used by GNU packages) to print with the same format, without having to deal with tab stops etc. I have found getopt from POSIX API to parse these options, but I can't find a way to print a list of them.


Solution

  • The GNU tools use argp, which is part of the GNU C library. It offers more options than getopt, including help.

    See: https://www.gnu.org/software/libc/manual/html_node/Argp.html