æ¸å¼
#include <unistd.h>
int getopt(int argc, char * const argv[],
const char *optstring);
extern char *optarg;
extern int optind, opterr, optopt;
#include <getopt.h>
int getopt_long(int argc, char * const argv[],
const char *optstring,
const struct option *longopts, int *longindex);
int getopt_long_only(int argc, char * const argv[],
const char *optstring,
const struct option *longopts, int *longindex);
glibc åãã®æ©è½æ¤æ»ãã¯ãã®è¦ä»¶ (feature_test_macros(7) åç §):
getopt(): _POSIX_C_SOURCEÂ >=Â 2 || _XOPEN_SOURCE
getopt_long(), getopt_long_only(): _GNU_SOURCE
説æ
getopt() é¢æ°ã¯ã³ãã³ãã©ã¤ã³å¼ãæ°ã解éããã getopt() ãã¨ãå¼ãæ° argc 㨠argv ã¯ãããããããã°ã©ã ã®èµ·åæã« main() é¢æ°ã«æ¸¡ãããå¼ãæ°ã®åæ°ã¨é åã§ããã argv ã®è¦ç´ ã®ãã¡ '-' ã§å§ã¾ããã® (ã㤠"-" åç¬ã "--" åç¬ã§ã¯ãªããã®) 㯠ãªãã·ã§ã³è¦ç´ (option element) ã¨ã¿ãªãããã ãã®è¦ç´ ããå é ã® '-' ãé¤ããæå㯠ãªãã·ã§ã³æå (option character) ã¨ãããã getopt() ã¯ãç¹°ãè¿ãå¼ã³åºããããã¨ã«ã次ã®ãªãã·ã§ã³æåãè¿ããå¤æ° optind ã¯ã argv ã®æ¬¡ã«å¦çãããè¦ç´ ã®ã¤ã³ããã¯ã¹ã§ããã ã·ã¹ãã ã«ãããã®å¤æ°ã®å¤ã¯ 1 ã«åæåãããã å¼ã³åºãå´ã§ãã®å¤ã 1 ã«ãªã»ãããããã¨ã§ãåã argv ã®ã¹ãã£ã³ãããç´ããããæ°ããå¼ãæ°ãã¯ãã«ãã¹ãã£ã³ãããã¨ãã§ããã
æ°ããªãªãã·ã§ã³æåãè¦ã¤ããã¨ã getopt() ã¯ãã®æåãè¿ãã å¤é¨å¤æ° optind ã¨ã¹ã¿ãã£ãã¯ãªå¤æ° nextchar ãæ´æ°ããã ãããã«ãã£ã¦ã getopt() ã¯æ¬¡åã®å¼ã³åºãã®éã«ã 以éã®ãªãã·ã§ã³æåã argv è¦ç´ ã®ã¹ãã£ã³ãç¶ç¶ã§ããã
ãªãã·ã§ã³æåããã以ä¸è¦ã¤ãããªããªãã¨ã getopt() 㯠-1 ãè¿ãããã㦠optind ã¯ãargv ã®è¦ç´ ã®ãã¡ã ãªãã·ã§ã³ã§ãªãæåã®è¦ç´ ã示ãããã«ãªãã
optstring ã¯åãä»ãããªãã·ã§ã³æåãããªãæååã§ããã æåã®ãã¨ã«ã³ãã³ (:) ãç½®ããã¦ããå ´åã¯ã ãªãã·ã§ã³ã«ã¯å¼ãæ°ãå¿ è¦ã§ãããã¨ã示ãã ãã®ã¨ã getopt() ã¯ãç¾å¨æ³¨ç®ãã¦ãã argv è¦ç´ ã§ããªãã·ã§ã³æåã«å¼ãç¶ãããã¹ãã¸ã®ãã¤ã³ã¿ãã ãããã¯æ¬¡ã® argv è¦ç´ ã®ããã¹ãã¸ã®ãã¤ã³ã¿ã optarg ã«ä»£å ¥ããã 2 åé£ç¶ãã¦ã³ãã³ãç½®ããã¦ããå ´åã¯ã ãã®ãªãã·ã§ã³ã¯å¼ãæ°ãã¨ã£ã¦ãã¨ããªãã¦ãããã ç¾å¨ã® argv è¦ç´ ã«ããã¹ããããã° (ã¤ã¾ãã"-oarg" ã®ããã«ããªãã·ã§ã³åèªèº«ã¨åãã¯ã¼ãå ã« ããã¹ããããå ´å)ãããã optarg ã«è¿ãããã ãªããã° optarg 㯠0 ã«è¨å®ãããã ãã㯠GNU ã«ããæ¡å¼µã§ããã optstring ã« W ã¨ããã«ç¶ãã»ãã³ãã³ãå ¥ã£ã¦ããã¨ã -W foo ã¯é·ããªãã·ã§ã³ --foo ã¨åãããã«æ±ããã (POSIX.2 㯠-W ãªãã·ã§ã³ãå®è£ ä¾åã®æ¡å¼µã¨ãã¦äºç´ãã¦ãã)ã ãã®åä½ã¯ GNU ã«ããæ¡å¼µã§ãããglibc 2 以åã®ã©ã¤ãã©ãªã§ã¯ å©ç¨ã§ããªãã
ããã©ã«ãã§ã¯ getopt() 㯠argv ãã¹ãã£ã³ããéã«é åºãå¤æ´ãã ãªãã·ã§ã³ä»¥å¤ã®è¦ç´ ãæå¾ã«ç§»åããã ä»ã«ã 2 ã¤ã®ã¢ã¼ããå®è£ ããã¦ããã optstring ã®å é æåã '+' ã§ããããç°å¢å¤æ° POSIXLY_CORRECT ãè¨å®ããã¦ããå ´åã«ã¯ããªãã·ã§ã³ã対象ã¨ããåä½ã¯ã éãªãã·ã§ã³ã®å¼ãæ°ãç¾ãã段éã§çµäºããã optstring ã®å é æåã '-' ã§ããå ´åã«ã¯ã ãªãã·ã§ã³ã§ãªã argv è¦ç´ ã¯ã æåã³ã¼ã 1 ã®ãªãã·ã§ã³ã§ãããã®ããã«æ±ããã (ãããç¨ããããã°ã©ã ã¯ã ãªãã·ã§ã³ã argv è¦ç´ ãä»»æã®é åºã§åãå ¥ãããã¤ãããã®é åºã æå³ãæã¤ããã«æ¸ããã¦ããå¿ è¦ããã)ã "--" ã¯ç¹æ®ãªå¼ãæ°ã§ãã¹ãã£ã³ã®ã¢ã¼ãã«ãããã ãªãã·ã§ã³ã®ã¹ãã£ã³ãå¼·å¶çã«çµäºãããã
èªèã§ããªããªãã·ã§ã³æåãããã¨ã getopt() ã¯ã¨ã©ã¼ã¡ãã»ã¼ã¸ãæ¨æºã¨ã©ã¼åºå stderr ã«è¡¨ç¤ºãã ãã®æåã optopt ã«ä¿åã㦠'?' ãè¿ãã å¼ã³åºããããã°ã©ã 㧠opterr ã 0 ã«ãã¦ããã°ã ã¨ã©ã¼ã¡ãã»ã¼ã¸ã®è¡¨ç¤ºãæå¶ã§ããã
getopt() 㯠argv ã®ä¸ã« optstring ã«ãªããªãã·ã§ã³æåãè¦ã¤ããå ´åã ã¾ãã¯ãªãã·ã§ã³å¼ãæ°ã足ããªããã¨ãåãã£ãå ´åã '?' ãè¿ãã¦å¤é¨å¤æ° optopt ããã®ãªãã·ã§ã³æåã«è¨å®ããã optstring ã® (ä¸ã§èª¬æãããªãã·ã§ã³ã§æå®ã§ãã '+' ã¾ã㯠'-' å¾ã«ç¶ã) æåã®æåã ã³ãã³ (':') ã®ã¨ãã getopt() ã¯ãªãã·ã§ã³å¼ãæ°ã足ããªãå ´åã« '?' ã§ã¯ãªã ':' ãè¿ãã ã¨ã©ã¼ãè¦ã¤ããå ´åã§ãã㤠optstring ã®æåã®æåãã³ãã³ã§ãªãã ãã¤å¤é¨å¤æ° opterr ã 0 ã§ãªãå ´å (ãããããã©ã«ã)ã getopt() ã¯ã¨ã©ã¼ã¡ãã»ã¼ã¸ã表示ããã
getopt_long() 㨠getopt_long_only()
getopt_long() é¢æ°ã¯ãé·ããªãã·ã§ã³ (2 ã¤ã®ããã·ã¥ "--" ã§å§ã¾ããªãã·ã§ã³) ã åãå ¥ãããã¨ãé¤ã㦠getopt() ã¨åãããã«åä½ãã (ããã°ã©ã ã«é·ããªãã·ã§ã³ã ãã渡ãããå ´åã optstring 㯠NULL ã§ã¯ãªã空æåå ("") ã¨ãªã)ã é·ããªãã·ã§ã³ã®ååã¯ãä»ã¨éãªããªãç¯å²ã«ããã¦ç縮ã§ããã ãããã¯å®ç¾©ããããªãã·ã§ã³ã«æ£ç¢ºã«ããããããã®ã§ã (å½ç¶) ãã¾ããªãã é·ããªãã·ã§ã³ã¯å¼ãæ°ãåããã¨ãã§ãã --arg=param ã¾ã㯠--arg param ã¨è¨ãå½¢å¼ã§æå®ãããlongopts 㯠struct option ã®è¦ç´ ãããªãé åã®ãå é è¦ç´ ã¸ã®ãã¤ã³ã¿ã§ããã struct option 㯠<getopt.h> ã§ä»¥ä¸ã®ããã«å®ç¾©ããã¦ããã
struct option { const char *name; int has_arg; int *flag; int val; };
ããããã®ãã£ã¼ã«ãã®æå³ã¯ä»¥ä¸ã®éãã
- name
- é·ããªãã·ã§ã³ã®ååã
- has_arg
- no_argument (ã¾ã㯠0) ãªãããªãã·ã§ã³ã¯å¼ãæ°ãã¨ããªãã required_argument (ã¾ã㯠1) ãªãããªãã·ã§ã³ã¯å¼ãæ°ãå¿ è¦ã¨ããã optional_argument (ã¾ã㯠2) ãªãããªãã·ã§ã³ã¯å¼ãæ°ãã¨ã£ã¦ã ã¨ããªãã¦ãè¯ãã
- flag
- é·ããªãã·ã§ã³ã«å¯¾ããçµæã®è¿ãæ¹ãæå®ãããflag ã NULL ãªã getopt_long() 㯠val ãè¿ã (ä¾ãã°å¼ã³åºãå ã®ããã°ã©ã ã¯ã val ã«ç価ãªãªãã·ã§ã³æåãä»£å ¥ãããã¨ãã§ãã)ã NULL 以å¤ã®å ´åã«ã¯ã getopt_long() 㯠0 ãè¿ãã ãã®ã¨ããªãã·ã§ã³ãè¦ã¤ãã㨠flag ããã¤ã³ãããå¤æ°ã« val ãä»£å ¥ããããè¦ã¤ãããªãã¨ãã®å¤æ°ã¯å¤æ´ãããªãã
- val
- è¿ãå¤ãã¾ã㯠flag ããã¤ã³ãããå¤æ°ã¸ãã¼ããããå¤ã
é åã®æå¾ã®è¦ç´ ã¯ãå ¨ã¦ 0 ã§åãããã¦ããªããã°ãªããªãã
longindex ã¯ãNULL ã§ãªããã°ã é·ããªãã·ã§ã³ã®ã¤ã³ããã¯ã¹ã longopts ããã®ç¸å¯¾ä½ç½®ã¨ãã¦ä¿æãã¦ããå¤æ°ã¸ã®ãã¤ã³ã¿ã¨ãªãã
getopt_long_only() 㯠getopt_long() ã¨åæ§ã®åä½ããããã '-' ã "--" ã¨åæ§ã«ã é·ããªãã·ã§ã³ã¨ãã¦æ±ãããã'-' ã§å§ã¾ã ("--" 以å¤ã®) ãªãã·ã§ã³ããé·ããã®ã«ã¯ãããããªããçããã®ã« ãããããå ´åã«ããã¦ã¯ãããã¯çããªãã·ã§ã³ã¨ãã¦è§£éãããã
è¿ãå¤
ãªãã·ã§ã³ãæ£å¸¸ã«è¦ã¤ããã° getopt() ã¯ãã®ãªãã·ã§ã³æåãè¿ãã ãã¹ã¦ã®ã³ãã³ãã©ã¤ã³ãªãã·ã§ã³ã®è§£æãçµãã£ããã getopt() 㯠-1 ãè¿ãã optstring ã«å«ã¾ããªããªãã·ã§ã³æåãè¦ã¤ããã¨ã'?' ãè¿ãã å¼ãæ°ã足ããªããªãã·ã§ã³ãè¦ã¤ãã£ãå ´åã è¿ãå¤ã¯ optstring ã®æåã®æåã«ããç°ãªã: æåã®æåã ':' ã§ããã° ':' ãè¿ãã ãã以å¤ã®å ´å㯠'?' ãè¿ããgetopt_long() 㨠getopt_long_only() ãã çããªãã·ã§ã³æåãèªèããå ´åã«ã¯ãã®æåãè¿ãã é·ããªãã·ã§ã³ã«å¯¾ãã¦ã¯ã flag ã NULL ãªã val ãè¿ãã flag ã NULL 以å¤ãªã 0 ãè¿ãã ã¨ã©ã¼ã¨ -1 ã®è¿ãå¤ã¯ getopt() ã¨åãã§ããã ããã« '?' ã¯ããããã確å®ã§ããªãå ´åãä½åãªãã©ã¡ã¼ã¿ã¼ãããå ´åã«ãè¿ãã
ç°å¢
- POSIXLY_CORRECT
- ãããè¨å®ããã¦ããã¨ãéãªãã·ã§ã³ã®å¼ãæ°ã«å°éããæç¹ã§ãªãã·ã§ã³ ã«å¯¾ããæä½ãåæ¢ãããã
- _<PID>_GNU_nonoption_argv_flags_
- ãã®å¤æ°ã¯ bash(1) 2.0 ã glibc ã¨éä¿¡ããããã«ç¨ããããã ã©ã®å¼ãæ°ãã¯ã¤ã«ãã«ã¼ããå±éããçµæã§ã ãããã£ã¦ãªãã·ã§ã³ã¨ã¿ãªãã¹ãã§ãªãããç¥ããããã®ã§ããã ãã®æ©è½ã¯ bash(1) ã®ãã¼ã¸ã§ã³ 2.01 ã§åé¤ãããããglibc ã«ã¯ã¾ã æ®ã£ã¦ããã
æºæ
- getopt():
-
ç°å¢å¤æ° POSIXLY_CORRECT ãè¨å®ããã¦ããå ´å㯠POSIX.2 㨠POSIX.1-2001 ã«æºæ ããã ä»ã®å ´åã¯
argv ã®è¦ç´ ã¯æ¬å½ã®æå³ã§ã®å®æ°ã«ã¯ãªããªãã ãªããªãé åºãå¤æ´ããã¦ãã¾ãããã§ããã
ãã ããããã¯ããããã¿ã¤ãã§ã¯å®æ°ã§ãããã®ããã«ãã¦ããã ããã¯ä»ã®ã·ã¹ãã ã¨ã®äºææ§ã®ããã§ããã
optstring 㧠'+' ã '-' ã使ãã®ã¯ GNU ã«ããæ¡å¼µã§ãã.
å¤ãå®è£ ã®ããã¤ãã§ã¯ã getopt() 㯠<stdio.h> ã§å®£è¨ããã¦ããã SUSv1 ã§ã¯ã <unistd.h> ã <stdio.h> ã®ã©ã¡ãã㧠宣è¨ãã¦ãããã£ãã POSIX.1-2001 ã§ã¯ã getopt ã®å®£è¨ã <stdio.h> ã§è¡ãã®ã¯ãéå»ã®åæ®ãã§ããã¨ãããã POSIX.1-2001 ã§ã¯ <stdio.h> ã§å®£è¨ãè¡ããã¨ãèªãã¦ããªãã
- getopt_long(), getopt_long_only():
- ãããã®é¢æ°ã¯ GNU ã«ããæ¡å¼µã§ããã
注æ
è¤æ°ã®å¼ãæ°ãã¯ãã«ãã¹ãã£ã³ããããåãå¼ãæ°ãã¯ãã«ãäºåä»¥ä¸ ã¹ãã£ã³ãããããªããã°ã©ã ã§ã optstring ã®å é 㧠'+' ã '-' ã¨ãã£ã GNU ã«ããæ¡å¼µæ©è½ã使ç¨ãããã å¼ãæ°ãã¯ãã«ã®åãæ¿ãæã« POSIXLY_CORRECT ã®å¤ãå¤æ´ãããããå ´åã«ã¯ã optind ãä¼çµ±ç㪠1 ã§ã¯ãªã 0 ã«ãªã»ãããããã¨ã§ getopt() ãååæåããªããã°ãªããªã (0 ã«ãªã»ãããããã¨ã§ã POSIXLY_CORRECT ã optstring ã® GNU æ¡å¼µæ©è½ã®ãã§ãã¯ãè¡ãå é¨åæåã«ã¼ãã³ãèµ·åããã)ããã°
POSIX.2 ã«ããã getopt() ã®ä»æ§ã«ã¯æè¡çãªåé¡ãããã ãã®å 容㯠POSIX.2 Interpretation 150 ã«è¨ããã¦ããã GNU ã«ããå®è£ ã§ã¯ (ããããä»ã®ãã¹ã¦ã®å®è£ ã§ã)ã ä»æ§ã¨ç°ãªãæ£ããåä½ãããããã«å®è£ ããã¦ãããä¾
以ä¸ã«ç¤ºãç°¡åãªãµã³ãã«ããã°ã©ã ã§ã¯ã äºç¨®é¡ã®ããã°ã©ã ãªãã·ã§ã³ãæ±ãã®ã« getopt() ã使ç¨ãã¦ãããä¸ã¤ã¯å¤ãä¼´ããªã -n ã§ãããä¸ã¤ã¯å¯¾å¿ããå¤ãå¿ è¦ãª -t val ã§ããã#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 'n': flags = 1; break; case 't': nsecs = atoi(optarg); tfnd = 1; break; default: /* '?' */ 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); }
以ä¸ã¯ã getopt_long() ã®ä½¿ç¨æ³ããã»ã¼ãã¹ã¦ã®æ©è½ã«ã¤ãã¦ç¤ºããããã°ã©ã ã®ä¾ã§ããã
#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", required_argument, 0, 0 }, {"append", no_argument, 0, 0 }, {"delete", required_argument, 0, 0 }, {"verbose", no_argument, 0, 0 }, {"create", required_argument, 0, 'c'}, {"file", required_argument, 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(EXIT_SUCCESS); }
ãã®ææ¸ã«ã¤ãã¦
ãã® man ãã¼ã¸ã¯ Linux man-pages ããã¸ã§ã¯ãã®ãªãªã¼ã¹ 3.65 ã®ä¸é¨ ã§ãããããã¸ã§ã¯ãã®èª¬æã¨ãã°å ±åã«é¢ããæ å ±ã¯ http://www.kernel.org/doc/man-pages/ ã«æ¸ããã¦ããã