OSDN Git Service

Import translated manuals from JM CVS Repository.
[linuxjm/jm.git] / manual / LDP_man-pages / draft / man3 / getopt.3
1 .\" Copyright (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de)
2 .\"
3 .\" Permission is granted to make and distribute verbatim copies of this
4 .\" manual provided the copyright notice and this permission notice are
5 .\" preserved on all copies.
6 .\"
7 .\" Permission is granted to copy and distribute modified versions of this
8 .\" manual under the conditions for verbatim copying, provided that the
9 .\" entire resulting derived work is distributed under the terms of a
10 .\" permission notice identical to this one.
11 .\"
12 .\" Since the Linux kernel and libraries are constantly changing, this
13 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
14 .\" responsibility for errors or omissions, or for damages resulting from
15 .\" the use of the information contained herein.  The author(s) may not
16 .\" have taken the same level of care in the production of this manual,
17 .\" which is licensed free of charge, as they might when working
18 .\" professionally.
19 .\"
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
22 .\" License.
23 .\" Modified Sat Jul 24 19:27:50 1993 by Rik Faith (faith@cs.unc.edu)
24 .\" Modified Mon Aug 30 22:02:34 1995 by Jim Van Zandt <jrv@vanzandt.mv.com>
25 .\"  longindex is a pointer, has_arg can take 3 values, using consistent
26 .\"  names for optstring and longindex, "\n" in formats fixed.  Documenting
27 .\"  opterr and getopt_long_only.  Clarified explanations (borrowing heavily
28 .\"  from the source code).
29 .\" Modified 8 May 1998 by Joseph S. Myers (jsm28@cam.ac.uk)
30 .\" Modified 990715, aeb: changed `EOF' into `-1' since that is what POSIX
31 .\"  says; moreover, EOF is not defined in <unistd.h>.
32 .\" Modified 2002-02-16, joey: added information about nonexisting
33 .\"  option character and colon as first option character
34 .\" Modified 2004-07-28, Michael Kerrisk <mtk.manpages@gmail.com>
35 .\"     Added text to explain how to order both '[-+]' and ':' at
36 .\"             the start of optstring
37 .\" Modified 2006-12-15, mtk, Added getopt() example program.
38 .\"
39 .\" Japanese Version Copyright (c) 1998 NAKANO Takeo all rights reserved.
40 .\" Translated 1998-08-12, NAKANO Takeo <nakano@apm.seikei.ac.jp>
41 .\" Updated & Modified 1999-08-21, NAKANO Takeo <nakano@apm.seikei.ac.jp>
42 .\" Updated & Modified 2001-07-01, Yuichi SATO <ysato@h4.dion.ne.jp>
43 .\" Updated & Modified 2002-03-23, Yuichi SATO
44 .\" Updated & Modified 2003-09-07, Yuichi SATO <ysato444@yahoo.co.jp>
45 .\" Updated & Modified 2005-01-10, Yuichi SATO
46 .\" Updated & Modified 2005-10-28, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
47 .\" Updated & Modified 2006-01-18, Akihiro MOTOKI
48 .\" Updated & Modified 2007-05-01, Akihiro MOTOKI, LDP v2.46
49 .\" Updated 2008-08-11, Akihiro MOTOKI, LDP v3.05
50 .\"
51 .\"WORD:        argv-element            argv Í×ÁÇ
52 .\"
53 .TH GETOPT 3  2010-02-03 "GNU" "Linux Programmer's Manual"
54 .\"O .SH NAME
55 .SH Ì¾Á°
56 .\"O getopt, getopt_long, getopt_long_only,
57 .\"O optarg, optind, opterr, optopt \- Parse command-line options
58 getopt, getopt_long, getopt_long_only,
59 optarg, optind, opterr, optopt \- ¥³¥Þ¥ó¥É¥é¥¤¥ó¥ª¥×¥·¥ç¥ó¤ò²ò¼á¤¹¤ë
60 .\"O .SH SYNOPSIS
61 .SH ½ñ¼°
62 .nf
63 .B #include <unistd.h>
64 .sp
65 .BI "int getopt(int " argc ", char * const " argv[] ,
66 .BI "           const char *" optstring );
67 .sp
68 .BI "extern char *" optarg ;
69 .BI "extern int " optind ", " opterr ", " optopt ;
70 .sp
71 .B #include <getopt.h>
72 .sp
73 .BI "int getopt_long(int " argc ", char * const " argv[] ,
74 .BI "           const char *" optstring ,
75 .BI "           const struct option *" longopts ", int *" longindex );
76 .sp
77 .BI "int getopt_long_only(int " argc ", char * const " argv[] ,
78 .BI "           const char *" optstring ,
79 .BI "           const struct option *" longopts ", int *" longindex );
80 .fi
81 .sp
82 .in -4n
83 .\"O Feature Test Macro Requirements for glibc (see
84 .\"O .BR feature_test_macros (7)):
85 glibc ¸þ¤±¤Îµ¡Ç½¸¡ºº¥Þ¥¯¥í¤ÎÍ×·ï
86 .RB ( feature_test_macros (7)
87 »²¾È):
88 .ad l
89 .in
90 .sp
91 .BR getopt ():
92 _POSIX_C_SOURCE\ >=\ 2 || _XOPEN_SOURCE
93 .br
94 .BR getopt_long (),
95 .BR getopt_long_only ():
96 _GNU_SOURCE
97 .ad b
98 .\"O .SH DESCRIPTION
99 .SH ÀâÌÀ
100 .\"O The
101 .\"O .BR getopt ()
102 .\"O function parses the command-line arguments.
103 .\"O Its arguments
104 .\"O .I argc
105 .\"O and
106 .\"O .I argv
107 .\"O are the argument count and array as passed to the
108 .\"O .IR main ()
109 .\"O function on program invocation.
110 .BR getopt ()
111 ´Ø¿ô¤Ï¥³¥Þ¥ó¥É¥é¥¤¥ó°ú¤­¿ô¤ò²ò¼á¤¹¤ë¡£
112 .BR getopt ()
113 ¤¬¤È¤ë°ú¤­¿ô
114 .I argc
115 ¤È
116 .I argv
117 ¤Ï¡¢¤½¤ì¤¾¤ì¥×¥í¥°¥é¥à¤Îµ¯Æ°»þ¤Ë
118 .IR main ()
119 ´Ø¿ô¤ËÅϤµ¤ì¤¿°ú¤­¿ô¤Î¸Ä¿ô¤ÈÇÛÎó¤Ç¤¢¤ë¡£
120 .\"O An element of \fIargv\fP that starts with \(aq\-\(aq
121 .\"O (and is not exactly "\-" or "\-\-")
122 .\"O is an option element.
123 .\"O The characters of this element
124 .\"O (aside from the initial \(aq\-\(aq) are option characters.
125 .\"O If
126 .\"O .BR getopt ()
127 .\"O is called repeatedly, it returns successively each of the option characters
128 .\"O from each of the option elements.
129 \fIargv\fP ¤ÎÍ×ÁǤΤ¦¤Á \(aq\-\(aq ¤Ç»Ï¤Þ¤ë¤â¤Î
130 (¤«¤Ä "\-" Ã±ÆȤä "\-\-" Ã±ÆȤǤϤʤ¤¤â¤Î) ¤Ï
131 ¥ª¥×¥·¥ç¥óÍ×ÁÇ (option element) ¤È¤ß¤Ê¤µ¤ì¤ë¡£
132 ¤³¤ÎÍ×ÁǤ«¤éÀèƬ¤Î \(aq\-\(aq ¤ò½ü¤¤¤¿Ê¸»ú¤Ï
133 ¥ª¥×¥·¥ç¥óʸ»ú (option character) ¤È¤µ¤ì¤ë¡£
134 .BR getopt ()
135 ¤Ï¡¢·«¤êÊÖ¤·¸Æ¤Ó½Ð¤µ¤ì¤ë¤´¤È¤Ë¡¢¼¡¤Î¥ª¥×¥·¥ç¥óʸ»ú¤òÊÖ¤¹¡£
136 .PP
137 .\"O The variable
138 .\"O .I optind
139 .\"O is the index of the next element to be processed in
140 .\"O .IR argv .
141 .\"O The system initializes this value to 1.
142 .\"O The caller can reset it to 1 to restart scanning of the same
143 .\"O .IR argv ,
144 .\"O or when scanning a new argument vector.
145 ÊÑ¿ô
146 .I optind
147 ¤Ï¡¢
148 .I argv
149 ¤Î¼¡¤Ë½èÍý¤µ¤ì¤ëÍ×ÁǤΥ¤¥ó¥Ç¥Ã¥¯¥¹¤Ç¤¢¤ë¡£
150 ¥·¥¹¥Æ¥à¤Ë¤è¤ê¤³¤ÎÊÑ¿ô¤ÎÃͤϠ1 ¤Ë½é´ü²½¤µ¤ì¤ë¡£
151 ¸Æ¤Ó½Ð¤·Â¦¤Ç¤³¤ÎÃͤò 1 ¤Ë¥ê¥»¥Ã¥È¤¹¤ë¤³¤È¤Ç¡¢Æ±¤¸
152 .I argv
153 ¤Î¥¹¥­¥ã¥ó¤ò¤ä¤êľ¤·¤¿¤ê¡¢¿·¤·¤¤°ú¤­¿ô¥Ù¥¯¥È¥ë¤ò¥¹¥­¥ã¥ó¤¹¤ë¤³¤È¤¬¤Ç¤­¤ë¡£
154 .PP
155 .\"O If
156 .\"O .BR getopt ()
157 .\"O finds another option character, it returns that
158 .\"O character, updating the external variable \fIoptind\fP and a static
159 .\"O variable \fInextchar\fP so that the next call to
160 .\"O .BR getopt ()
161 .\"O can
162 .\"O resume the scan with the following option character or
163 .\"O \fIargv\fP-element.
164 ¿·¤¿¤Ê¥ª¥×¥·¥ç¥óʸ»ú¤ò¸«¤Ä¤±¤ë¤È¡¢
165 .BR getopt ()
166 ¤Ï¤½¤Îʸ»ú¤òÊÖ¤·¡¢
167 ³°ÉôÊÑ¿ô \fIoptind\fP ¤È¥¹¥¿¥Æ¥£¥Ã¥¯¤ÊÊÑ¿ô \fInextchar\fP ¤ò¹¹¿·¤¹¤ë¡£
168 ¤³¤ì¤é¤Ë¤è¤Ã¤Æ¡¢
169 .BR getopt ()
170 ¤Ï¼¡²ó¤Î¸Æ¤Ó½Ð¤·¤ÎºÝ¤Ë¡¢
171 °Ê¹ß¤Î¥ª¥×¥·¥ç¥óʸ»ú¤ä \fIargv\fP Í×ÁǤΥ¹¥­¥ã¥ó¤ò·Ñ³¤Ç¤­¤ë¡£
172 .PP
173 .\"O If there are no more option characters,
174 .\"O .BR getopt ()
175 .\"O returns \-1.
176 .\"O Then \fIoptind\fP is the index in \fIargv\fP of the first
177 .\"O \fIargv\fP-element that is not an option.
178 ¥ª¥×¥·¥ç¥óʸ»ú¤¬¤½¤ì°Ê¾å¸«¤Ä¤«¤é¤Ê¤¯¤Ê¤ë¤È¡¢
179 .BR getopt ()
180 ¤Ï
181 \-1 ¤òÊÖ¤¹¡£¤½¤·¤Æ \fIoptind\fP ¤Ï¡¢\fIargv\fP ¤ÎÍ×ÁǤΤ¦¤Á¡¢
182 ¥ª¥×¥·¥ç¥ó¤Ç¤Ê¤¤ºÇ½é¤ÎÍ×ÁǤò¼¨¤¹¤è¤¦¤Ë¤Ê¤ë¡£
183 .PP
184 .\"O .I optstring
185 .\"O is a string containing the legitimate option characters.
186 .\"O If such a
187 .\"O character is followed by a colon, the option requires an argument, so
188 .\"O .BR getopt ()
189 .\"O places a pointer to the following text in the same
190 .\"O \fIargv\fP-element, or the text of the following \fIargv\fP-element, in
191 .\"O .IR optarg .
192 .I optstring
193 ¤Ï¼õ¤±ÉÕ¤±¤ë¥ª¥×¥·¥ç¥óʸ»ú¤«¤é¤Ê¤ëʸ»úÎó¤Ç¤¢¤ë¡£
194 ʸ»ú¤Î¤¢¤È¤Ë¥³¥í¥ó (:) ¤¬ÃÖ¤«¤ì¤Æ¤¤¤ë¾ì¹ç¤Ï¡¢
195 ¥ª¥×¥·¥ç¥ó¤Ë¤Ï°ú¤­¿ô¤¬É¬ÍפǤ¢¤ë¤³¤È¤ò¼¨¤¹¡£
196 ¤³¤Î¤È¤­
197 .BR getopt ()
198 ¤Ï¡¢¸½ºßÃíÌܤ·¤Æ¤¤¤ë
199 \fIargv\fP Í×ÁǤǡ¢¥ª¥×¥·¥ç¥óʸ»ú¤Ë°ú¤­Â³¤¯¥Æ¥­¥¹¥È¤Ø¤Î¥Ý¥¤¥ó¥¿¤«¡¢
200 ¤¢¤ë¤¤¤Ï¼¡¤Î \fIargv\fP Í×ÁǤΥƥ­¥¹¥È¤Ø¤Î¥Ý¥¤¥ó¥¿¤ò
201 .I optarg
202 ¤ËÂåÆþ¤¹¤ë¡£
203 .\"O Two colons mean an option takes
204 .\"O an optional arg; if there is text in the current \fIargv\fP-element
205 .\"O (i.e., in the same word as the option name itself, for example, "\-oarg"),
206 .\"O then it is returned in \fIoptarg\fP, otherwise \fIoptarg\fP is set to zero.
207 .\"O This is a GNU extension.
208 .\"O If
209 .\"O .I optstring
210 .\"O contains
211 .\"O .B W
212 .\"O followed by a semicolon, then
213 .\"O .B \-W foo
214 .\"O is treated as the long option
215 .\"O .BR \-\-foo .
216 2 ¸ÄϢ³¤·¤Æ¥³¥í¥ó¤¬ÃÖ¤«¤ì¤Æ¤¤¤ë¾ì¹ç¤Ï¡¢
217 ¤½¤Î¥ª¥×¥·¥ç¥ó¤Ï°ú¤­¿ô¤ò¤È¤Ã¤Æ¤â¤È¤é¤Ê¤¯¤Æ¤â¤è¤¤¡£
218 ¸½ºß¤Î \fIargv\fP Í×ÁǤ˥ƥ­¥¹¥È¤¬¤¢¤ì¤Ð
219 (¤Ä¤Þ¤ê¡¢"\-oarg" ¤Î¤è¤¦¤Ë¡¢¥ª¥×¥·¥ç¥ó̾¼«¿È¤ÈƱ¤¸¥ï¡¼¥ÉÆâ¤Ë
220 ¥Æ¥­¥¹¥È¤¬¤¢¤ë¾ì¹ç)¡¢¤½¤ì¤¬ \fIoptarg\fP ¤ËÊÖ¤µ¤ì¤ë¡£
221 ¤Ê¤±¤ì¤Ð \fIoptarg\fP ¤Ï 0 ¤ËÀßÄꤵ¤ì¤ë¡£
222 ¤³¤ì¤Ï GNU ¤Ë¤è¤ë³ÈÄ¥¤Ç¤¢¤ë¡£
223 .I optstring
224 ¤Ë
225 .B W
226 ¤È¤½¤ì¤Ë³¤¯¥»¥ß¥³¥í¥ó¤¬Æþ¤Ã¤Æ¤¤¤ë¤È¡¢
227 .B \-W foo
228 ¤ÏŤ¤¥ª¥×¥·¥ç¥ó
229 .B \-\-foo
230 ¤ÈƱ¤¸¤è¤¦¤Ë°·¤ï¤ì¤ë
231 .\"O (The
232 .\"O .B \-W
233 .\"O option is reserved by POSIX.2 for implementation extensions.)
234 .\"O This behavior is a GNU extension, not available with libraries before
235 .\"O glibc 2.
236 (POSIX.2 ¤Ï
237 .B \-W
238 ¥ª¥×¥·¥ç¥ó¤ò¼ÂÁõ°Í¸¤Î³ÈÄ¥¤È¤·¤ÆͽÌ󤷤Ƥ¤¤ë)¡£
239 ¤³¤ÎÆ°ºî¤Ï GNU ¤Ë¤è¤ë³ÈÄ¥¤Ç¤¢¤ê¡¢glibc 2 °ÊÁ°¤Î¥é¥¤¥Ö¥é¥ê¤Ç¤Ï
240 ÍøÍѤǤ­¤Ê¤¤¡£
241 .PP
242 .\"O By default,
243 .\"O .BR getopt ()
244 .\"O permutes the contents of \fIargv\fP as it
245 .\"O scans, so that eventually all the nonoptions are at the end.
246 .\"O Two other modes are also implemented.
247 .\"O If the first character of
248 .\"O \fIoptstring\fP is \(aq+\(aq or the environment variable
249 .\"O .B POSIXLY_CORRECT
250 .\"O is set, then option processing stops as soon as a nonoption argument is
251 .\"O encountered.
252 .\"O If the first character of \fIoptstring\fP is \(aq\-\(aq, then
253 .\"O each nonoption \fIargv\fP-element is handled as if it were the argument of
254 .\"O an option with character code 1.  (This is used by programs that were
255 .\"O written to expect options and other \fIargv\fP-elements in any order
256 .\"O and that care about the ordering of the two.)
257 .\"O The special argument "\-\-" forces an end of option-scanning regardless
258 .\"O of the scanning mode.
259 ¥Ç¥Õ¥©¥ë¥È¤Ç¤Ï
260 .BR getopt ()
261 ¤Ï \fIargv\fP ¤ò¥¹¥­¥ã¥ó¤¹¤ëºÝ¤Ë½ç½ø¤òÊѹ¹¤·¡¢
262 ¥ª¥×¥·¥ç¥ó°Ê³°¤ÎÍ×ÁǤòºÇ¸å¤Ë°ÜÆ°¤¹¤ë¡£
263 ¾¤Ë¤â 2 ¤Ä¤Î¥â¡¼¥É¤¬¼ÂÁõ¤µ¤ì¤Æ¤¤¤ë¡£
264 \fIoptstring\fP ¤ÎÀèƬʸ»ú¤¬ \(aq+\(aq ¤Ç¤¢¤ë¤«¡¢´Ä¶­ÊÑ¿ô
265 .B POSIXLY_CORRECT
266 ¤¬ÀßÄꤵ¤ì¤Æ¤¤¤ë¾ì¹ç¤Ë¤Ï¡¢¥ª¥×¥·¥ç¥ó¤òÂоݤȤ¹¤ëÆ°ºî¤Ï¡¢
267 È󥪥ץ·¥ç¥ó¤Î°ú¤­¿ô¤¬¸½¤ì¤¿Ãʳ¬¤Ç½ªÎ»¤¹¤ë¡£
268 \fIoptstring\fP ¤ÎÀèƬʸ»ú¤¬ \(aq\-\(aq ¤Ç¤¢¤ë¾ì¹ç¤Ë¤Ï¡¢
269 ¥ª¥×¥·¥ç¥ó¤Ç¤Ê¤¤ \fIargv\fP Í×ÁǤϡ¢
270 ʸ»ú¥³¡¼¥É 1 ¤Î¥ª¥×¥·¥ç¥ó¤Ç¤¢¤ë¤«¤Î¤è¤¦¤Ë°·¤ï¤ì¤ë (¤³¤ì¤òÍѤ¤¤ë¥×¥í¥°¥é¥à¤Ï¡¢
271 ¥ª¥×¥·¥ç¥ó¤ä \fIargv\fP Í×ÁǤòǤ°Õ¤Î½ç½ø¤Ç¼õ¤±Æþ¤ì¡¢¤«¤Ä¤½¤ì¤é¤Î½ç½ø¤¬
272 °ÕÌ£¤ò»ý¤Ä¤è¤¦¤Ë½ñ¤«¤ì¤Æ¤¤¤ëɬÍפ¬¤¢¤ë)¡£
273 "\-\-" ¤ÏÆüì¤Ê°ú¤­¿ô¤Ç¡¢¥¹¥­¥ã¥ó¤Î¥â¡¼¥É¤Ë¤è¤é¤º¡¢
274 ¥ª¥×¥·¥ç¥ó¤Î¥¹¥­¥ã¥ó¤ò¶¯À©Åª¤Ë½ªÎ»¤µ¤»¤ë¡£
275 .PP
276 .\"O If
277 .\"O .BR getopt ()
278 .\"O does not recognize an option character, it prints an
279 .\"O error message to \fIstderr\fP, stores the character in \fIoptopt\fP, and
280 .\"O returns \(aq?\(aq.
281 .\"O The calling program may prevent the error message by
282 .\"O setting \fIopterr\fP to 0.
283 ǧ¼±¤Ç¤­¤Ê¤¤¥ª¥×¥·¥ç¥óʸ»ú¤¬¤¢¤ë¤È¡¢
284 .BR getopt ()
285 ¤Ï¥¨¥é¡¼¥á¥Ã¥»¡¼¥¸¤òɸ½à¥¨¥é¡¼½ÐÎÏ \fIstderr\fP ¤Ëɽ¼¨¤·¡¢
286 ¤½¤Îʸ»ú¤ò \fIoptopt\fP ¤ËÊݸ¤·¤Æ \(aq?\(aq ¤òÊÖ¤¹¡£
287 ¸Æ¤Ó½Ð¤·¤¿¥×¥í¥°¥é¥à¤Ç \fIopterr\fP ¤ò 0 ¤Ë¤·¤Æ¤ª¤±¤Ð¡¢
288 ¥¨¥é¡¼¥á¥Ã¥»¡¼¥¸¤Îɽ¼¨¤òÍÞÀ©¤Ç¤­¤ë¡£
289 .PP
290 .\"O If
291 .\"O .BR getopt ()
292 .\"O finds an option character in \fIargv\fP that was not
293 .\"O included in \fIoptstring\fP, or if it detects a missing option argument,
294 .\"O it returns \(aq?\(aq  and sets the external variable \fIoptopt\fP to the
295 .\"O actual option character.
296 .\"O If the first character
297 .\"O (following any optional \(aq+\(aq or \(aq\-\(aq described above)
298 .\"O of \fIoptstring\fP
299 .\"O is a colon (\(aq:\(aq), then
300 .\"O .BR getopt ()
301 .\"O returns \(aq:\(aq instead of \(aq?\(aq to
302 .\"O indicate a missing option argument.
303 .\"O If an error was detected, and
304 .\"O the first character of \fIoptstring\fP is not a colon, and
305 .\"O the external variable \fIopterr\fP is nonzero (which is the default),
306 .\"O .BR getopt ()
307 .\"O prints an error message.
308 .BR getopt ()
309 ¤Ï \fIargv\fP ¤ÎÃæ¤Ë
310 \fIoptstring\fP ¤Ë¤Ê¤¤¥ª¥×¥·¥ç¥óʸ»ú¤ò¸«¤Ä¤±¤¿¾ì¹ç¡¢
311 ¤Þ¤¿¤Ï¥ª¥×¥·¥ç¥ó°ú¤­¿ô¤¬Â­¤ê¤Ê¤¤¤³¤È¤¬Ê¬¤«¤Ã¤¿¾ì¹ç¡¢
312 \&\(aq?\(aq ¤òÊÖ¤·¤Æ³°ÉôÊÑ¿ô \fIoptopt\fP ¤ò¤½¤Î¥ª¥×¥·¥ç¥óʸ»ú¤ËÀßÄꤹ¤ë¡£
313 \fIoptstring\fP ¤Î (¾å¤ÇÀâÌÀ¤·¤¿¥ª¥×¥·¥ç¥ó¤Ç»ØÄê¤Ç¤­¤ë
314 \&\(aq+\(aq ¤Þ¤¿¤Ï \(aq\-\(aq ¸å¤Ë³¤¯) ºÇ½é¤Îʸ»ú¤¬
315 ¥³¥í¥ó (\(aq:\(aq) ¤Î¤È¤­¡¢
316 .BR getopt ()
317 ¤Ï¥ª¥×¥·¥ç¥ó°ú¤­¿ô¤¬Â­¤ê¤Ê¤¤¾ì¹ç¤Ë \(aq?\(aq ¤Ç¤Ï¤Ê¤¯ \(aq:\(aq ¤òÊÖ¤¹¡£
318 ¥¨¥é¡¼¤ò¸«¤Ä¤±¤¿¾ì¹ç¤Ç¡¢¤«¤Ä \fIoptstring\fP ¤ÎºÇ½é¤Îʸ»ú¤¬¥³¥í¥ó¤Ç¤Ê¤¯¡¢
319 ¤«¤Ä³°ÉôÊÑ¿ô \fIopterr\fP ¤¬ 0 ¤Ç¤Ê¤¤¾ì¹ç (¤³¤ì¤¬¥Ç¥Õ¥©¥ë¥È)¡¢
320 .BR getopt ()
321 ¤Ï¥¨¥é¡¼¥á¥Ã¥»¡¼¥¸¤òɽ¼¨¤¹¤ë¡£
322 .\"O .SS getopt_long() and getopt_long_only()
323 .SS getopt_long() ¤È getopt_long_only()
324 .\"O The
325 .\"O .BR getopt_long ()
326 .\"O function works like
327 .\"O .BR getopt ()
328 .\"O except that it also accepts long options, started with two dashes.
329 .\"O (If the program accepts only long options, then
330 .\"O .I optstring
331 .\"O should be specified as an empty string (""), not NULL.)
332 .\"O Long option names may be abbreviated if the abbreviation is
333 .\"O unique or is an exact match for some defined option.
334 .\"O A long option
335 .\"O may take a parameter, of the form
336 .\"O .B \-\-arg=param
337 .\"O or
338 .\"O .BR "\-\-arg param" .
339 .BR getopt_long ()
340 ´Ø¿ô¤Ï¡¢Ä¹¤¤¥ª¥×¥·¥ç¥ó (2 ¤Ä¤Î¥À¥Ã¥·¥å "\-\-" ¤Ç»Ï¤Þ¤ë¥ª¥×¥·¥ç¥ó) ¤ò
341 ¼õ¤±Æþ¤ì¤ë¤³¤È¤ò½ü¤¤¤Æ
342 .BR getopt ()
343 ¤ÈƱ¤¸¤è¤¦¤ËÆ°ºî¤¹¤ë
344 (¥×¥í¥°¥é¥à¤ËŤ¤¥ª¥×¥·¥ç¥ó¤À¤±¤¬ÅϤµ¤ì¤¿¾ì¹ç¡¢
345 .I optstring
346 ¤Ï NULL ¤Ç¤Ï¤Ê¤¯¶õʸ»úÎó ("") ¤È¤Ê¤ë)¡£
347 Ť¤¥ª¥×¥·¥ç¥ó¤Î̾Á°¤Ï¡¢Â¾¤È½Å¤Ê¤é¤Ê¤¤ÈϰϤˤª¤¤¤Æû½Ì¤Ç¤­¤ë¡£
348 ¤¢¤ë¤¤¤ÏÄêµÁ¤µ¤ì¤¿¥ª¥×¥·¥ç¥ó¤ËÀµ³Î¤Ë¥Þ¥Ã¥Á¤¹¤ë¤â¤Î¤Ç¤â (ÅöÁ³) ¤«¤Þ¤ï¤Ê¤¤¡£
349 Ť¤¥ª¥×¥·¥ç¥ó¤Ï°ú¤­¿ô¤ò¼è¤ë¤³¤È¤¬¤Ç¤­¡¢
350 .B \-\-arg=param
351 ¤Þ¤¿¤Ï
352 .B "\-\-arg param"
353 ¤È¸À¤¦·Á¼°¤Ç»ØÄꤹ¤ë¡£
354 .PP
355 .\"O .I longopts
356 .\"O is a pointer to the first element of an array of
357 .\"O .I struct option
358 .\"O declared in
359 .\"O .I <getopt.h>
360 .\"O as
361 .I longopts
362 ¤Ï
363 .I struct option
364 ¤ÎÍ×ÁǤ«¤é¤Ê¤ëÇÛÎó¤Î¡¢ÀèƬÍ×ÁǤؤΥݥ¤¥ó¥¿¤Ç¤¢¤ë¡£
365 .I struct option
366 ¤Ï
367 .I <getopt.h>
368 ¤Ç°Ê²¼¤Î¤è¤¦¤ËÄêµÁ¤µ¤ì¤Æ¤¤¤ë¡£
369 .in +4n
370 .nf
371 .sp
372 struct option {
373     const char *name;
374     int         has_arg;
375     int        *flag;
376     int         val;
377 };
378 .fi
379 .in
380 .PP
381 .\"O The meanings of the different fields are:
382 ¤½¤ì¤¾¤ì¤Î¥Õ¥£¡¼¥ë¥É¤Î°ÕÌ£¤Ï°Ê²¼¤ÎÄ̤ꡣ
383 .TP
384 .I name
385 .\"O is the name of the long option.
386 Ť¤¥ª¥×¥·¥ç¥ó¤Î̾Á°¡£
387 .TP
388 .I has_arg
389 .\"O is:
390 .\"O \fBno_argument\fP (or 0) if the option does not take an argument;
391 .\"O \fBrequired_argument\fP (or 1) if the option requires an argument; or
392 .\"O \fBoptional_argument\fP (or 2) if the option takes an optional argument.
393 \fBno_argument\fP (¤Þ¤¿¤Ï 0) ¤Ê¤é¡¢¥ª¥×¥·¥ç¥ó¤Ï°ú¤­¿ô¤ò¤È¤é¤Ê¤¤¡£
394 \fBrequired_argument\fP (¤Þ¤¿¤Ï 1) ¤Ê¤é¡¢¥ª¥×¥·¥ç¥ó¤Ï°ú¤­¿ô¤òɬÍפȤ¹¤ë¡£
395 \fBoptional_argument\fP (¤Þ¤¿¤Ï 2) ¤Ê¤é¡¢¥ª¥×¥·¥ç¥ó¤Ï°ú¤­¿ô¤ò¤È¤Ã¤Æ¤â
396 ¤È¤é¤Ê¤¯¤Æ¤âÎɤ¤¡£
397 .TP
398 .I flag
399 .\"O specifies how results are returned for a long option.
400 .\"O If \fIflag\fP
401 .\"O is NULL, then
402 .\"O .BR getopt_long ()
403 .\"O returns \fIval\fP.  (For
404 .\"O example, the calling program may set \fIval\fP to the equivalent short
405 .\"O option character.)
406 .\"O Otherwise,
407 .\"O .BR getopt_long ()
408 .\"O returns 0, and
409 .\"O \fIflag\fP points to a variable which is set to \fIval\fP if the
410 .\"O option is found, but left unchanged if the option is not found.
411 Ť¤¥ª¥×¥·¥ç¥ó¤ËÂФ¹¤ë·ë²Ì¤ÎÊÖ¤·Êý¤ò»ØÄꤹ¤ë¡£\fIflag\fP ¤¬
412 NULL ¤Ê¤é
413 .BR getopt_long ()
414 ¤Ï \fIval\fP ¤òÊÖ¤¹
415 (Î㤨¤Ð¸Æ¤Ó½Ð¤·¸µ¤Î¥×¥í¥°¥é¥à¤Ï¡¢
416 \fIval\fP ¤ËÅù²Á¤Ê¥ª¥×¥·¥ç¥óʸ»ú¤òÂåÆþ¤¹¤ë¤³¤È¤¬¤Ç¤­¤ë)¡£
417 NULL °Ê³°¤Î¾ì¹ç¤Ë¤Ï¡¢
418 .BR getopt_long ()
419 ¤Ï 0 ¤òÊÖ¤¹¡£
420 ¤³¤Î¤È¤­¥ª¥×¥·¥ç¥ó¤¬¸«¤Ä¤«¤ë¤È \fIflag\fP ¤¬¥Ý¥¤¥ó¥È¤¹¤ëÊÑ¿ô¤Ë
421 \fIval\fP ¤¬ÂåÆþ¤µ¤ì¤ë¡£¸«¤Ä¤«¤é¤Ê¤¤¤È¤³¤ÎÊÑ¿ô¤ÏÊѹ¹¤µ¤ì¤Ê¤¤¡£
422 .TP
423 \fIval\fP
424 .\"O is the value to return, or to load into the variable pointed
425 .\"O to by \fIflag\fP.
426 ÊÖ¤êÃÍ¡¢¤Þ¤¿¤Ï \fIflag\fP ¤¬¥Ý¥¤¥ó¥È¤¹¤ëÊÑ¿ô¤Ø¥í¡¼¥É¤µ¤ì¤ëÃÍ¡£
427 .PP
428 .\"O The last element of the array has to be filled with zeros.
429 ÇÛÎó¤ÎºÇ¸å¤ÎÍ×ÁǤϡ¢Á´¤Æ 0 ¤ÇËä¤á¤é¤ì¤Æ¤¤¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
430 .PP
431 .\"O If \fIlongindex\fP is not NULL, it
432 .\"O points to a variable which is set to the index of the long option relative to
433 .\"O .IR longopts .
434 \fIlongindex\fP ¤Ï¡¢NULL ¤Ç¤Ê¤±¤ì¤Ð¡¢
435 Ť¤¥ª¥×¥·¥ç¥ó¤Î¥¤¥ó¥Ç¥Ã¥¯¥¹¤ò
436 .I longopts
437 ¤«¤é¤ÎÁêÂаÌÃ֤Ȥ·¤ÆÊÝ»ý¤·¤Æ¤¤¤ëÊÑ¿ô¤Ø¤Î¥Ý¥¤¥ó¥¿¤È¤Ê¤ë¡£
438 .PP
439 .\"O .BR getopt_long_only ()
440 .\"O is like
441 .\"O .BR getopt_long (),
442 .\"O but \(aq\-\(aq as well
443 .\"O as "\-\-" can indicate a long option.
444 .\"O If an option that starts with \(aq\-\(aq
445 .\"O (not "\-\-") doesn't match a long option, but does match a short option,
446 .\"O it is parsed as a short option instead.
447 .BR getopt_long_only ()
448 ¤Ï
449 .BR getopt_long ()
450 ¤ÈƱÍͤÎÆ°ºî¤ò¤¹¤ë¤¬¡¢ \(aq\-\(aq ¤â "\-\-" ¤ÈƱÍͤˡ¢
451 Ť¤¥ª¥×¥·¥ç¥ó¤È¤·¤Æ°·¤ï¤ì¤ë¡£\(aq\-\(aq ¤Ç»Ï¤Þ¤ë
452 ("\-\-" °Ê³°¤Î) ¥ª¥×¥·¥ç¥ó¤¬¡¢Ä¹¤¤¤â¤Î¤Ë¤Ï¥Þ¥Ã¥Á¤·¤Ê¤¤¤¬Ã»¤¤¤â¤Î¤Ë
453 ¥Þ¥Ã¥Á¤¹¤ë¾ì¹ç¤Ë¤ª¤¤¤Æ¤Ï¡¢¤½¤ì¤Ïû¤¤¥ª¥×¥·¥ç¥ó¤È¤·¤Æ²ò¼á¤µ¤ì¤ë¡£
454 .\"O .SH "RETURN VALUE"
455 .SH ÊÖ¤êÃÍ
456 .\"O If an option was successfully found, then
457 .\"O .BR getopt ()
458 .\"O returns the option character.
459 .\"O If all command-line options have been parsed, then
460 .\"O .BR getopt ()
461 .\"O returns \-1.
462 .\"O If
463 .\"O .BR getopt ()
464 .\"O encounters an option character that was not in
465 .\"O .IR optstring ,
466 .\"O then \(aq?\(aq is returned.
467 .\"O If
468 .\"O .BR getopt ()
469 .\"O encounters an option with a missing argument,
470 .\"O then the return value depends on the first character in
471 .\"O .IR optstring :
472 .\"O if it is \(aq:\(aq, then \(aq:\(aq is returned; otherwise \(aq?\(aq is returned.
473 ¥ª¥×¥·¥ç¥ó¤¬Àµ¾ï¤Ë¸«¤Ä¤«¤ì¤Ð
474 .BR getopt ()
475 ¤Ï¤½¤Î¥ª¥×¥·¥ç¥óʸ»ú¤òÊÖ¤¹¡£
476 ¤¹¤Ù¤Æ¤Î¥³¥Þ¥ó¥É¥é¥¤¥ó¥ª¥×¥·¥ç¥ó¤Î²òÀϤ¬½ª¤ï¤Ã¤¿¤é¡¢
477 .BR getopt ()
478 ¤Ï \-1 ¤òÊÖ¤¹¡£
479 .I optstring
480 ¤Ë´Þ¤Þ¤ì¤Ê¤¤¥ª¥×¥·¥ç¥óʸ»ú¤¬¸«¤Ä¤«¤ë¤È¡¢\(aq?\(aq ¤òÊÖ¤¹¡£
481 °ú¤­¿ô¤¬Â­¤ê¤Ê¤¤¥ª¥×¥·¥ç¥ó¤¬¸«¤Ä¤«¤Ã¤¿¾ì¹ç¡¢
482 ÊÖ¤êÃͤÏ
483 .I optstring
484 ¤ÎºÇ½é¤Îʸ»ú¤Ë¤è¤ë°Û¤Ê¤ë: ºÇ½é¤Îʸ»ú¤¬ \(aq:\(aq ¤Ç¤¢¤ì¤Ð \(aq:\(aq ¤òÊÖ¤·¡¢
485 ¤½¤ì°Ê³°¤Î¾ì¹ç¤Ï \(aq?\(aq ¤òÊÖ¤¹¡£
486 .PP
487 .\"O .BR getopt_long ()
488 .\"O and
489 .\"O .BR getopt_long_only ()
490 .\"O also return the option
491 .\"O character when a short option is recognized.
492 .\"O For a long option, they
493 .\"O return \fIval\fP if \fIflag\fP is NULL, and 0 otherwise.
494 .\"O Error and \-1 returns are the same as for
495 .\"O .BR getopt (),
496 .\"O plus \(aq?\(aq for an
497 .\"O ambiguous match or an extraneous parameter.
498 .BR getopt_long ()
499 ¤È
500 .BR getopt_long_only ()
501 ¤â¡¢
502 û¤¤¥ª¥×¥·¥ç¥óʸ»ú¤òǧ¼±¤·¤¿¾ì¹ç¤Ë¤Ï¤½¤Îʸ»ú¤òÊÖ¤¹¡£
503 Ť¤¥ª¥×¥·¥ç¥ó¤ËÂФ·¤Æ¤Ï¡¢
504 \fIflag\fP ¤¬ NULL ¤Ê¤é \fIval\fP ¤òÊÖ¤·¡¢
505 \fIflag\fP ¤¬ NULL °Ê³°¤Ê¤é 0 ¤òÊÖ¤¹¡£
506 ¥¨¥é¡¼¤È \-1 ¤ÎÊÖ¤êÃͤÏ
507 .BR getopt ()
508 ¤ÈƱ¤¸¤Ç¤¢¤ë¡£
509 ¤µ¤é¤Ë \(aq?\(aq ¤Ï¡¢¥Þ¥Ã¥Á¤¬³ÎÄê¤Ç¤­¤Ê¤¤¾ì¹ç¤ä;ʬ¤Ê¥Ñ¥é¥á¡¼¥¿¡¼¤¬¤¢¤ë¾ì¹ç¤Ë¤âÊ֤롣
510 .\"O .SH ENVIRONMENT
511 .SH ´Ä¶­
512 .TP
513 .B POSIXLY_CORRECT
514 .\"O If this is set, then option processing stops as soon as a nonoption
515 .\"O argument is encountered.
516 ¤³¤ì¤¬ÀßÄꤵ¤ì¤Æ¤¤¤ë¤È¡¢È󥪥ץ·¥ç¥ó¤Î°ú¤­¿ô¤ËÅþ㤷¤¿»þÅÀ¤Ç¥ª¥×¥·¥ç¥ó
517 ¤ËÂФ¹¤ëÁàºî¤¬Ää»ß¤µ¤ì¤ë¡£
518 .TP
519 .B _<PID>_GNU_nonoption_argv_flags_
520 .\"O This variable was used by
521 .\"O .BR bash (1)
522 .\"O 2.0 to communicate to glibc which arguments are the results of
523 .\"O wildcard expansion and so should not be considered as options.
524 .\"O This behavior was removed in
525 .\"O .BR bash (1)
526 .\"O version 2.01, but the support remains in glibc.
527 ¤³¤ÎÊÑ¿ô¤Ï
528 .BR bash (1)
529 2.0 ¤¬ glibc ¤ÈÄÌ¿®¤¹¤ë¤¿¤á¤ËÍѤ¤¤é¤ì¤¿¡£
530 ¤É¤Î°ú¤­¿ô¤¬¥ï¥¤¥ë¥É¥«¡¼¥É¤òŸ³«¤·¤¿·ë²Ì¤Ç¡¢
531 ¤·¤¿¤¬¤Ã¤Æ¥ª¥×¥·¥ç¥ó¤È¤ß¤Ê¤¹¤Ù¤­¤Ç¤Ê¤¤¤«¤òÃΤ餻¤ë¤â¤Î¤Ç¤¢¤ë¡£
532 ¤³¤Îµ¡Ç½¤Ï
533 .BR bash (1)
534 ¤Î¥Ð¡¼¥¸¥ç¥ó 2.01 ¤Çºï½ü¤µ¤ì¤¿¤¬¡¢glibc ¤Ë¤Ï¤Þ¤À»Ä¤Ã¤Æ¤¤¤ë¡£
535 .\"O .SH "CONFORMING TO"
536 .SH ½àµò
537 .TP
538 .BR getopt ():
539 .\"O POSIX.2 and POSIX.1-2001,
540 .\"O provided the environment variable
541 .\"O .B POSIXLY_CORRECT
542 .\"O is set.
543 .\"O Otherwise, the elements of \fIargv\fP aren't really const, because we
544 .\"O permute them.
545 .\"O We pretend they're const in the prototype to be
546 .\"O compatible with other systems.
547 ´Ä¶­ÊÑ¿ô
548 .B POSIXLY_CORRECT
549 ¤¬ÀßÄꤵ¤ì¤Æ¤¤¤ë¾ì¹ç¤Ï POSIX.2 ¤È POSIX.1-2001 ¤Ë½àµò¤¹¤ë¡£
550 ¾¤Î¾ì¹ç¤Ï \fIargv\fP ¤ÎÍ×ÁǤÏËÜÅö¤Î°ÕÌ£¤Ç¤ÎÄê¿ô¤Ë¤Ï¤Ê¤é¤Ê¤¤¡£
551 ¤Ê¤¼¤Ê¤é½ç½ø¤¬Êѹ¹¤µ¤ì¤Æ¤·¤Þ¤¦¤«¤é¤Ç¤¢¤ë¡£
552 ¤¿¤À¤·¤½¤ì¤é¤Ï¡¢¥×¥í¥È¥¿¥¤¥×¤Ç¤ÏÄê¿ô¤Ç¤¢¤ë¤«¤Î¤è¤¦¤Ë¤·¤Æ¤¢¤ë¡£
553 ¤³¤ì¤Ï¾¤Î¥·¥¹¥Æ¥à¤È¤Î¸ß´¹À­¤Î¤¿¤á¤Ç¤¢¤ë¡£
554
555 .\"O The use of \(aq+\(aq and \(aq\-\(aq in
556 .\"O .I optstring
557 .\"O is a GNU extension.
558 .I optstring
559 ¤Ç \(aq+\(aq ¤ä \(aq\-\(aq ¤ò»È¤¦¤Î¤Ï GNU ¤Ë¤è¤ë³ÈÄ¥¤Ç¤¢¤ë.
560
561 .\"O On some older implementations,
562 .\"O .BR getopt ()
563 .\"O was declared in
564 .\"O .IR <stdio.h> .
565 .\"O SUSv1 permitted the declaration to appear in either
566 .\"O .I <unistd.h>
567 .\"O or
568 .\"O .IR <stdio.h> .
569 .\"O POSIX.1-2001 marked the use if
570 .\"O .I <stdio.h>
571 .\"O for this purpose as LEGACY.
572 .\"O POSIX.1-2001 does not allow the declaration to appear in
573 .\"O .IR <stdio.h> .
574 ¸Å¤¤¼ÂÁõ¤Î¤¤¤¯¤Ä¤«¤Ç¤Ï¡¢
575 .BR getopt ()
576 ¤Ï
577 .I <stdio.h>
578 ¤ÇÀë¸À¤µ¤ì¤Æ¤¤¤¿¡£
579 SUSv1 ¤Ç¤Ï¡¢
580 .I <unistd.h>
581 ¤«
582 .I <stdio.h>
583 ¤Î¤É¤Á¤é¤«¤Ç
584 Àë¸À¤·¤Æ¤â¤è¤«¤Ã¤¿¡£
585 POSIX.1-2001 ¤Ç¤Ï¡¢
586 .B getopt
587 ¤ÎÀë¸À¤ò
588 .I <stdio.h>
589 ¤Ç¹Ô¤¦¤Î¤Ï¡Ö²áµî¤Î̾»Ä¡×¤Ç¤¢¤ë¤È¤µ¤ì¤¿¡£
590 POSIX.1-2001 ¤Ç¤Ï
591 .I <stdio.h>
592 ¤ÇÀë¸À¤ò¹Ô¤¦¤³¤È¤òǧ¤á¤Æ¤¤¤Ê¤¤¡£
593 .TP
594 .\"O .BR getopt_long "() and " getopt_long_only ():
595 .\"O These functions are GNU extensions.
596 .BR getopt_long "(), " getopt_long_only ():
597 ¤³¤ì¤é¤Î´Ø¿ô¤Ï GNU ¤Ë¤è¤ë³ÈÄ¥¤Ç¤¢¤ë¡£
598 .\"O .SH NOTES
599 .SH Ãí°Õ
600 .\"O A program that scans multiple argument vectors,
601 .\"O or rescans the same vector more than once,
602 .\"O and wants to make use of GNU extensions such as \(aq+\(aq
603 .\"O and \(aq\-\(aq at the start of
604 .\"O .IR optstring ,
605 .\"O or changes the value of
606 .\"O .B POSIXLY_CORRECT
607 .\"O between scans,
608 .\"O must reinitialize
609 .\"O .BR getopt ()
610 .\"O by resetting
611 .\"O .I optind
612 .\"O to 0, rather than the traditional value of 1.
613 .\"O (Resetting to 0 forces the invocation of an internal initialization
614 .\"O routine that rechecks
615 .\"O .B POSIXLY_CORRECT
616 .\"O and checks for GNU extensions in
617 .\"O .IR optstring .)
618 Ê£¿ô¤Î°ú¤­¿ô¥Ù¥¯¥È¥ë¤ò¥¹¥­¥ã¥ó¤·¤¿¤ê¡¢Æ±¤¸°ú¤­¿ô¥Ù¥¯¥È¥ë¤òÆó²ó°Ê¾å
619 ¥¹¥­¥ã¥ó¤¹¤ë¤è¤¦¤Ê¥×¥í¥°¥é¥à¤Ç¡¢
620 .I optstring
621 ¤ÎÀèƬ¤Ç \(aq+\(aq ¤ä \(aq\-\(aq ¤È¤¤¤Ã¤¿ GNU ¤Ë¤è¤ë³ÈÄ¥µ¡Ç½¤ò»ÈÍѤ·¤¿¤ê¡¢
622 °ú¤­¿ô¥Ù¥¯¥È¥ë¤ÎÀÚ¤êÂؤ¨»þ¤Ë
623 .B POSIXLY_CORRECT
624 ¤ÎÃͤòÊѹ¹¤·¤¿¤ê¤¹¤ë¾ì¹ç¤Ë¤Ï¡¢
625 .I optind
626 ¤òÅÁÅýŪ¤Ê 1 ¤Ç¤Ï¤Ê¤¯ 0 ¤Ë¥ê¥»¥Ã¥È¤¹¤ë¤³¤È¤Ç
627 .BR getopt ()
628 ¤òºÆ½é´ü²½¤·¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤
629 (0 ¤Ë¥ê¥»¥Ã¥È¤¹¤ë¤³¤È¤Ç¡¢
630 .B POSIXLY_CORRECT
631 ¤ä
632 .I optstring
633 ¤Î GNU ³ÈÄ¥µ¡Ç½¤Î¥Á¥§¥Ã¥¯¤ò¹Ô¤¦ÆâÉô½é´ü²½¥ë¡¼¥Á¥ó¤¬µ¯Æ°¤µ¤ì¤ë)¡£
634 .\"O .SH "BUGS"
635 .SH ¥Ð¥°
636 .\"O The POSIX.2 specification of
637 .\"O .BR getopt ()
638 .\"O has a technical error described in POSIX.2 Interpretation 150.
639 .\"O The GNU
640 .\"O implementation (and probably all other implementations) implements the
641 .\"O correct behavior rather than that specified.
642 POSIX.2 ¤Ë¤ª¤±¤ë
643 .BR getopt ()
644 ¤Î»ÅÍͤˤϵ»½ÑŪ¤ÊÌäÂ꤬¤¢¤ê¡¢
645 ¤½¤ÎÆâÍƤϠPOSIX.2 Interpretation 150 ¤Ëµ­¤µ¤ì¤Æ¤¤¤ë¡£
646 GNU ¤Ë¤è¤ë¼ÂÁõ¤Ç¤Ï (¤ª¤½¤é¤¯Â¾¤Î¤¹¤Ù¤Æ¤Î¼ÂÁõ¤Ç¤â)¡¢
647 »ÅÍͤȰۤʤëÀµ¤·¤¤Æ°ºî¤ò¤¹¤ë¤è¤¦¤Ë¼ÂÁõ¤µ¤ì¤Æ¤¤¤ë¡£
648 .\"O .SH EXAMPLE
649 .SH Îã
650 .\"O The following trivial example program uses
651 .\"O .BR getopt ()
652 .\"O to handle two program options:
653 .\"O .IR \-n ,
654 .\"O with no associated value; and
655 .\"O .IR "\-t val" ,
656 .\"O which expects an associated value.
657 °Ê²¼¤Ë¼¨¤¹´Êñ¤Ê¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à¤Ç¤Ï¡¢
658 Æó¼ïÎà¤Î¥×¥í¥°¥é¥à¥ª¥×¥·¥ç¥ó¤ò°·¤¦¤Î¤Ë
659 .BR getopt ()
660 ¤ò»ÈÍѤ·¤Æ¤¤¤ë¡£°ì¤Ä¤ÏÃͤòȼ¤ï¤Ê¤¤
661 .I \-n
662 ¤Ç¡¢¤â¤¦°ì¤Ä¤ÏÂбþ¤¹¤ëÃͤ¬É¬ÍפÊ
663 .I "\-t val"
664 ¤Ç¤¢¤ë¡£
665 .nf
666 .sp
667 #include <unistd.h>
668 #include <stdlib.h>
669 #include <stdio.h>
670
671 int
672 main(int argc, char *argv[])
673 {
674     int flags, opt;
675     int nsecs, tfnd;
676
677     nsecs = 0;
678     tfnd = 0;
679     flags = 0;
680     while ((opt = getopt(argc, argv, "nt:")) != \-1) {
681         switch (opt) {
682         case \(aqn\(aq:
683             flags = 1;
684             break;
685         case \(aqt\(aq:
686             nsecs = atoi(optarg);
687             tfnd = 1;
688             break;
689         default: /* \(aq?\(aq */
690             fprintf(stderr, "Usage: %s [\-t nsecs] [\-n] name\\n",
691                     argv[0]);
692             exit(EXIT_FAILURE);
693         }
694     }
695
696     printf("flags=%d; tfnd=%d; optind=%d\\n", flags, tfnd, optind);
697
698     if (optind >= argc) {
699         fprintf(stderr, "Expected argument after options\\n");
700         exit(EXIT_FAILURE);
701     }
702
703     printf("name argument = %s\\n", argv[optind]);
704
705     /* Other code omitted */
706
707     exit(EXIT_SUCCESS);
708 }
709 .fi
710 .PP
711 .\"O The following example program illustrates the use of
712 .\"O .BR getopt_long ()
713 .\"O with most of its features.
714 °Ê²¼¤Ï¡¢
715 .BR getopt_long ()
716 ¤Î»ÈÍÑË¡¤ò¡¢¤Û¤Ü¤¹¤Ù¤Æ¤Îµ¡Ç½¤Ë¤Ä¤¤¤Æ¼¨¤·¤¿¥×¥í¥°¥é¥à¤ÎÎã¤Ç¤¢¤ë¡£
717 .nf
718 .sp
719 #include <stdio.h>     /* for printf */
720 #include <stdlib.h>    /* for exit */
721 #include <getopt.h>
722
723 int
724 main(int argc, char **argv) {
725     int c;
726     int digit_optind = 0;
727
728     while (1) {
729         int this_option_optind = optind ? optind : 1;
730         int option_index = 0;
731         static struct option long_options[] = {
732             {"add", 1, 0, 0},
733             {"append", 0, 0, 0},
734             {"delete", 1, 0, 0},
735             {"verbose", 0, 0, 0},
736             {"create", 1, 0, \(aqc\(aq},
737             {"file", 1, 0, 0},
738             {0, 0, 0, 0}
739         };
740
741         c = getopt_long(argc, argv, "abc:d:012",
742                  long_options, &option_index);
743         if (c == \-1)
744             break;
745
746         switch (c) {
747         case 0:
748             printf("option %s", long_options[option_index].name);
749             if (optarg)
750                 printf(" with arg %s", optarg);
751             printf("\\n");
752             break;
753
754         case \(aq0\(aq:
755         case \(aq1\(aq:
756         case \(aq2\(aq:
757             if (digit_optind != 0 && digit_optind != this_option_optind)
758               printf("digits occur in two different argv\-elements.\\n");
759             digit_optind = this_option_optind;
760             printf("option %c\\n", c);
761             break;
762
763         case \(aqa\(aq:
764             printf("option a\\n");
765             break;
766
767         case \(aqb\(aq:
768             printf("option b\\n");
769             break;
770
771         case \(aqc\(aq:
772             printf("option c with value \(aq%s\(aq\\n", optarg);
773             break;
774
775         case \(aqd\(aq:
776             printf("option d with value \(aq%s\(aq\\n", optarg);
777             break;
778
779         case \(aq?\(aq:
780             break;
781
782         default:
783             printf("?? getopt returned character code 0%o ??\\n", c);
784         }
785     }
786
787     if (optind < argc) {
788         printf("non-option ARGV\-elements: ");
789         while (optind < argc)
790             printf("%s ", argv[optind++]);
791         printf("\\n");
792     }
793
794     exit(EXIT_SUCCESS);
795 }
796 .fi
797 .\"O .SH "SEE ALSO"
798 .SH ´ØÏ¢¹àÌÜ
799 .BR getsubopt (3),
800 .BR feature_test_macros (7)