File: getopt.3

package info (click to toggle)
manpages-ja 0.5.0.0.20100315-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 21,156 kB
  • ctags: 1
  • sloc: sh: 13,935; perl: 157; makefile: 114
file content (526 lines) | stat: -rw-r--r-- 15,614 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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
.\" Copyright (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)
.\" Modified 990715, aeb: changed `EOF' into `-1' since that is what POSIX
.\"  says; moreover, EOF is not defined in <unistd.h>.
.\" Modified 2002-02-16, joey: added information about non-existing
.\"  option character and colon as first option character
.\" Modified 2004-07-28, Michael Kerrisk <mtk.manpages@gmail.com>
.\"	Added text to explain how to order both '[-+]' and ':' at
.\"		the start of optstring
.\" Modified 2006-12-15, mtk, Added getopt() example program.
.\"
.\" Japanese Version Copyright (c) 1998 NAKANO Takeo all rights reserved.
.\" Translated 1998-08-12, NAKANO Takeo <nakano@apm.seikei.ac.jp>
.\" Updated & Modified 1999-08-21, NAKANO Takeo <nakano@apm.seikei.ac.jp>
.\" Updated & Modified 2001-07-01, Yuichi SATO <ysato@h4.dion.ne.jp>
.\" Updated & Modified 2002-03-23, Yuichi SATO
.\" Updated & Modified 2003-09-07, Yuichi SATO <ysato444@yahoo.co.jp>
.\" Updated & Modified 2005-01-10, Yuichi SATO
.\" Updated & Modified 2005-10-28, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
.\" Updated & Modified 2006-01-18, Akihiro MOTOKI
.\" Updated & Modified 2007-05-01, Akihiro MOTOKI, LDP v2.46
.\" Updated 2008-08-11, Akihiro MOTOKI, LDP v3.05
.\"
.\"WORD:	argv-element		argv 
.\"
.TH GETOPT 3  2008-08-29 "GNU" "Linux Programmer's Manual"
.SH ̾
getopt, getopt_long, getopt_long_only,
optarg, optind, opterr, optopt \- ޥɥ饤󥪥ץ᤹
.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 #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
.sp
.in -4n
glibc εǽޥ׷
.RB ( feature_test_macros (7)
):
.ad l
.in
.sp
.BR getopt ():
_POSIX_C_SOURCE\ >=\ 2 || _XOPEN_SOURCE || _POSIX_SOURCE
.br
.BR getopt_long (),
.BR getopt_long_only ():
_GNU_SOURCE
.ad b
.SH 
.BR getopt ()
ؿϥޥɥ饤᤹롣
.BR getopt ()
Ȥ
.I argc

.I argv
ϡ줾ץεư
.IR main ()
ؿϤ줿θĿǤ롣
\fIargv\fP ǤΤ \(aq\-\(aq ǻϤޤ
( "\-" ñȤ "\-\-" ñȤǤϤʤ) 
ץ (option element) Ȥߤʤ롣
ǤƬ \(aq\-\(aq ʸ
ץʸ (option character) Ȥ롣
.BR getopt ()
ϡ֤ƤӽФ뤴ȤˡΥץʸ֤
.PP
ѿ
.I optind
ϡ
.I argv
μ˽ǤΥǥåǤ롣
ƥˤꤳѿͤ 1 ˽롣
ƤӽФ¦Ǥͤ 1 ˥ꥻåȤ뤳ȤǡƱ
.I argv
Υľꡢ٥ȥ򥹥󤹤뤳ȤǤ롣
.PP
ʥץʸ򸫤Ĥȡ
.BR getopt ()
Ϥʸ֤
ѿ \fIoptind\fP ȥƥåѿ \fInextchar\fP 򹹿롣
ˤäơ
.BR getopt ()
ϼθƤӽФκݤˡ
ʹߤΥץʸ \fIargv\fP ǤΥ³Ǥ롣
.PP
ץʸʾ師Ĥʤʤȡ
.BR getopt ()

\-1 ֤ \fIoptind\fP ϡ\fIargv\fP ǤΤ
ץǤʤǽǤ򼨤褦ˤʤ롣
.PP
.I optstring
ϼդ륪ץʸʤʸǤ롣
ʸΤȤ˥ (:) ֤Ƥϡ
ץˤϰɬפǤ뤳Ȥ򼨤
ΤȤ
.BR getopt ()
ϡܤƤ
\fIargv\fP Ǥǡץʸ˰³ƥȤؤΥݥ󥿤
뤤ϼ \fIargv\fP ǤΥƥȤؤΥݥ󥿤
.I optarg
2 Ϣ³ƥ֤Ƥϡ
ΥץϰȤäƤȤʤƤ褤
ߤ \fIargv\fP Ǥ˥ƥȤ
(Ĥޤꡢ"\-oarg" Τ褦ˡץ̾ȤƱ
ƥȤ)줬 \fIoptarg\fP ֤롣
ʤ \fIoptarg\fP  0 ꤵ롣
 GNU ˤĥǤ롣
.I optstring

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

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

.I <getopt.h>
ǰʲΤ褦Ƥ롣
.in +4n
.nf
.sp
struct option {
    const char *name;
    int         has_arg;
    int        *flag;
    int         val;
};
.fi
.in
.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 
NULL ʤ
.BR getopt_long ()
 \fIval\fP ֤
(㤨иƤӽФΥץϡ
\fIval\fP ʥץʸ뤳ȤǤ)
NULL ʳξˤϡ
.BR getopt_long ()
 0 ֤
ΤȤץ󤬸Ĥ \fIflag\fP ݥȤѿ
\fIval\fP 롣ĤʤȤѿѹʤ
.TP
\fIval\fP
֤͡ޤ \fIflag\fP ݥȤѿإɤ͡
.PP
κǸǤϡ 0 ƤʤФʤʤ
.PP
\fIlongindex\fP ϡNULL ǤʤС
ĹץΥǥå
.I longopts
а֤ȤݻƤѿؤΥݥ󥿤Ȥʤ롣
.PP
.BR getopt_long_only ()

.BR getopt_long ()
Ʊͤư򤹤뤬 \(aq\-\(aq  "\-\-" Ʊͤˡ
ĹץȤư롣\(aq\-\(aq ǻϤޤ
("\-\-" ʳ) ץ󤬡ĹΤˤϥޥåʤûΤ
ޥåˤƤϡûץȤƲᤵ롣
.SH ֤
ץ˸Ĥ
.BR getopt ()
ϤΥץʸ֤
٤ƤΥޥɥ饤󥪥ץβϤä顢
.BR getopt ()
 \-1 ֤
.I optstring
˴ޤޤʤץʸĤȡ\(aq?\(aq ֤
­ʤץ󤬸Ĥä硢
֤ͤ
.I optstring
κǽʸˤۤʤ: ǽʸ \(aq:\(aq Ǥ \(aq:\(aq ֤
ʳξ \(aq?\(aq ֤
.PP
.BR getopt_long ()

.BR getopt_long_only ()
ûץʸǧˤϤʸ֤
ĹץФƤϡ
\fIflag\fP  NULL ʤ \fIval\fP ֤
\fIflag\fP  NULL ʳʤ 0 ֤
顼 \-1 ֤ͤ
.BR getopt ()
ƱǤ롣
 \(aq?\(aq ϡޥåǤʤ;ʬʥѥ᡼ˤ֤롣
.SH Ķ
.TP
.B POSIXLY_CORRECT
줬ꤵƤȡ󥪥ץΰãǥץ
Фߤ롣
.TP
.B _<PID>_GNU_nonoption_argv_flags_
ѿ
.BR bash (1)
2.0  glibc ̿뤿Ѥ줿
ɤΰ磻ɥɤŸ̤ǡ
äƥץȤߤʤ٤ǤʤΤ餻ΤǤ롣
εǽ
.BR bash (1)
ΥС 2.01 Ǻ줿glibc ˤϤޤĤäƤ롣
.SH 
.TP
.BR getopt ():
Ķѿ
.B POSIXLY_CORRECT
ꤵƤ POSIX.2  POSIX.1-2001 ˽򤹤롣
¾ξ \fIargv\fP Ǥΰ̣ǤˤϤʤʤ
ʤʤѹƤޤǤ롣
ϡץȥפǤǤ뤫Τ褦ˤƤ롣
¾ΥƥȤθߴΤǤ롣

.I optstring
 \(aq+\(aq  \(aq\-\(aq ȤΤ GNU ˤĥǤ.

ŤΤĤǤϡ
.BR getopt ()

.I <stdio.h>
Ƥ
SUSv1 Ǥϡ
.I <unistd.h>

.I <stdio.h>
Τɤ餫
Ƥ褫ä
POSIX.1-2001 Ǥϡ
.B getopt

.I <stdio.h>
ǹԤΤϡֲ̾ġפǤȤ줿
POSIX.1-2001 Ǥ
.I <stdio.h>
ԤȤǧƤʤ
.TP
.BR getopt_long "(), " getopt_long_only ():
δؿ GNU ˤĥǤ롣
.SH 
ʣΰ٥ȥ򥹥󤷤ꡢƱ٥ȥʾ
󤹤褦ʥץǡ
.I optstring
Ƭ \(aq+\(aq  \(aq\-\(aq Ȥä GNU ˤĥǽѤꡢ
٥ȥڤؤ
.B POSIXLY_CORRECT
ͤѹꤹˤϡ
.I optind
Ū 1 ǤϤʤ 0 ˥ꥻåȤ뤳Ȥ
.BR getopt ()
ƽʤФʤʤ
(0 ˥ꥻåȤ뤳Ȥǡ
.B POSIXLY_CORRECT

.I optstring
 GNU ĥǽΥåԤ롼󤬵ư)
.SH Х
POSIX.2 ˤ
.BR getopt ()
λͤˤϵŪ꤬ꡢ
Ƥ POSIX.2 Interpretation 150 ˵Ƥ롣
GNU ˤǤ (餯¾Τ٤ƤμǤ)
ͤȰۤʤư򤹤褦˼Ƥ롣
.SH 
ʲ˼ñʥץץǤϡ
Υץ४ץ򰷤Τ
.BR getopt ()
ѤƤ롣Ĥͤȼʤ
.I \-n
ǡ⤦Ĥбͤɬפ
.I "\-t val"
Ǥ롣
.nf
.sp
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

int
main(int argc, char *argv[])
{
    int flags, opt;
    int nsecs, tfnd;

    nsecs = 0;
    tfnd = 0;
    flags = 0;
    while ((opt = getopt(argc, argv, "nt:")) != \-1) {
        switch (opt) {
        case \(aqn\(aq:
            flags = 1;
            break;
        case \(aqt\(aq:
            nsecs = atoi(optarg);
            tfnd = 1;
            break;
        default: /* \(aq?\(aq */
            fprintf(stderr, "Usage: %s [\-t nsecs] [\-n] name\\n",
                    argv[0]);
            exit(EXIT_FAILURE);
        }
    }

    printf("flags=%d; tfnd=%d; optind=%d\\n", flags, tfnd, optind);

    if (optind >= argc) {
        fprintf(stderr, "Expected argument after options\\n");
        exit(EXIT_FAILURE);
    }

    printf("name argument = %s\\n", argv[optind]);

    /* Other code omitted */

    exit(EXIT_SUCCESS);
}
.fi
.PP
ʲϡ
.BR 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, \(aqc\(aq},
            {"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 \(aq0\(aq:
        case \(aq1\(aq:
        case \(aq2\(aq:
            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 \(aqa\(aq:
            printf("option a\\n");
            break;

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

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

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

        case \(aq?\(aq:
            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(EXIT_SUCCESS);
}
.fi
.SH Ϣ
.BR getsubopt (3),
.BR feature_test_macros (7)