SH-ARG(1)
NAME
arg − shell command-line argument parsing
SYNOPSIS
load arg
arg [ opts command ]... - args
DESCRIPTION
Arg is a loadable module for sh(1) that parses command-line arguments in the same form as arg(2). It accepts a list of (opts, command) pairs, where each character in opts is an acceptable option, and command is a shell command to be run if any character in opts is found. If the last character in opts is a plus (+), then arg will extract the argument associated with the option before running command. For the duration of command, the environment variable $opt will be set to the option that has been found, and $arg will be set to the option’s argument (if the argument has been extracted and there is such an argument, otherwise it will be set to nil). The option character asterisk (*) matches any option letter (this must be quoted, to avoid the usual special interpretation by the shell). Only one command will be run for any option found; if there is no matching option letter, then a default error message will be printed, and a usage exception raised.
The list of option specifications is terminated with a single minus (-); the arguments to be parsed follow this. When the argument parsing has finished the environment variable $args is set to the remaining list of arguments.
EXAMPLE
The following shell script, script, takes options b, c and f, where f takes a file name argument. #!/dis/sh load arg bflag := cflag := 0 file := () args := $* (arg
bc {$opt^flag = 1}
f+ {file=$arg}
’*’ {echo unknown option $opt}
- $args ) echo $0 $bflag $cflag $file echo arguments are $args
When invoked as follows:
script -bc -ffile a b c
the output is:
./script 1 1 file arguments are a b c
and when invoked by:
script -b -f file -z -- -bc
the output is:
unknown option z ./script 1 0 file arguments are -bc
SOURCE
/appl/cmd/sh/arg.b