OSDN Git Service

(split) LDP v3.30, v3.31 の定型的な変更内容を反映。
[linuxjm/LDP_man-pages.git] / release / 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-11-01 "GNU" "Linux Programmer's Manual"
54 .SH Ì¾Á°
55 getopt, getopt_long, getopt_long_only,
56 optarg, optind, opterr, optopt \- ¥³¥Þ¥ó¥É¥é¥¤¥ó¥ª¥×¥·¥ç¥ó¤ò²ò¼á¤¹¤ë
57 .SH ½ñ¼°
58 .nf
59 .B #include <unistd.h>
60 .sp
61 .BI "int getopt(int " argc ", char * const " argv[] ,
62 .BI "           const char *" optstring );
63 .sp
64 .BI "extern char *" optarg ;
65 .BI "extern int " optind ", " opterr ", " optopt ;
66 .sp
67 .B #include <getopt.h>
68 .sp
69 .BI "int getopt_long(int " argc ", char * const " argv[] ,
70 .BI "           const char *" optstring ,
71 .BI "           const struct option *" longopts ", int *" longindex );
72 .sp
73 .BI "int getopt_long_only(int " argc ", char * const " argv[] ,
74 .BI "           const char *" optstring ,
75 .BI "           const struct option *" longopts ", int *" longindex );
76 .fi
77 .sp
78 .in -4n
79 glibc ¸þ¤±¤Îµ¡Ç½¸¡ºº¥Þ¥¯¥í¤ÎÍ×·ï
80 .RB ( feature_test_macros (7)
81 »²¾È):
82 .ad l
83 .in
84 .sp
85 .BR getopt ():
86 _POSIX_C_SOURCE\ >=\ 2 || _XOPEN_SOURCE
87 .br
88 .BR getopt_long (),
89 .BR getopt_long_only ():
90 _GNU_SOURCE
91 .ad b
92 .SH ÀâÌÀ
93 .BR getopt ()
94 ´Ø¿ô¤Ï¥³¥Þ¥ó¥É¥é¥¤¥ó°ú¤­¿ô¤ò²ò¼á¤¹¤ë¡£
95 .BR getopt ()
96 ¤¬¤È¤ë°ú¤­¿ô
97 .I argc
98 ¤È
99 .I argv
100 ¤Ï¡¢¤½¤ì¤¾¤ì¥×¥í¥°¥é¥à¤Îµ¯Æ°»þ¤Ë
101 .IR main ()
102 ´Ø¿ô¤ËÅϤµ¤ì¤¿°ú¤­¿ô¤Î¸Ä¿ô¤ÈÇÛÎó¤Ç¤¢¤ë¡£
103 \fIargv\fP ¤ÎÍ×ÁǤΤ¦¤Á \(aq\-\(aq ¤Ç»Ï¤Þ¤ë¤â¤Î
104 (¤«¤Ä "\-" Ã±ÆȤä "\-\-" Ã±ÆȤǤϤʤ¤¤â¤Î) ¤Ï
105 ¥ª¥×¥·¥ç¥óÍ×ÁÇ (option element) ¤È¤ß¤Ê¤µ¤ì¤ë¡£
106 ¤³¤ÎÍ×ÁǤ«¤éÀèƬ¤Î \(aq\-\(aq ¤ò½ü¤¤¤¿Ê¸»ú¤Ï
107 ¥ª¥×¥·¥ç¥óʸ»ú (option character) ¤È¤µ¤ì¤ë¡£
108 .BR getopt ()
109 ¤Ï¡¢·«¤êÊÖ¤·¸Æ¤Ó½Ð¤µ¤ì¤ë¤´¤È¤Ë¡¢¼¡¤Î¥ª¥×¥·¥ç¥óʸ»ú¤òÊÖ¤¹¡£
110 .PP
111 ÊÑ¿ô
112 .I optind
113 ¤Ï¡¢
114 .I argv
115 ¤Î¼¡¤Ë½èÍý¤µ¤ì¤ëÍ×ÁǤΥ¤¥ó¥Ç¥Ã¥¯¥¹¤Ç¤¢¤ë¡£
116 ¥·¥¹¥Æ¥à¤Ë¤è¤ê¤³¤ÎÊÑ¿ô¤ÎÃͤϠ1 ¤Ë½é´ü²½¤µ¤ì¤ë¡£
117 ¸Æ¤Ó½Ð¤·Â¦¤Ç¤³¤ÎÃͤò 1 ¤Ë¥ê¥»¥Ã¥È¤¹¤ë¤³¤È¤Ç¡¢Æ±¤¸
118 .I argv
119 ¤Î¥¹¥­¥ã¥ó¤ò¤ä¤êľ¤·¤¿¤ê¡¢¿·¤·¤¤°ú¤­¿ô¥Ù¥¯¥È¥ë¤ò¥¹¥­¥ã¥ó¤¹¤ë¤³¤È¤¬¤Ç¤­¤ë¡£
120 .PP
121 ¿·¤¿¤Ê¥ª¥×¥·¥ç¥óʸ»ú¤ò¸«¤Ä¤±¤ë¤È¡¢
122 .BR getopt ()
123 ¤Ï¤½¤Îʸ»ú¤òÊÖ¤·¡¢
124 ³°ÉôÊÑ¿ô \fIoptind\fP ¤È¥¹¥¿¥Æ¥£¥Ã¥¯¤ÊÊÑ¿ô \fInextchar\fP ¤ò¹¹¿·¤¹¤ë¡£
125 ¤³¤ì¤é¤Ë¤è¤Ã¤Æ¡¢
126 .BR getopt ()
127 ¤Ï¼¡²ó¤Î¸Æ¤Ó½Ð¤·¤ÎºÝ¤Ë¡¢
128 °Ê¹ß¤Î¥ª¥×¥·¥ç¥óʸ»ú¤ä \fIargv\fP Í×ÁǤΥ¹¥­¥ã¥ó¤ò·Ñ³¤Ç¤­¤ë¡£
129 .PP
130 ¥ª¥×¥·¥ç¥óʸ»ú¤¬¤½¤ì°Ê¾å¸«¤Ä¤«¤é¤Ê¤¯¤Ê¤ë¤È¡¢
131 .BR getopt ()
132 ¤Ï
133 \-1 ¤òÊÖ¤¹¡£¤½¤·¤Æ \fIoptind\fP ¤Ï¡¢\fIargv\fP ¤ÎÍ×ÁǤΤ¦¤Á¡¢
134 ¥ª¥×¥·¥ç¥ó¤Ç¤Ê¤¤ºÇ½é¤ÎÍ×ÁǤò¼¨¤¹¤è¤¦¤Ë¤Ê¤ë¡£
135 .PP
136 .I optstring
137 ¤Ï¼õ¤±ÉÕ¤±¤ë¥ª¥×¥·¥ç¥óʸ»ú¤«¤é¤Ê¤ëʸ»úÎó¤Ç¤¢¤ë¡£
138 ʸ»ú¤Î¤¢¤È¤Ë¥³¥í¥ó (:) ¤¬ÃÖ¤«¤ì¤Æ¤¤¤ë¾ì¹ç¤Ï¡¢
139 ¥ª¥×¥·¥ç¥ó¤Ë¤Ï°ú¤­¿ô¤¬É¬ÍפǤ¢¤ë¤³¤È¤ò¼¨¤¹¡£
140 ¤³¤Î¤È¤­
141 .BR getopt ()
142 ¤Ï¡¢¸½ºßÃíÌܤ·¤Æ¤¤¤ë
143 \fIargv\fP Í×ÁǤǡ¢¥ª¥×¥·¥ç¥óʸ»ú¤Ë°ú¤­Â³¤¯¥Æ¥­¥¹¥È¤Ø¤Î¥Ý¥¤¥ó¥¿¤«¡¢
144 ¤¢¤ë¤¤¤Ï¼¡¤Î \fIargv\fP Í×ÁǤΥƥ­¥¹¥È¤Ø¤Î¥Ý¥¤¥ó¥¿¤ò
145 .I optarg
146 ¤ËÂåÆþ¤¹¤ë¡£
147 2 ¸ÄϢ³¤·¤Æ¥³¥í¥ó¤¬ÃÖ¤«¤ì¤Æ¤¤¤ë¾ì¹ç¤Ï¡¢
148 ¤½¤Î¥ª¥×¥·¥ç¥ó¤Ï°ú¤­¿ô¤ò¤È¤Ã¤Æ¤â¤È¤é¤Ê¤¯¤Æ¤â¤è¤¤¡£
149 ¸½ºß¤Î \fIargv\fP Í×ÁǤ˥ƥ­¥¹¥È¤¬¤¢¤ì¤Ð
150 (¤Ä¤Þ¤ê¡¢"\-oarg" ¤Î¤è¤¦¤Ë¡¢¥ª¥×¥·¥ç¥ó̾¼«¿È¤ÈƱ¤¸¥ï¡¼¥ÉÆâ¤Ë
151 ¥Æ¥­¥¹¥È¤¬¤¢¤ë¾ì¹ç)¡¢¤½¤ì¤¬ \fIoptarg\fP ¤ËÊÖ¤µ¤ì¤ë¡£
152 ¤Ê¤±¤ì¤Ð \fIoptarg\fP ¤Ï 0 ¤ËÀßÄꤵ¤ì¤ë¡£
153 ¤³¤ì¤Ï GNU ¤Ë¤è¤ë³ÈÄ¥¤Ç¤¢¤ë¡£
154 .I optstring
155 ¤Ë
156 .B W
157 ¤È¤½¤ì¤Ë³¤¯¥»¥ß¥³¥í¥ó¤¬Æþ¤Ã¤Æ¤¤¤ë¤È¡¢
158 .B \-W foo
159 ¤ÏŤ¤¥ª¥×¥·¥ç¥ó
160 .B \-\-foo
161 ¤ÈƱ¤¸¤è¤¦¤Ë°·¤ï¤ì¤ë
162 (POSIX.2 ¤Ï
163 .B \-W
164 ¥ª¥×¥·¥ç¥ó¤ò¼ÂÁõ°Í¸¤Î³ÈÄ¥¤È¤·¤ÆͽÌ󤷤Ƥ¤¤ë)¡£
165 ¤³¤ÎÆ°ºî¤Ï GNU ¤Ë¤è¤ë³ÈÄ¥¤Ç¤¢¤ê¡¢glibc 2 °ÊÁ°¤Î¥é¥¤¥Ö¥é¥ê¤Ç¤Ï
166 ÍøÍѤǤ­¤Ê¤¤¡£
167 .PP
168 ¥Ç¥Õ¥©¥ë¥È¤Ç¤Ï
169 .BR getopt ()
170 ¤Ï \fIargv\fP ¤ò¥¹¥­¥ã¥ó¤¹¤ëºÝ¤Ë½ç½ø¤òÊѹ¹¤·¡¢
171 ¥ª¥×¥·¥ç¥ó°Ê³°¤ÎÍ×ÁǤòºÇ¸å¤Ë°ÜÆ°¤¹¤ë¡£
172 ¾¤Ë¤â 2 ¤Ä¤Î¥â¡¼¥É¤¬¼ÂÁõ¤µ¤ì¤Æ¤¤¤ë¡£
173 \fIoptstring\fP ¤ÎÀèƬʸ»ú¤¬ \(aq+\(aq ¤Ç¤¢¤ë¤«¡¢´Ä¶­ÊÑ¿ô
174 .B POSIXLY_CORRECT
175 ¤¬ÀßÄꤵ¤ì¤Æ¤¤¤ë¾ì¹ç¤Ë¤Ï¡¢¥ª¥×¥·¥ç¥ó¤òÂоݤȤ¹¤ëÆ°ºî¤Ï¡¢
176 È󥪥ץ·¥ç¥ó¤Î°ú¤­¿ô¤¬¸½¤ì¤¿Ãʳ¬¤Ç½ªÎ»¤¹¤ë¡£
177 \fIoptstring\fP ¤ÎÀèƬʸ»ú¤¬ \(aq\-\(aq ¤Ç¤¢¤ë¾ì¹ç¤Ë¤Ï¡¢
178 ¥ª¥×¥·¥ç¥ó¤Ç¤Ê¤¤ \fIargv\fP Í×ÁǤϡ¢
179 ʸ»ú¥³¡¼¥É 1 ¤Î¥ª¥×¥·¥ç¥ó¤Ç¤¢¤ë¤«¤Î¤è¤¦¤Ë°·¤ï¤ì¤ë (¤³¤ì¤òÍѤ¤¤ë¥×¥í¥°¥é¥à¤Ï¡¢
180 ¥ª¥×¥·¥ç¥ó¤ä \fIargv\fP Í×ÁǤòǤ°Õ¤Î½ç½ø¤Ç¼õ¤±Æþ¤ì¡¢¤«¤Ä¤½¤ì¤é¤Î½ç½ø¤¬
181 °ÕÌ£¤ò»ý¤Ä¤è¤¦¤Ë½ñ¤«¤ì¤Æ¤¤¤ëɬÍפ¬¤¢¤ë)¡£
182 "\-\-" ¤ÏÆüì¤Ê°ú¤­¿ô¤Ç¡¢¥¹¥­¥ã¥ó¤Î¥â¡¼¥É¤Ë¤è¤é¤º¡¢
183 ¥ª¥×¥·¥ç¥ó¤Î¥¹¥­¥ã¥ó¤ò¶¯À©Åª¤Ë½ªÎ»¤µ¤»¤ë¡£
184 .PP
185 ǧ¼±¤Ç¤­¤Ê¤¤¥ª¥×¥·¥ç¥óʸ»ú¤¬¤¢¤ë¤È¡¢
186 .BR getopt ()
187 ¤Ï¥¨¥é¡¼¥á¥Ã¥»¡¼¥¸¤òɸ½à¥¨¥é¡¼½ÐÎÏ \fIstderr\fP ¤Ëɽ¼¨¤·¡¢
188 ¤½¤Îʸ»ú¤ò \fIoptopt\fP ¤ËÊݸ¤·¤Æ \(aq?\(aq ¤òÊÖ¤¹¡£
189 ¸Æ¤Ó½Ð¤·¤¿¥×¥í¥°¥é¥à¤Ç \fIopterr\fP ¤ò 0 ¤Ë¤·¤Æ¤ª¤±¤Ð¡¢
190 ¥¨¥é¡¼¥á¥Ã¥»¡¼¥¸¤Îɽ¼¨¤òÍÞÀ©¤Ç¤­¤ë¡£
191 .PP
192 .BR getopt ()
193 ¤Ï \fIargv\fP ¤ÎÃæ¤Ë
194 \fIoptstring\fP ¤Ë¤Ê¤¤¥ª¥×¥·¥ç¥óʸ»ú¤ò¸«¤Ä¤±¤¿¾ì¹ç¡¢
195 ¤Þ¤¿¤Ï¥ª¥×¥·¥ç¥ó°ú¤­¿ô¤¬Â­¤ê¤Ê¤¤¤³¤È¤¬Ê¬¤«¤Ã¤¿¾ì¹ç¡¢
196 \&\(aq?\(aq ¤òÊÖ¤·¤Æ³°ÉôÊÑ¿ô \fIoptopt\fP ¤ò¤½¤Î¥ª¥×¥·¥ç¥óʸ»ú¤ËÀßÄꤹ¤ë¡£
197 \fIoptstring\fP ¤Î (¾å¤ÇÀâÌÀ¤·¤¿¥ª¥×¥·¥ç¥ó¤Ç»ØÄê¤Ç¤­¤ë
198 \&\(aq+\(aq ¤Þ¤¿¤Ï \(aq\-\(aq ¸å¤Ë³¤¯) ºÇ½é¤Îʸ»ú¤¬
199 ¥³¥í¥ó (\(aq:\(aq) ¤Î¤È¤­¡¢
200 .BR getopt ()
201 ¤Ï¥ª¥×¥·¥ç¥ó°ú¤­¿ô¤¬Â­¤ê¤Ê¤¤¾ì¹ç¤Ë \(aq?\(aq ¤Ç¤Ï¤Ê¤¯ \(aq:\(aq ¤òÊÖ¤¹¡£
202 ¥¨¥é¡¼¤ò¸«¤Ä¤±¤¿¾ì¹ç¤Ç¡¢¤«¤Ä \fIoptstring\fP ¤ÎºÇ½é¤Îʸ»ú¤¬¥³¥í¥ó¤Ç¤Ê¤¯¡¢
203 ¤«¤Ä³°ÉôÊÑ¿ô \fIopterr\fP ¤¬ 0 ¤Ç¤Ê¤¤¾ì¹ç (¤³¤ì¤¬¥Ç¥Õ¥©¥ë¥È)¡¢
204 .BR getopt ()
205 ¤Ï¥¨¥é¡¼¥á¥Ã¥»¡¼¥¸¤òɽ¼¨¤¹¤ë¡£
206 .SS getopt_long() ¤È getopt_long_only()
207 .BR getopt_long ()
208 ´Ø¿ô¤Ï¡¢Ä¹¤¤¥ª¥×¥·¥ç¥ó (2 ¤Ä¤Î¥À¥Ã¥·¥å "\-\-" ¤Ç»Ï¤Þ¤ë¥ª¥×¥·¥ç¥ó) ¤ò
209 ¼õ¤±Æþ¤ì¤ë¤³¤È¤ò½ü¤¤¤Æ
210 .BR getopt ()
211 ¤ÈƱ¤¸¤è¤¦¤ËÆ°ºî¤¹¤ë
212 (¥×¥í¥°¥é¥à¤ËŤ¤¥ª¥×¥·¥ç¥ó¤À¤±¤¬ÅϤµ¤ì¤¿¾ì¹ç¡¢
213 .I optstring
214 ¤Ï NULL ¤Ç¤Ï¤Ê¤¯¶õʸ»úÎó ("") ¤È¤Ê¤ë)¡£
215 Ť¤¥ª¥×¥·¥ç¥ó¤Î̾Á°¤Ï¡¢Â¾¤È½Å¤Ê¤é¤Ê¤¤ÈϰϤˤª¤¤¤Æû½Ì¤Ç¤­¤ë¡£
216 ¤¢¤ë¤¤¤ÏÄêµÁ¤µ¤ì¤¿¥ª¥×¥·¥ç¥ó¤ËÀµ³Î¤Ë¥Þ¥Ã¥Á¤¹¤ë¤â¤Î¤Ç¤â (ÅöÁ³) ¤«¤Þ¤ï¤Ê¤¤¡£
217 Ť¤¥ª¥×¥·¥ç¥ó¤Ï°ú¤­¿ô¤ò¼è¤ë¤³¤È¤¬¤Ç¤­¡¢
218 .B \-\-arg=param
219 ¤Þ¤¿¤Ï
220 .B "\-\-arg param"
221 ¤È¸À¤¦·Á¼°¤Ç»ØÄꤹ¤ë¡£
222 .PP
223 .I longopts
224 ¤Ï
225 .I struct option
226 ¤ÎÍ×ÁǤ«¤é¤Ê¤ëÇÛÎó¤Î¡¢ÀèƬÍ×ÁǤؤΥݥ¤¥ó¥¿¤Ç¤¢¤ë¡£
227 .I struct option
228 ¤Ï
229 .I <getopt.h>
230 ¤Ç°Ê²¼¤Î¤è¤¦¤ËÄêµÁ¤µ¤ì¤Æ¤¤¤ë¡£
231 .in +4n
232 .nf
233 .sp
234 struct option {
235     const char *name;
236     int         has_arg;
237     int        *flag;
238     int         val;
239 };
240 .fi
241 .in
242 .PP
243 ¤½¤ì¤¾¤ì¤Î¥Õ¥£¡¼¥ë¥É¤Î°ÕÌ£¤Ï°Ê²¼¤ÎÄ̤ꡣ
244 .TP
245 .I name
246 Ť¤¥ª¥×¥·¥ç¥ó¤Î̾Á°¡£
247 .TP
248 .I has_arg
249 \fBno_argument\fP (¤Þ¤¿¤Ï 0) ¤Ê¤é¡¢¥ª¥×¥·¥ç¥ó¤Ï°ú¤­¿ô¤ò¤È¤é¤Ê¤¤¡£
250 \fBrequired_argument\fP (¤Þ¤¿¤Ï 1) ¤Ê¤é¡¢¥ª¥×¥·¥ç¥ó¤Ï°ú¤­¿ô¤òɬÍפȤ¹¤ë¡£
251 \fBoptional_argument\fP (¤Þ¤¿¤Ï 2) ¤Ê¤é¡¢¥ª¥×¥·¥ç¥ó¤Ï°ú¤­¿ô¤ò¤È¤Ã¤Æ¤â
252 ¤È¤é¤Ê¤¯¤Æ¤âÎɤ¤¡£
253 .TP
254 .I flag
255 Ť¤¥ª¥×¥·¥ç¥ó¤ËÂФ¹¤ë·ë²Ì¤ÎÊÖ¤·Êý¤ò»ØÄꤹ¤ë¡£\fIflag\fP ¤¬
256 NULL ¤Ê¤é
257 .BR getopt_long ()
258 ¤Ï \fIval\fP ¤òÊÖ¤¹
259 (Î㤨¤Ð¸Æ¤Ó½Ð¤·¸µ¤Î¥×¥í¥°¥é¥à¤Ï¡¢
260 \fIval\fP ¤ËÅù²Á¤Ê¥ª¥×¥·¥ç¥óʸ»ú¤òÂåÆþ¤¹¤ë¤³¤È¤¬¤Ç¤­¤ë)¡£
261 NULL °Ê³°¤Î¾ì¹ç¤Ë¤Ï¡¢
262 .BR getopt_long ()
263 ¤Ï 0 ¤òÊÖ¤¹¡£
264 ¤³¤Î¤È¤­¥ª¥×¥·¥ç¥ó¤¬¸«¤Ä¤«¤ë¤È \fIflag\fP ¤¬¥Ý¥¤¥ó¥È¤¹¤ëÊÑ¿ô¤Ë
265 \fIval\fP ¤¬ÂåÆþ¤µ¤ì¤ë¡£¸«¤Ä¤«¤é¤Ê¤¤¤È¤³¤ÎÊÑ¿ô¤ÏÊѹ¹¤µ¤ì¤Ê¤¤¡£
266 .TP
267 \fIval\fP
268 ÊÖ¤êÃÍ¡¢¤Þ¤¿¤Ï \fIflag\fP ¤¬¥Ý¥¤¥ó¥È¤¹¤ëÊÑ¿ô¤Ø¥í¡¼¥É¤µ¤ì¤ëÃÍ¡£
269 .PP
270 ÇÛÎó¤ÎºÇ¸å¤ÎÍ×ÁǤϡ¢Á´¤Æ 0 ¤ÇËä¤á¤é¤ì¤Æ¤¤¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
271 .PP
272 \fIlongindex\fP ¤Ï¡¢NULL ¤Ç¤Ê¤±¤ì¤Ð¡¢
273 Ť¤¥ª¥×¥·¥ç¥ó¤Î¥¤¥ó¥Ç¥Ã¥¯¥¹¤ò
274 .I longopts
275 ¤«¤é¤ÎÁêÂаÌÃ֤Ȥ·¤ÆÊÝ»ý¤·¤Æ¤¤¤ëÊÑ¿ô¤Ø¤Î¥Ý¥¤¥ó¥¿¤È¤Ê¤ë¡£
276 .PP
277 .BR getopt_long_only ()
278 ¤Ï
279 .BR getopt_long ()
280 ¤ÈƱÍͤÎÆ°ºî¤ò¤¹¤ë¤¬¡¢ \(aq\-\(aq ¤â "\-\-" ¤ÈƱÍͤˡ¢
281 Ť¤¥ª¥×¥·¥ç¥ó¤È¤·¤Æ°·¤ï¤ì¤ë¡£\(aq\-\(aq ¤Ç»Ï¤Þ¤ë
282 ("\-\-" °Ê³°¤Î) ¥ª¥×¥·¥ç¥ó¤¬¡¢Ä¹¤¤¤â¤Î¤Ë¤Ï¥Þ¥Ã¥Á¤·¤Ê¤¤¤¬Ã»¤¤¤â¤Î¤Ë
283 ¥Þ¥Ã¥Á¤¹¤ë¾ì¹ç¤Ë¤ª¤¤¤Æ¤Ï¡¢¤½¤ì¤Ïû¤¤¥ª¥×¥·¥ç¥ó¤È¤·¤Æ²ò¼á¤µ¤ì¤ë¡£
284 .SH ÊÖ¤êÃÍ
285 ¥ª¥×¥·¥ç¥ó¤¬Àµ¾ï¤Ë¸«¤Ä¤«¤ì¤Ð
286 .BR getopt ()
287 ¤Ï¤½¤Î¥ª¥×¥·¥ç¥óʸ»ú¤òÊÖ¤¹¡£
288 ¤¹¤Ù¤Æ¤Î¥³¥Þ¥ó¥É¥é¥¤¥ó¥ª¥×¥·¥ç¥ó¤Î²òÀϤ¬½ª¤ï¤Ã¤¿¤é¡¢
289 .BR getopt ()
290 ¤Ï \-1 ¤òÊÖ¤¹¡£
291 .I optstring
292 ¤Ë´Þ¤Þ¤ì¤Ê¤¤¥ª¥×¥·¥ç¥óʸ»ú¤¬¸«¤Ä¤«¤ë¤È¡¢\(aq?\(aq ¤òÊÖ¤¹¡£
293 °ú¤­¿ô¤¬Â­¤ê¤Ê¤¤¥ª¥×¥·¥ç¥ó¤¬¸«¤Ä¤«¤Ã¤¿¾ì¹ç¡¢
294 ÊÖ¤êÃͤÏ
295 .I optstring
296 ¤ÎºÇ½é¤Îʸ»ú¤Ë¤è¤ë°Û¤Ê¤ë: ºÇ½é¤Îʸ»ú¤¬ \(aq:\(aq ¤Ç¤¢¤ì¤Ð \(aq:\(aq ¤òÊÖ¤·¡¢
297 ¤½¤ì°Ê³°¤Î¾ì¹ç¤Ï \(aq?\(aq ¤òÊÖ¤¹¡£
298 .PP
299 .BR getopt_long ()
300 ¤È
301 .BR getopt_long_only ()
302 ¤â¡¢
303 û¤¤¥ª¥×¥·¥ç¥óʸ»ú¤òǧ¼±¤·¤¿¾ì¹ç¤Ë¤Ï¤½¤Îʸ»ú¤òÊÖ¤¹¡£
304 Ť¤¥ª¥×¥·¥ç¥ó¤ËÂФ·¤Æ¤Ï¡¢
305 \fIflag\fP ¤¬ NULL ¤Ê¤é \fIval\fP ¤òÊÖ¤·¡¢
306 \fIflag\fP ¤¬ NULL °Ê³°¤Ê¤é 0 ¤òÊÖ¤¹¡£
307 ¥¨¥é¡¼¤È \-1 ¤ÎÊÖ¤êÃͤÏ
308 .BR getopt ()
309 ¤ÈƱ¤¸¤Ç¤¢¤ë¡£
310 ¤µ¤é¤Ë \(aq?\(aq ¤Ï¡¢¥Þ¥Ã¥Á¤¬³ÎÄê¤Ç¤­¤Ê¤¤¾ì¹ç¤ä;ʬ¤Ê¥Ñ¥é¥á¡¼¥¿¡¼¤¬¤¢¤ë¾ì¹ç¤Ë¤âÊ֤롣
311 .SH ´Ä¶­
312 .TP
313 .B POSIXLY_CORRECT
314 ¤³¤ì¤¬ÀßÄꤵ¤ì¤Æ¤¤¤ë¤È¡¢È󥪥ץ·¥ç¥ó¤Î°ú¤­¿ô¤ËÅþ㤷¤¿»þÅÀ¤Ç¥ª¥×¥·¥ç¥ó
315 ¤ËÂФ¹¤ëÁàºî¤¬Ää»ß¤µ¤ì¤ë¡£
316 .TP
317 .B _<PID>_GNU_nonoption_argv_flags_
318 ¤³¤ÎÊÑ¿ô¤Ï
319 .BR bash (1)
320 2.0 ¤¬ glibc ¤ÈÄÌ¿®¤¹¤ë¤¿¤á¤ËÍѤ¤¤é¤ì¤¿¡£
321 ¤É¤Î°ú¤­¿ô¤¬¥ï¥¤¥ë¥É¥«¡¼¥É¤òŸ³«¤·¤¿·ë²Ì¤Ç¡¢
322 ¤·¤¿¤¬¤Ã¤Æ¥ª¥×¥·¥ç¥ó¤È¤ß¤Ê¤¹¤Ù¤­¤Ç¤Ê¤¤¤«¤òÃΤ餻¤ë¤â¤Î¤Ç¤¢¤ë¡£
323 ¤³¤Îµ¡Ç½¤Ï
324 .BR bash (1)
325 ¤Î¥Ð¡¼¥¸¥ç¥ó 2.01 ¤Çºï½ü¤µ¤ì¤¿¤¬¡¢glibc ¤Ë¤Ï¤Þ¤À»Ä¤Ã¤Æ¤¤¤ë¡£
326 .SH ½àµò
327 .TP
328 .BR getopt ():
329 ´Ä¶­ÊÑ¿ô
330 .B POSIXLY_CORRECT
331 ¤¬ÀßÄꤵ¤ì¤Æ¤¤¤ë¾ì¹ç¤Ï POSIX.2 ¤È POSIX.1-2001 ¤Ë½àµò¤¹¤ë¡£
332 ¾¤Î¾ì¹ç¤Ï \fIargv\fP ¤ÎÍ×ÁǤÏËÜÅö¤Î°ÕÌ£¤Ç¤ÎÄê¿ô¤Ë¤Ï¤Ê¤é¤Ê¤¤¡£
333 ¤Ê¤¼¤Ê¤é½ç½ø¤¬Êѹ¹¤µ¤ì¤Æ¤·¤Þ¤¦¤«¤é¤Ç¤¢¤ë¡£
334 ¤¿¤À¤·¤½¤ì¤é¤Ï¡¢¥×¥í¥È¥¿¥¤¥×¤Ç¤ÏÄê¿ô¤Ç¤¢¤ë¤«¤Î¤è¤¦¤Ë¤·¤Æ¤¢¤ë¡£
335 ¤³¤ì¤Ï¾¤Î¥·¥¹¥Æ¥à¤È¤Î¸ß´¹À­¤Î¤¿¤á¤Ç¤¢¤ë¡£
336
337 .I optstring
338 ¤Ç \(aq+\(aq ¤ä \(aq\-\(aq ¤ò»È¤¦¤Î¤Ï GNU ¤Ë¤è¤ë³ÈÄ¥¤Ç¤¢¤ë.
339
340 ¸Å¤¤¼ÂÁõ¤Î¤¤¤¯¤Ä¤«¤Ç¤Ï¡¢
341 .BR getopt ()
342 ¤Ï
343 .I <stdio.h>
344 ¤ÇÀë¸À¤µ¤ì¤Æ¤¤¤¿¡£
345 SUSv1 ¤Ç¤Ï¡¢
346 .I <unistd.h>
347 ¤«
348 .I <stdio.h>
349 ¤Î¤É¤Á¤é¤«¤Ç
350 Àë¸À¤·¤Æ¤â¤è¤«¤Ã¤¿¡£
351 POSIX.1-2001 ¤Ç¤Ï¡¢
352 .B getopt
353 ¤ÎÀë¸À¤ò
354 .I <stdio.h>
355 ¤Ç¹Ô¤¦¤Î¤Ï¡Ö²áµî¤Î̾»Ä¡×¤Ç¤¢¤ë¤È¤µ¤ì¤¿¡£
356 POSIX.1-2001 ¤Ç¤Ï
357 .I <stdio.h>
358 ¤ÇÀë¸À¤ò¹Ô¤¦¤³¤È¤òǧ¤á¤Æ¤¤¤Ê¤¤¡£
359 .TP
360 .BR getopt_long "(), " getopt_long_only ():
361 ¤³¤ì¤é¤Î´Ø¿ô¤Ï GNU ¤Ë¤è¤ë³ÈÄ¥¤Ç¤¢¤ë¡£
362 .SH Ãí°Õ
363 Ê£¿ô¤Î°ú¤­¿ô¥Ù¥¯¥È¥ë¤ò¥¹¥­¥ã¥ó¤·¤¿¤ê¡¢Æ±¤¸°ú¤­¿ô¥Ù¥¯¥È¥ë¤òÆó²ó°Ê¾å
364 ¥¹¥­¥ã¥ó¤¹¤ë¤è¤¦¤Ê¥×¥í¥°¥é¥à¤Ç¡¢
365 .I optstring
366 ¤ÎÀèƬ¤Ç \(aq+\(aq ¤ä \(aq\-\(aq ¤È¤¤¤Ã¤¿ GNU ¤Ë¤è¤ë³ÈÄ¥µ¡Ç½¤ò»ÈÍѤ·¤¿¤ê¡¢
367 °ú¤­¿ô¥Ù¥¯¥È¥ë¤ÎÀÚ¤êÂؤ¨»þ¤Ë
368 .B POSIXLY_CORRECT
369 ¤ÎÃͤòÊѹ¹¤·¤¿¤ê¤¹¤ë¾ì¹ç¤Ë¤Ï¡¢
370 .I optind
371 ¤òÅÁÅýŪ¤Ê 1 ¤Ç¤Ï¤Ê¤¯ 0 ¤Ë¥ê¥»¥Ã¥È¤¹¤ë¤³¤È¤Ç
372 .BR getopt ()
373 ¤òºÆ½é´ü²½¤·¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤
374 (0 ¤Ë¥ê¥»¥Ã¥È¤¹¤ë¤³¤È¤Ç¡¢
375 .B POSIXLY_CORRECT
376 ¤ä
377 .I optstring
378 ¤Î GNU ³ÈÄ¥µ¡Ç½¤Î¥Á¥§¥Ã¥¯¤ò¹Ô¤¦ÆâÉô½é´ü²½¥ë¡¼¥Á¥ó¤¬µ¯Æ°¤µ¤ì¤ë)¡£
379 .SH ¥Ð¥°
380 POSIX.2 ¤Ë¤ª¤±¤ë
381 .BR getopt ()
382 ¤Î»ÅÍͤˤϵ»½ÑŪ¤ÊÌäÂ꤬¤¢¤ê¡¢
383 ¤½¤ÎÆâÍƤϠPOSIX.2 Interpretation 150 ¤Ëµ­¤µ¤ì¤Æ¤¤¤ë¡£
384 GNU ¤Ë¤è¤ë¼ÂÁõ¤Ç¤Ï (¤ª¤½¤é¤¯Â¾¤Î¤¹¤Ù¤Æ¤Î¼ÂÁõ¤Ç¤â)¡¢
385 »ÅÍͤȰۤʤëÀµ¤·¤¤Æ°ºî¤ò¤¹¤ë¤è¤¦¤Ë¼ÂÁõ¤µ¤ì¤Æ¤¤¤ë¡£
386 .SH Îã
387 °Ê²¼¤Ë¼¨¤¹´Êñ¤Ê¥µ¥ó¥×¥ë¥×¥í¥°¥é¥à¤Ç¤Ï¡¢
388 Æó¼ïÎà¤Î¥×¥í¥°¥é¥à¥ª¥×¥·¥ç¥ó¤ò°·¤¦¤Î¤Ë
389 .BR getopt ()
390 ¤ò»ÈÍѤ·¤Æ¤¤¤ë¡£°ì¤Ä¤ÏÃͤòȼ¤ï¤Ê¤¤
391 .I \-n
392 ¤Ç¡¢¤â¤¦°ì¤Ä¤ÏÂбþ¤¹¤ëÃͤ¬É¬ÍפÊ
393 .I "\-t val"
394 ¤Ç¤¢¤ë¡£
395 .nf
396 .sp
397 #include <unistd.h>
398 #include <stdlib.h>
399 #include <stdio.h>
400
401 int
402 main(int argc, char *argv[])
403 {
404     int flags, opt;
405     int nsecs, tfnd;
406
407     nsecs = 0;
408     tfnd = 0;
409     flags = 0;
410     while ((opt = getopt(argc, argv, "nt:")) != \-1) {
411         switch (opt) {
412         case \(aqn\(aq:
413             flags = 1;
414             break;
415         case \(aqt\(aq:
416             nsecs = atoi(optarg);
417             tfnd = 1;
418             break;
419         default: /* \(aq?\(aq */
420             fprintf(stderr, "Usage: %s [\-t nsecs] [\-n] name\\n",
421                     argv[0]);
422             exit(EXIT_FAILURE);
423         }
424     }
425
426     printf("flags=%d; tfnd=%d; optind=%d\\n", flags, tfnd, optind);
427
428     if (optind >= argc) {
429         fprintf(stderr, "Expected argument after options\\n");
430         exit(EXIT_FAILURE);
431     }
432
433     printf("name argument = %s\\n", argv[optind]);
434
435     /* Other code omitted */
436
437     exit(EXIT_SUCCESS);
438 }
439 .fi
440 .PP
441 °Ê²¼¤Ï¡¢
442 .BR getopt_long ()
443 ¤Î»ÈÍÑË¡¤ò¡¢¤Û¤Ü¤¹¤Ù¤Æ¤Îµ¡Ç½¤Ë¤Ä¤¤¤Æ¼¨¤·¤¿¥×¥í¥°¥é¥à¤ÎÎã¤Ç¤¢¤ë¡£
444 .nf
445 .sp
446 #include <stdio.h>     /* for printf */
447 #include <stdlib.h>    /* for exit */
448 #include <getopt.h>
449
450 int
451 main(int argc, char **argv) {
452     int c;
453     int digit_optind = 0;
454
455     while (1) {
456         int this_option_optind = optind ? optind : 1;
457         int option_index = 0;
458         static struct option long_options[] = {
459             {"add",     required_argument, 0,  0 },
460             {"append",  no_argument,       0,  0 },
461             {"delete",  required_argument, 0,  0 },
462             {"verbose", no_argument,       0,  0 },
463             {"create",  required_argument, 0, \(aqc\(aq},
464             {"file",    required_argument, 0,  0 },
465             {0,         0,                 0,  0 }
466         };
467
468         c = getopt_long(argc, argv, "abc:d:012",
469                  long_options, &option_index);
470         if (c == \-1)
471             break;
472
473         switch (c) {
474         case 0:
475             printf("option %s", long_options[option_index].name);
476             if (optarg)
477                 printf(" with arg %s", optarg);
478             printf("\\n");
479             break;
480
481         case \(aq0\(aq:
482         case \(aq1\(aq:
483         case \(aq2\(aq:
484             if (digit_optind != 0 && digit_optind != this_option_optind)
485               printf("digits occur in two different argv\-elements.\\n");
486             digit_optind = this_option_optind;
487             printf("option %c\\n", c);
488             break;
489
490         case \(aqa\(aq:
491             printf("option a\\n");
492             break;
493
494         case \(aqb\(aq:
495             printf("option b\\n");
496             break;
497
498         case \(aqc\(aq:
499             printf("option c with value \(aq%s\(aq\\n", optarg);
500             break;
501
502         case \(aqd\(aq:
503             printf("option d with value \(aq%s\(aq\\n", optarg);
504             break;
505
506         case \(aq?\(aq:
507             break;
508
509         default:
510             printf("?? getopt returned character code 0%o ??\\n", c);
511         }
512     }
513
514     if (optind < argc) {
515         printf("non-option ARGV\-elements: ");
516         while (optind < argc)
517             printf("%s ", argv[optind++]);
518         printf("\\n");
519     }
520
521     exit(EXIT_SUCCESS);
522 }
523 .fi
524 .SH ´ØÏ¢¹àÌÜ
525 .BR getsubopt (3)