2 <title>arg(3) - Plan 9 from User Space</title>
3 <meta content="text/html; charset=utf-8" http-equiv=Content-Type>
6 <table border=0 cellpadding=0 cellspacing=0 width=100%>
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>
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 – process
17 option letters from argv<br>
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 <u.h><br>
25 #include <libc.h>
26 <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
28 <tt><font size=+1>ARGBEGIN { <br>
30 char *EARGF(code); <br>
34 <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
36 <tt><font size=+1>extern char *argv0; <br>
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>−−</font></tt>, before an argument <tt><font size=+1>−</font></tt>, or
48 before an argument that doesn’t begin with <tt><font size=+1>−</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,
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
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>
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
79 <table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td>
81 <tt><font size=+1>#include <u.h><br>
82 #include <libc.h><br>
84 main(int argc, char *argv[])<br>
87 <table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td>
90 print("%s", argv[0]);<br>
93 print(" −b");<br>
96 print(" −f(%s)", (f=ARGF())? f: "no arg");<br>
99 print(" badflag('%c')", ARGC());<br>
101 print(" %d args:", argc);<br>
103 print(" '%s'", *argv++);<br>
104 print("\n");<br>
110 <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
114 Here is the output from running the command <tt><font size=+1>prog −bffile1 −r −f
117 <table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td>
119 prog −b −f(file1) badflag('r') −f(file2) 2 args: 'arg1' 'arg2'
121 <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
125 <table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td>
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>
144 <table border=0 cellpadding=0 cellspacing=0 width=100%>
145 <tr height=15><td width=10><td><td width=10>
148 <a href="../../"><img src="../../dist/spaceglenda100.png" alt="Space Glenda" border=1></a>