GETFLAGS(1plan9)
π GETFLAGS π
okay bestie, listen up β this is a plan9 section 1 situation, and we're talking about command-line parsing for shell scripts. two lil utilities: getflags and usage. they are the dynamic duo of not letting your script choke on -x and friends. periodt. π
β¨ SYNOPSIS β¨
getflags $*
usage [ progname ]
π¦ DESCRIPTION π¦
π
getflags
so getflags looks at the options in its command-line arguments and parses them according to the environment variable $flagfmt. it's giving "i read the rules and i follow them". π
$flagfmt needs to be a list of comma-separated options. and each option is one of two vibes:
- a single letter β meaning it does not take arguments
- a letter followed by the space-separated names of its arguments
then getflags prints an rc(1) script on standard output, and that script initializes the environment variable $flagx for every option mentioned in $flagfmt. every single one. no option left behind fr fr.
here's the tea on what that script does per option:
- if the option is not present on the command line β the script sets that option's flag variable to an empty list
- if the option is present β the script sets that option's flag variable to a list containing the option's arguments... or, if the option takes no arguments, to the string
1. slay.
and the script also sets the variable $* to the list of arguments that come after the options. β¨
the final line of the script sets the $status variable:
- empty string on success π
- the string
usagewhen there's an error parsing the command line π
πΈ usage
usage prints a usage message to standard error. dramatic? maybe. necessary? absolutely.
it builds that message out of:
$flagfmt, as described above π$args, which should contain the string to be printed explaining non-option arguments$0, the program name (seerc(1))
β οΈ important bestie: if you're running undersh(1), which does not set$0, then you have to give the program name explicitly on the command line. no shortcuts, no vibes, just type it. π
π EXAMPLE π
let's parse the arguments for leak(1) β iconic behavior honestly:
flagfmt='b,s,f binary,r res,x width'
args='name | pid list'
if(! ifs=() eval `{getflags $*} || ~ $#* 0){
usage
exit usage
}
see how $flagfmt and $args are just... sitting there, being set, doing their thing? that's the whole mood. π
π SOURCE π
/src/cmd/getflags.c
/src/cmd/usage.c
π SEE ALSO π
okay that's the whole page, you ate it, you understood it, you're a shell-scripting main character now π
β¨ this was GETFLAGS(1plan9) from the plan9 manual, section 1 β go forth and parse responsibly, bestie π«ΆπΈ
π¦ where this page comes from
- debian bookworm: 9base 1:6-13 Β· Lucent
- debian trixie: 9base 1:6-14 Β· Lucent
- ubuntu jammy: 9base 1:6-11 Β· Lucent
- ubuntu noble: 9base 1:6-14 Β· Lucent
- ubuntu questing: 9base 1:6-15
- ubuntu resolute: 9base 1:6-15build1 Β· Lucent
- ubuntu stonking: 9base 1:6-15build1 Β· Lucent
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms