GETFLAGS(8)

πŸ“¦ version 6, in arch rolling, nixpkgs unstable

🌸 NAME 🌸

okay bestie, you're on the GETFLAGS(8) page from the System Manager's Manual, and it's giving "command-line parsing for shell scripts" πŸ’…. The stars are getflags and usage, and yes they share a page because they're a package deal. 🫢

getflags, usage β€” command-line parsing for shell scripts.

🫢 SYNOPSIS 🫢

getflags $*

usage [ progname ]

πŸ’… DESCRIPTION πŸ’…

getflags parses the options in its command-line arguments according to the environment variable $flagfmt. That variable should be a list of comma-separated options. Each option can be:

  • a single letter, which means it does not take arguments, or
  • a letter followed by the space-separated names of its arguments.

Then getflags prints an rc(1) script on standard output. That script initializes the environment variable $flagx for every option mentioned in $flagfmt. If the option is not present on the command-line, the script sets that option's flag variable to an empty list. Otherwise, the script sets that option's flag variable with a list containing the option's arguments or, if the option takes no arguments, with the string 1. The script also sets the variable $* to the list of arguments following the options. The final line in the script sets the $status variable: to the empty string on success, and to the string usage when there is an error parsing the command line.

usage prints a usage message to standard error. It creates the message using $flagfmt (as described above), $args β€” which should contain the string to be printed explaining non-option arguments β€” and $0, the program name (see rc(1)). If run under sh(1), which does not set $0, the program name must be given explicitly on the command line.

tiny but important, bestie: that sh(1) situation is real β€” no $0 means you gotta hand over the program name yourself. πŸ’€

🌸 EXAMPLE 🌸

Parse the arguments for leak(1):

flagfmt='b,s,f binary,r res,x width'
args='name | pid list'
if(! ifs=() eval `{getflags $*} || ~ $#* 0){
     usage
     exit usage
}

πŸŽ€ SOURCE πŸŽ€

  • /src/cmd/getflags.c
  • /src/cmd/usage.c

πŸ¦‹ SEE ALSO πŸ¦‹

mwah πŸ’‹ β€” that's the GETFLAGS(8) page from the System Manager's Manual. The footer is literally just GETFLAGS(8) with no date attached, so that's the whole sign-off: bye bestie, go parse those flags slayfully 🫢✨

πŸ“¦ where this page comes from