summaryrefslogtreecommitdiff
path: root/man/1/sh-arg
blob: 17f3d5055268b0f234c59f20b2f73a0ef9f13089 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
.TH SH-ARG 1
.SH NAME
arg \- shell command-line argument parsing
.SH SYNOPSIS
.B load arg
.br
.B arg
[
.I opts command
]...
.B -
.I args
.SH DESCRIPTION
.I Arg
is a loadable module for
.IR sh (1)
that parses command-line arguments in the same
form as
.IR arg (2).
It accepts a list of
.RI ( opts ,\  command )
pairs, where
each character in
.I opts
is an acceptable option, and
.I command
is a shell command to be run if any character
in
.I opts
is found.
Any trailing plus
.RB ( + )
characters in
.I opts
cause
.I arg
to extract the same number of arguments associated
with the option before running
.IR command .

For the duration of
.IR command ,
the environment variable
.B $opt
will be set to the option that has been found,
and 
.B $arg
will be set to the option's arguments (if the correct number
of arguments have been extracted;
otherwise a message will be printed, and a
.B usage
exception raised).
The option character asterisk
.RB ( * )
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
.B usage
exception raised.
.PP
The list of option specifications is terminated with a single
minus
.RB ( - );
the arguments to be parsed follow this.
When the argument parsing has finished
the environment variable
.B $*
is set to the remaining list of arguments.
.SH EXAMPLE
The following shell script,
.BR script ,
takes options
.BR b ,
.B c
and
.BR f ,
where
.B f
takes a file name argument.
.EX
#!/dis/sh
load arg
bflag := cflag := 0
file  := ()
args  := $*
(arg 
    bc  {$opt^flag = 1}
    f+  {file=$arg}
    r++++ {rect=$arg}
    '*' {echo unknown option $opt}
    - $args
)
echo $0 $bflag $cflag $file
echo rect $rect
echo arguments are $*
.EE
.PP
When invoked as follows:
.IP
.B "script -bc -r 0 10 50 100 -ffile a b c"
.PP
the output is:
.IP
.EX
\&./script 1 1 file
rect 0 10 50 100
arguments are a b c
.EE
.PP
and when invoked by:
.IP
.B "script -b -f file -z -- -bc"
.PP
the output is:
.IP
.EX
unknown option z
\&./script 1 0 file
arguments are -bc
.EE
.SH SOURCE
.B /appl/cmd/sh/arg.b
.SH SEE ALSO
.IR sh (1),
.IR arg (2),
.IR sh-std (1)