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
|
.\" (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@gmx.net>
.\" Added text to explain how to order both '[-+]' and ':' at
.\" the start of optstring
.\"
.\" 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>
.\" Updated & Modified Sat Mar 23 23:03:20 JST 2002 by Yuichi SATO
.\" Updated & Modified Sun Sep 7 13:49:03 JST 2003
.\" by Yuichi SATO <ysato444@yahoo.co.jp>
.\" Updated & Modified Mon Jan 10 13:52:19 JST 2005 by Yuichi SATO
.\"
.\"WORD: argv-element argv
.\"
.TH GETOPT 3 2004-07-28 "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 ݤ˽ѹ
ץʳǤǸ˰ư롣
¾ˤ 2 ĤΥ⡼ɤƤ롣
\fIoptstring\fP Ƭʸ '+' Ǥ뤫Ķѿ
POSIXLY_CORRECT ꤵƤˤϡץоݤȤưϡ
ץΰ줿ʳǽλ롣
\fIoptstring\fP Ƭʸ '\-' Ǥˤϡ
ץǤʤ \fIargv\fP Ǥϡ
ʸ 1 ΥץǤ뤫Τ褦˰ (Ѥץϡ
ץ \fIargv\fP ǤǤդνǼ졢Ĥν
̣Ĥ褦˽Ƥɬפ)
"\-\-" üʰǡΥ⡼ɤˤ餺
ץΥŪ˽λ롣
.PP
ǧǤʤץʸȡ
\fBgetopt()\fP ϥ顼åɸϤɽ
ʸ \fIoptopt\fP ¸ '?' ֤
ƤӽФץ \fIopterr\fP 0 ˤƤС
顼åɽǤ롣
.PP
\fBgetopt()\fP \fIargv\fP
\fIoptstring\fP ˤʤץʸĤ硢
ޤϥץʤȤʬä硢
\&'?' ֤Ƴѿ \fIoptopt\fP Υץʸꤹ롣
\fIoptstring\fP (ץǻǤ
\&'+' ޤ '\-' ³) ǽʸ (':') ΤȤ
\fBgetopt()\fP ϥץʤ '?' ǤϤʤ ':' ֤
顼Ĥǡ \fIoptstring\fP κǽʸǤʤ
ijѿ \fIopterr\fP 0 Ǥʤ (줬ǥե)
\fBgetopt()\fP ϥ顼åɽ롣
.PP
.B getopt_long()
ؿϡĹץ (2 ĤΥå `\-\-' ǻϤޤ륪ץ)
뤳Ȥ
.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 Ǥΰ̣ǤˤϤʤʤ
ʤʤѹƤޤǤ롣
ϡץȥפǤǤ뤫Τ褦ˤƤ롣
¾ΥƥȤθߴΤǤ롣
|