aboutsummaryrefslogtreecommitdiff
path: root/man/man3/arg.html
blob: 8ca66c803f251a87607e810a7f2141c2d29c82b3 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<head>
<title>arg(3) - Plan 9 from User Space</title>
<meta content="text/html; charset=utf-8" http-equiv=Content-Type>
</head>
<body bgcolor=#ffffff>
<table border=0 cellpadding=0 cellspacing=0 width=100%>
<tr height=10><td>
<tr><td width=20><td>
<tr><td width=20><td><b>ARG(3)</b><td align=right><b>ARG(3)</b>
<tr><td width=20><td colspan=2>
    <br>
<p><font size=+1><b>NAME     </b></font><br>

<table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td>

    ARGBEGIN, ARGEND, ARGC, ARGF, EARGF, arginit, argopt &ndash; process
    option letters from argv<br>
    
</table>
<p><font size=+1><b>SYNOPSIS     </b></font><br>

<table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td>

    <tt><font size=+1>#include &lt;u.h&gt;<br>
    #include &lt;libc.h&gt; 
    <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
    </font></tt>
    <tt><font size=+1>ARGBEGIN { <br>
    char *ARGF(); <br>
    char *EARGF(code); <br>
    Rune ARGC(); <br>
    } ARGEND <br>
    
    <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
    </font></tt>
    <tt><font size=+1>extern char *argv0; <br>
    </font></tt>
</table>
<p><font size=+1><b>DESCRIPTION     </b></font><br>

<table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td>

    These macros assume the names <i>argc</i> and <i>argv</i> are in scope; see
    <a href="../man3/exec.html"><i>exec</i>(3)</a>. <i>ARGBEGIN</i> and <i>ARGEND</i> 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 <tt><font size=+1>&#8722;&#8722;</font></tt>, before an argument <tt><font size=+1>&#8722;</font></tt>, or
    before an argument that doesn&#8217;t begin with <tt><font size=+1>&#8722;</font></tt>. 
    <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
    
    The function macro <i>ARGC</i> returns the current option character,
    as an integer. 
    <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
    
    The function macro <i>ARGF</i> returns the current option argument: a
    pointer to the rest of the option string if not empty, or the
    next argument in <i>argv</i> if any, or 0. <i>ARGF</i> must be called just once
    for each option that takes an argument. The macro <i>EARGF</i> is like
    <i>ARGF</i> but instead of returning zero runs <i>code</i> and, if that
    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>,
    as in <tt><font size=+1>EARGF(usage())</font></tt>. 
    <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
    
    After <i>ARGBEGIN</i>, <i>argv0</i> is a copy of <tt><font size=+1>argv[0]</font></tt> (conventionally the
    name of the program). 
    <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
    
    After <i>ARGEND</i>, <i>argv</i> points at a zero-terminated list of the remaining
    <i>argc</i> arguments.<br>
    
</table>
<p><font size=+1><b>EXAMPLE     </b></font><br>

<table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td>

    This C program can take option <tt><font size=+1>b</font></tt> and option <tt><font size=+1>f</font></tt>, which requires
    an argument.<br>
    
    <table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td>

        <tt><font size=+1>#include &lt;u.h&gt;<br>
        #include &lt;libc.h&gt;<br>
        void<br>
        main(int argc, char *argv[])<br>
        {<br>
        
        <table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td>

            char *f;<br>
            print(&quot;%s&quot;, argv[0]);<br>
            ARGBEGIN {<br>
            case 'b':<br>
             print(&quot; &#8722;b&quot;);<br>
             break;<br>
            case 'f':<br>
             print(&quot; &#8722;f(%s)&quot;, (f=ARGF())? f: &quot;no arg&quot;);<br>
             break;<br>
            default:<br>
             print(&quot; badflag('%c')&quot;, ARGC());<br>
            } ARGEND<br>
            print(&quot; %d args:&quot;, argc);<br>
            while(*argv)<br>
             print(&quot; '%s'&quot;, *argv++);<br>
            print(&quot;\n&quot;);<br>
            exits(nil);<br>
            
        </table>
        }<br>
        
        <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
        </font></tt>
        
    </table>
    Here is the output from running the command <tt><font size=+1>prog &#8722;bffile1 &#8722;r &#8722;f
    file2 arg1 arg2<br>
    
    <table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td>

        prog &#8722;b &#8722;f(file1) badflag('r') &#8722;f(file2) 2 args: 'arg1' 'arg2'
        
        <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table>
        
    </table>
    </font></tt>
    <table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td>

        
        
    </table>
    
</table>
<p><font size=+1><b>SOURCE     </b></font><br>

<table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td>

    <tt><font size=+1>/usr/local/plan9/include/libc.h<br>
    </font></tt>
</table>

<td width=20>
<tr height=20><td>
</table>
<!-- TRAILER -->
<table border=0 cellpadding=0 cellspacing=0 width=100%>
<tr height=15><td width=10><td><td width=10>
<tr><td><td>
<center>
<a href="../../"><img src="../../dist/spaceglenda100.png" alt="Space Glenda" border=1></a>
</center>
</table>
<!-- TRAILER -->
</body></html>