File: getopt.3

package info (click to toggle)
manpages-ja 0.4.0.0.20020315-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 14,808 kB
  • ctags: 4
  • sloc: sh: 9,595; perl: 157; makefile: 62
file content (323 lines) | stat: -rw-r--r-- 10,919 bytes parent folder | download
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
.\" (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de)
.\"
.\" Permission is granted to make and distribute verbatim copies of this
.\" manual provided the copyright notice and this permission notice are
.\" preserved on all copies.
.\"
.\" Permission is granted to copy and distribute modified versions of this
.\" manual under the conditions for verbatim copying, provided that the
.\" entire resulting derived work is distributed under the terms of a
.\" permission notice identical to this one
.\" 
.\" Since the Linux kernel and libraries are constantly changing, this
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
.\" responsibility for errors or omissions, or for damages resulting from
.\" the use of the information contained herein.  The author(s) may not
.\" have taken the same level of care in the production of this manual,
.\" which is licensed free of charge, as they might when working
.\" professionally.
.\" 
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
.\" License.
.\" Modified Sat Jul 24 19:27:50 1993 by Rik Faith (faith@cs.unc.edu)
.\" Modified Mon Aug 30 22:02:34 1995 by Jim Van Zandt <jrv@vanzandt.mv.com>
.\"  longindex is a pointer, has_arg can take 3 values, using consistent
.\"  names for optstring and longindex, "\n" in formats fixed.  Documenting
.\"  opterr and getopt_long_only.  Clarified explanations (borrowing heavily
.\"  from the source code).
.\" Modified 8 May 1998 by Joseph S. Myers (jsm28@cam.ac.uk)
.\"
.\" Japanese Version Copyright (c) 1998 NAKANO Takeo all rights reserved.
.\" Translated Wed 12 Aug 1998 by NAKANO Takeo <nakano@apm.seikei.ac.jp>
.\" Updated & Modified Sat Aug 21 1999
.\"       by NAKANO Takeo <nakano@apm.seikei.ac.jp>
.\" Updated & Modified Sun Jul  1 13:13:52 JST 2001
.\"       by Yuichi SATO <ysato@h4.dion.ne.jp>
.\"
.\"WORD:	argv-element		argv 
.\"
.TH GETOPT 3  1998-05-08 "GNU" "Linux Programmer's Manual"
.SH ̾
getopt \- ޥɥ饤󥪥ץ᤹
.SH 
.nf
.B #include <unistd.h>
.sp
.BI "int getopt(int " argc ", char * const " argv[] ,
.BI "           const char *" optstring );
.sp
.BI "extern char *" optarg ;
.BI "extern int " optind ", " opterr ", " optopt ;
.sp
.B #define _GNU_SOURCE
.br
.B #include <getopt.h>
.sp
.BI "int getopt_long(int " argc ", char * const " argv[] ,
.BI "           const char *" optstring ,
.BI "           const struct option *" longopts ", int *" longindex );
.sp
.BI "int getopt_long_only(int " argc ", char * const " argv[] ,
.BI "           const char *" optstring ,
.BI "           const struct option *" longopts ", int *" longindex );
.fi
.SH 
.B getopt()
ؿϥޥɥ饤᤹롣
.B getopt()
Ȥ
.I argc

.I argv
ϡ줾ץεư
.B main()
ؿϤ줿θĿǤ롣
\fIargv\fP ǤΤ `\-' ǻϤޤ ( `\-' ñȤ `\-\-' 
Ϥʤ) ϥץ (option element) Ȥߤʤ롣
Ƭ `\-' ʸϥץʸ (option character) Ȥ롣
\fBgetopt()\fP ϡ֤ƤӽФ뤴ȤˡΥץʸ֤
.PP
ʥץʸ򸫤Ĥȡ \fBgetopt()\fP Ϥʸ֤
ѿ \fIoptind\fP ȥƥåѿ \fInextchar\fP 򹹿롣
ˤäơ \fBgetopt()\fP ϼθƤӽФκݤˡʹߤΥץ
ʸ \fIargv\fP ǤΥ³Ǥ롣
.PP
ץʸʾ師Ĥʤʤȡ \fBgetopt()\fP  
\-1 ֤ \fIoptind\fP ϡ \fIargv\fP ǤΤ
ץǤʤǽǤ򼨤褦ˤʤ롣
.PP
.I optstring
ϼդ륪ץʸʤʸǤ롣ʸΤȤ˥
 (:) ֤ƤϡץˤϰɬפǤ뤳Ȥ
ΤȤ \fBgetopt\fP ϡܤƤ
\fIargv\fP Ǥǡץʸ˰³ƥȤؤΥݥ󥿤
뤤ϼ \fIargv\fP ǤΥƥȤؤΥݥ󥿤
.I optarg
2 Ϣ³ƥ֤ƤϡΥץϰȤäƤ
ȤʤƤ褤ߤ \fIargv\fP Ǥ˥ƥȤС줬 
\fIoptarg\fP ֤롣ʤ \fIoptarg\fP  0 ˤ롣
 GNU ˤĥǤ롣
.I optstring

.B W
Ȥ³ߥäƤȡ
.B \-W foo
Ĺץ
.B \-\-foo
Ʊ褦˰롣
(POSIX.2 
.B \-W
ץ¸γĥȤͽ󤷤Ƥ롣)
ư GNU ˤĥǤꡢ GNU libc 2 Υ饤֥Ǥ
ѤǤʤ
.PP
ǥեȤǤ \fBgetopt()\fP  \fIargv\fP 򥹥󤹤ݤ˽
ץʳǤǸ˰ư롣¾ˤĤΥ⡼ɤ
Ƥ롣 \fIoptstring\fP Ƭʸ `+' Ǥ뤫Ķѿ 
POSIXLY_CORRECT ꤵƤˤϡץоݤȤưϡ
󥪥ץΰ줿ʳǽλ롣 \fIoptstring\fP κǽ
Ƭʸ `\-' ǤˤϡץǤʤ \fIargv\fP Ǥϡ
ʸ 1 ΥץǤ뤫Τ褦˰ (Ѥץϡ
ץ \fIargv\fP ǤǤդνǼ졢Ĥν
̣Ĥ褦˽񤫤Ƥɬפ) `\-\-' üʰǡ
Υ⡼ɤˤ餺ץΥŪ˽λ롣
.PP
ץʸǧʤäˤϡ \fBgetopt()\fP ϥ顼å
ɸϤɽʸ \fIoptopt\fP ¸ `?' ֤
ƤӽФץ \fIopterr\fP  0 ˤƤС顼å
ɽǤ롣
.PP
.B getopt_long()
ؿϡĹץ (ĤΥå `\-\-' ǻϤޤ륪ץ) 
뤳Ȥ
.B getopt()
Ʊ褦ư롣Ĺץ̾ϡ¾ȽŤʤʤϰϤˤ
ṳ̂Ǥ롣뤤줿ץΤ˥ޥåΤǤ
() ޤʤ
Ĺץϰ뤳ȤǤ
.B \-\-arg=param
ޤ
.B "\-\-arg param"
ȸǻꤹ롣
.PP
.I longopts

.B struct option
ǤʤΡƬǤؤΥݥ󥿤Ǥ롣
.B struct option

.B <getopt.h>
ǰʲΤ褦Ƥ롣
.nf
.sp
.in 10
struct option {
.in 14
const char *name;
int has_arg;
int *flag;
int val;
.in 10
};
.fi
.PP
줾Υեɤΰ̣ϰʲ̤ꡣ
.TP
.I name
Ĺץ̾
.TP
.I has_arg
\fBno_argument\fP (ޤ 0) ʤ顢ץϰȤʤ
\fBrequired_argument\fP (ޤ 1) ʤ顢ץϰɬפȤ롣
\fBoptional_argument\fP (ޤ 2) ʤ顢ץϰȤäƤ
ȤʤƤɤ
.TP
.I flag
ĹץФ̤֤ꤹ롣 \fIflag\fP  
\fBNULL\fP ʤ \fBgetopt_long()\fP  \fIval\fP ֤ (㤨иƤ
ФΥץϡ \fIval\fP ʥץʸ뤳
Ǥ) \fBNULL\fP ʳξˤϡ \fBgetopt_long()\fP  0 
ΤȤץ󤬸Ĥ \fIflag\fP ݥȤѿ 
\fIval\fP 롣ĤʤȤѿѹʤ
.TP
\fIval\fP 
֤͡ޤ \fIflag\fP ݥȤѿإɤ͡
.PP
κǸǤϡ 0 ƤʤФʤʤ
.PP
\fIlongindex\fP ϡ \fBNULL\fP ǤʤС
ĹץΥǥå
.I longopts
а֤ȤݻƤѿؤΥݥ󥿤Ȥʤ롣
.PP
\fBgetopt_long_only()\fP  \fBgetopt_long()\fP Ʊͤư򤹤뤬 
`\-'  `\-\-' ƱͤˡĹץȤư롣 `\-' ǻϤޤ
(`\-\-' ʳ) ץ󤬡ĹΤˤϥޥåʤûΤ˥ޥ
ˤƤϡûץȤƲᤵ롣 
.SH ֤
.B getopt()
ؿϥץ󤬸ĤФΥץʸ֤ɬפʰ­
ʤץ󤬤 `:' ֤Ƥʤץʸ
 `?' ֤ץꥹȤκǸ褿ˤ \-1 ֤
.PP
\fBgetopt_long()\fP  \fBgetopt_long_only()\fP ⡢ûץʸ
ǧˤϤʸ֤ĹץФƤϡ 
\fIflag\fP  \fBNULL\fP ʤ \fIval\fP ֤ \fIflag\fP  
\fBNULL\fP ʳʤ 0 ֤顼 \-1 ֤ͤ \fBgetopt()\fP 
ƱǤ롣 `?' ϡޥåǤʤ;ʬʥѥ᡼
ˤ֤롣
.SH Ķѿ
.TP
.SM
.B POSIXLY_CORRECT
줬ꤵƤȡ󥪥ץΰãǥץ
Фߤ롣
.TP
.SM
.B _<PID>_GNU_nonoption_argv_flags_
ѿ
.B bash
2.0  GNU libc ̿뤿Ѥ줿ɤΰ磻ɥ
Ÿ̤ǡäƥץȤߤʤ٤ǤʤΤ餻
ΤǤ롣εǽ
.B bash
ΥС 2.01 Ǻ줿 GNU libc ˤϤޤĤäƤ롣
.SH 
ʲϡ
.B getopt_long()
λˡ򡢤ۤܤ٤ƤεǽˤĤƼץǤ롣
.nf
.sp
#include <stdio.h>     /* for printf */
#include <stdlib.h>    /* for exit */
#include <getopt.h>

int
main (int argc, char **argv) {
    int c;
    int digit_optind = 0;

    while (1) {
        int this_option_optind = optind ? optind : 1;
        int option_index = 0;
        static struct option long_options[] = {
            {"add", 1, 0, 0},
            {"append", 0, 0, 0},
            {"delete", 1, 0, 0},
            {"verbose", 0, 0, 0},
            {"create", 1, 0, 'c'},
            {"file", 1, 0, 0},
            {0, 0, 0, 0}
        };

        c = getopt_long (argc, argv, "abc:d:012",
                 long_options, &option_index);
        if (c == -1)
            break;

        switch (c) {
        case 0:
            printf ("option %s", long_options[option_index].name);
            if (optarg)
                printf (" with arg %s", optarg);
            printf ("\\n");
            break;

        case '0':
        case '1':
        case '2':
            if (digit_optind != 0 && digit_optind != this_option_optind)
              printf ("digits occur in two different argv-elements.\\n");
            digit_optind = this_option_optind;
            printf ("option %c\\n", c);
            break;

        case 'a':
            printf ("option a\\n");
            break;

        case 'b':
            printf ("option b\\n");
            break;

        case 'c':
            printf ("option c with value `%s'\\n", optarg);
            break;

        case 'd':
            printf ("option d with value `%s'\\n", optarg);
            break;

        case '?':
            break;

        default:
            printf ("?? getopt returned character code 0%o ??\\n", c);
        }
    }

    if (optind < argc) {
        printf ("non-option ARGV-elements: ");
        while (optind < argc)
            printf ("%s ", argv[optind++]);
        printf ("\\n");
    }

    exit (0);
}
.fi
.SH Х
POSIX.2 ˤ
.B getopt()
λ͡ POSIX.2 Interpretation 150 ˤϵŪ꤬롣
GNU ˤǤ (餯¾Τ٤ƤμǤ)ͤȰۤʤ
ưƤ롣
.SH 
.TP
\fBgetopt()\fP:
Ķѿ POSIXLY_CORRECT ꤵƤ POSIX.2 ˽
¾ξ \fIargv\fP Ǥΰ̣ǤˤϤʤʤʤ
ѹƤޤǤ롣ϡץȥפǤ
Ǥ뤫Τ褦ˤƤ롣¾ΥƥȤθߴΤǤ롣