2011-03-30 13:49:47 +00:00
|
|
|
.TH GETFLAGS 8
|
|
|
|
.SH NAME
|
|
|
|
getflags, usage \- command-line parsing for shell scripts
|
|
|
|
.SH SYNOPSIS
|
|
|
|
.B aux/getflags $*
|
|
|
|
.PP
|
|
|
|
.B aux/usage
|
|
|
|
.SH DESCRIPTION
|
|
|
|
.I Getflags
|
2020-04-18 22:38:38 +00:00
|
|
|
parses the flags in its command-line arguments
|
2011-03-30 13:49:47 +00:00
|
|
|
according to the environment variable
|
|
|
|
.BR $flagfmt .
|
2020-04-18 22:38:38 +00:00
|
|
|
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.
|
|
|
|
|
|
|
|
.PP
|
2011-03-30 13:49:47 +00:00
|
|
|
.I Getflags
|
|
|
|
prints an
|
|
|
|
.IR rc (1)
|
2020-04-18 22:38:38 +00:00
|
|
|
script to be evaluated by the calling program.
|
|
|
|
For every flag specified in
|
|
|
|
.BR $flagfmt ,
|
|
|
|
the generated script sets a corresponding environment variable.
|
|
|
|
If the flag specifier contains
|
|
|
|
.BR :name ,
|
|
|
|
the corresponding variable is named
|
|
|
|
.BR $name .
|
|
|
|
Otherwise, it is named
|
|
|
|
.BI $flag x.
|
|
|
|
|
|
|
|
.PP
|
|
|
|
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
|
|
|
|
.BR '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
|
2011-03-30 13:49:47 +00:00
|
|
|
.B $*
|
2020-04-18 22:38:38 +00:00
|
|
|
to the list of remaining non-flag arguments.
|
|
|
|
.PP
|
|
|
|
The
|
2011-03-30 13:49:47 +00:00
|
|
|
.B $status
|
2020-04-18 22:38:38 +00:00
|
|
|
is variable to the empty string on success, or
|
|
|
|
.B 'usage'
|
2011-03-30 13:49:47 +00:00
|
|
|
when there is an error parsing the command line.
|
|
|
|
.PP
|
|
|
|
.I Usage
|
|
|
|
prints a usage message to standard error.
|
2020-04-18 22:38:38 +00:00
|
|
|
The message is constructed using
|
|
|
|
.BR $0 ,
|
2011-03-30 13:49:47 +00:00
|
|
|
.BR $flagfmt ,
|
|
|
|
and
|
2020-04-18 22:38:38 +00:00
|
|
|
.BR $args .
|
|
|
|
The program name is taken from
|
2011-03-30 13:49:47 +00:00
|
|
|
.BR $0 ,
|
2020-04-18 22:38:38 +00:00
|
|
|
as set by
|
|
|
|
.IR rc (1)
|
|
|
|
The list of flags is extracted from
|
|
|
|
.BR $flagfmt .
|
|
|
|
The description of positional argument list is taken from
|
|
|
|
.BR $args .
|
|
|
|
|
2011-03-30 13:49:47 +00:00
|
|
|
.SH EXAMPLE
|
2020-04-18 22:38:38 +00:00
|
|
|
.PP
|
|
|
|
An example of the script generated:
|
|
|
|
.IP
|
|
|
|
.EX
|
2020-05-06 22:10:09 +00:00
|
|
|
% flagfmt='e:example, x, a:arg with args'
|
2020-04-18 22:38:38 +00:00
|
|
|
% aux/getflags -exa arg list positional stuff
|
|
|
|
example=()
|
|
|
|
flagx=()
|
|
|
|
arg=()
|
|
|
|
example=1
|
|
|
|
flagx=1
|
|
|
|
arg=(arg list)
|
|
|
|
*=(positional stuff)
|
|
|
|
status=''
|
|
|
|
.EE
|
|
|
|
.PP
|
2011-03-30 13:49:47 +00:00
|
|
|
Parse the arguments for
|
|
|
|
.IR leak (1):
|
|
|
|
.IP
|
|
|
|
.EX
|
2020-05-06 22:10:09 +00:00
|
|
|
flagfmt='b:showbmp, s:acidfmt, f binary, r res, x width'
|
2011-03-30 13:49:47 +00:00
|
|
|
args='name | pid list'
|
|
|
|
if(! ifs=() eval `{aux/getflags $*} || ~ $#* 0){
|
|
|
|
aux/usage
|
|
|
|
exit usage
|
|
|
|
}
|
2020-04-18 22:38:38 +00:00
|
|
|
if(~ $#showbmp 0)
|
|
|
|
echo '-b flag not set'
|
|
|
|
echo $showbmp # named
|
|
|
|
echo $acidfmt # also named
|
|
|
|
echo $flagf # default name
|
|
|
|
echo $flagr # default name
|
2011-03-30 13:49:47 +00:00
|
|
|
.EE
|
|
|
|
.SH SOURCE
|
|
|
|
.B /sys/src/cmd/aux/getflags.c
|
|
|
|
.br
|
|
|
|
.B /sys/src/cmd/aux/usage.c
|
|
|
|
.SH SEE ALSO
|
|
|
|
.IR arg (2)
|