GETFLAGS(8)GETFLAGS(8)

NAME

getflags, usage – command-line parsing for shell scripts

SYNOPSIS

aux/getflags $*

aux/usage

DESCRIPTION

Getflags parses the flags in its command-line arguments according to the environment variable $flagfmt. This variable should be a comma-separated list of flag specifiers. Each flag is a single letter, optionally followed by a colon and a name. It may be followed by a space-separated list of argument names.

Getflags prints an rc(1) script to be evaluated by the calling program. For every flag specified in $flagfmt, the generated script sets a corresponding environment variable. If the flag specifier contains :name, the corresponding variable is named $name. Otherwise, it is named $flagx.

After evaluating the script, the environment variables will be set as follows: If a flag is not present in the argument list, the environment variable will default to the empty list. If the flag is present and takes no arguments, the environment variable will be initialized with the string ’1’. If the flag takes arguments, the flag’s variable will be initialized with a list of those argument values. The script then sets the variable $* to the list of remaining non-flag arguments.

The $status is variable to the empty string on success, or ’usage’ when there is an error parsing the command line.

Usage prints a usage message to standard error. The message is constructed using $0, $flagfmt, and $args. The program name is taken from $0, as set by rc(1) The list of flags is extracted from $flagfmt. The description of positional argument list is taken from $args.

EXAMPLE

An example of the script generated:

 

% flagfmt='e:example, x, a:arg with args'
% aux/getflags -exa arg list positional stuff
example=()
flagx=()
arg=()
example=1
flagx=1
arg=(arg list)
*=(positional stuff)
status=''

Parse the arguments for leak(1):

 

flagfmt='b:showbmp, s:acidfmt, f binary, r res, x width'
args='name | pid list'
if(! ifs=() eval `{aux/getflags $*} || ~ $#* 0){
	aux/usage
	exit usage
}
if(~ $#showbmp 0)
	echo '-b flag not set'
echo $showbmp	# named
echo $acidfmt	# also named
echo $flagf	# default name
echo $flagr	# default name

SOURCE

/sys/src/cmd/aux/getflags.c
/sys/src/cmd/aux/usage.c

SEE

arg(2)