aboutsummaryrefslogtreecommitdiff
path: root/man/man3/arg.3
blob: d9aa812ff5a958492b15e04b39d4992372ca13c4 (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
.TH ARG 3
.SH NAME
ARGBEGIN, ARGEND, ARGC, ARGF, EARGF, arginit, argopt \- process option letters from argv
.SH SYNOPSIS
.B #include <u.h>
.br
.B #include <libc.h>
.PP
.nf
.B ARGBEGIN {
.B char *ARGF();
.B char *EARGF(code);
.B Rune ARGC();
.B } ARGEND
.PP
.B extern char *argv0;
.SH DESCRIPTION
These macros assume the names
.I argc
and
.I argv
are in scope; see
.MR exec (3) .
.I ARGBEGIN
and
.I ARGEND
surround code for processing program options.
The code
should be the cases of a C switch on
option characters;
it is executed once for each option character.
Options end after an argument 
.BR -- ,
before an argument
.BR - ,
or before an argument that doesn't begin with
.BR - .
.PP
The function macro
.I ARGC
returns the current option character, as an integer.
.PP
The function macro
.I ARGF
returns the current option argument:
a pointer to the rest of the option string if not empty,
or the next argument in
.I argv
if any, or 0.
.I ARGF
must be called just once for each option
that takes an argument.
The macro
.I EARGF
is like
.I ARGF
but instead of returning zero
runs
.I code
and, if that returns, calls
.MR abort (3) .
A typical value for
.I code
is 
.BR usage() ,
as in
.BR EARGF(usage()) .
.PP
After
.IR ARGBEGIN ,
.I argv0
is a copy of
.BR argv[0]
(conventionally the name of the program).
.PP
After
.IR ARGEND ,
.I argv
points at a zero-terminated list of the remaining
.I argc
arguments.
.SH EXAMPLE
This C program can take option
.B b
and option
.BR f ,
which requires an argument.
.IP
.EX
.ta \w'12345678'u +\w'12345678'u +\w'12345678'u +\w'12345678'u +\w'12345678'u
#include <u.h>
#include <libc.h>
void
main(int argc, char *argv[])
{
	char *f;
	print("%s", argv[0]);
	ARGBEGIN {
	case 'b':
		print(" -b");
		break;
	case 'f':
		print(" -f(%s)", (f=ARGF())? f: "no arg");
		break;
	default:
		print(" badflag('%c')", ARGC());
	} ARGEND
	print(" %d args:", argc);
	while(*argv)
		print(" '%s'", *argv++);
	print("\en");
	exits(nil);
}
.EE
.PP
Here is the output from running the command
.B
prog -bffile1 -r -f file2 arg1 arg2
.IP
.B
prog -b -f(file1) badflag('r') -f(file2) 2 args: 'arg1' 'arg2'
.PP
.SH SOURCE
.B \*9/include/libc.h