Blob


1 <head>
2 <title>arg(3) - Plan 9 from User Space</title>
3 <meta content="text/html; charset=utf-8" http-equiv=Content-Type>
4 </head>
5 <body bgcolor=#ffffff>
6 <table border=0 cellpadding=0 cellspacing=0 width=100%>
7 <tr height=10><td>
8 <tr><td width=20><td>
9 <tr><td width=20><td><b>ARG(3)</b><td align=right><b>ARG(3)</b>
10 <tr><td width=20><td colspan=2>
11 <br>
12 <p><font size=+1><b>NAME </b></font><br>
14 <table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td>
16 ARGBEGIN, ARGEND, ARGC, ARGF, EARGF, arginit, argopt &ndash; process
17 option letters from argv<br>
19 </table>
20 <p><font size=+1><b>SYNOPSIS </b></font><br>
22 <table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td>
24 <tt><font size=+1>#include &lt;u.h&gt;<br>
25 #include &lt;libc.h&gt;
26 <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
27 </font></tt>
28 <tt><font size=+1>ARGBEGIN { <br>
29 char *ARGF(); <br>
30 char *EARGF(code); <br>
31 Rune ARGC(); <br>
32 } ARGEND <br>
34 <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
35 </font></tt>
36 <tt><font size=+1>extern char *argv0; <br>
37 </font></tt>
38 </table>
39 <p><font size=+1><b>DESCRIPTION </b></font><br>
41 <table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td>
43 These macros assume the names <i>argc</i> and <i>argv</i> are in scope; see
44 <a href="../man3/exec.html"><i>exec</i>(3)</a>. <i>ARGBEGIN</i> and <i>ARGEND</i> surround code for processing program
45 options. The code should be the cases of a C switch on option
46 characters; it is executed once for each option character. Options
47 end after an argument <tt><font size=+1>&#8722;&#8722;</font></tt>, before an argument <tt><font size=+1>&#8722;</font></tt>, or
48 before an argument that doesn&#8217;t begin with <tt><font size=+1>&#8722;</font></tt>.
49 <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
51 The function macro <i>ARGC</i> returns the current option character,
52 as an integer.
53 <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
55 The function macro <i>ARGF</i> returns the current option argument: a
56 pointer to the rest of the option string if not empty, or the
57 next argument in <i>argv</i> if any, or 0. <i>ARGF</i> must be called just once
58 for each option that takes an argument. The macro <i>EARGF</i> is like
59 <i>ARGF</i> but instead of returning zero runs <i>code</i> and, if that
60 returns, calls <a href="../man3/abort.html"><i>abort</i>(3)</a>. A typical value for <i>code</i> is <tt><font size=+1>usage()</font></tt>,
61 as in <tt><font size=+1>EARGF(usage())</font></tt>.
62 <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
64 After <i>ARGBEGIN</i>, <i>argv0</i> is a copy of <tt><font size=+1>argv[0]</font></tt> (conventionally the
65 name of the program).
66 <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
68 After <i>ARGEND</i>, <i>argv</i> points at a zero-terminated list of the remaining
69 <i>argc</i> arguments.<br>
71 </table>
72 <p><font size=+1><b>EXAMPLE </b></font><br>
74 <table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td>
76 This C program can take option <tt><font size=+1>b</font></tt> and option <tt><font size=+1>f</font></tt>, which requires
77 an argument.<br>
79 <table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td>
81 <tt><font size=+1>#include &lt;u.h&gt;<br>
82 #include &lt;libc.h&gt;<br>
83 void<br>
84 main(int argc, char *argv[])<br>
85 {<br>
87 <table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td>
89 char *f;<br>
90 print(&quot;%s&quot;, argv[0]);<br>
91 ARGBEGIN {<br>
92 case 'b':<br>
93 print(&quot; &#8722;b&quot;);<br>
94 break;<br>
95 case 'f':<br>
96 print(&quot; &#8722;f(%s)&quot;, (f=ARGF())? f: &quot;no arg&quot;);<br>
97 break;<br>
98 default:<br>
99 print(&quot; badflag('%c')&quot;, ARGC());<br>
100 } ARGEND<br>
101 print(&quot; %d args:&quot;, argc);<br>
102 while(*argv)<br>
103 print(&quot; '%s'&quot;, *argv++);<br>
104 print(&quot;\n&quot;);<br>
105 exits(nil);<br>
107 </table>
108 }<br>
110 <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
111 </font></tt>
113 </table>
114 Here is the output from running the command <tt><font size=+1>prog &#8722;bffile1 &#8722;r &#8722;f
115 file2 arg1 arg2<br>
117 <table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td>
119 prog &#8722;b &#8722;f(file1) badflag('r') &#8722;f(file2) 2 args: 'arg1' 'arg2'
121 <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
123 </table>
124 </font></tt>
125 <table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td>
129 </table>
131 </table>
132 <p><font size=+1><b>SOURCE </b></font><br>
134 <table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td>
136 <tt><font size=+1>/usr/local/plan9/include/libc.h<br>
137 </font></tt>
138 </table>
140 <td width=20>
141 <tr height=20><td>
142 </table>
143 <!-- TRAILER -->
144 <table border=0 cellpadding=0 cellspacing=0 width=100%>
145 <tr height=15><td width=10><td><td width=10>
146 <tr><td><td>
147 <center>
148 <a href="../../"><img src="../../dist/spaceglenda100.png" alt="Space Glenda" border=1></a>
149 </center>
150 </table>
151 <!-- TRAILER -->
152 </body></html>