메뉴 건너뛰기


Developer > Application

C,C++ VC++ 에서 사용할 수 있는 getopt 함수

2013.12.28 03:51

푸우 조회 수:19386



VC++ 에 사용할 수 있는 getopt 를 구현할까하다 검색해 보니 좋은 소스가 발견되어서 올려 놓습니다.
001: #ifdef WIN32
002: 
003: #include <stdio.h>
004: #include <string.h>
005: #include <assert.h>
006: #include <stdlib.h>
007: 
008: #define OPTERRCOLON (1)
009: #define OPTERRNF (2)
010: #define OPTERRARG (3)
011: 
012: char *optarg;
013: int optreset = 0;
014: int optind = 1;
015: int opterr = 1;
016: int optopt;
017: 
018: static int optiserr(int argc, char * const *argv, int oint, const char *optstr, int optchr, int err)
019: {
020:   if(opterr)
021:   {
022:     fprintf(stderr, "Error in argument %d, char %d: ", oint, optchr+1);
023:     switch(err)
024:     {
025:     case OPTERRCOLON:
026:       fprintf(stderr, ": in flags\n");
027:       break;
028:     case OPTERRNF:
029:       fprintf(stderr, "option not found %c\n", argv[oint][optchr]);
030:       break;
031:     case OPTERRARG:
032:       fprintf(stderr, "no argument for option %c\n", argv[oint][optchr]);
033:       break;
034:     default:
035:       fprintf(stderr, "unknown\n");
036:       break;
037:     }
038:   }
039:   optopt = argv[oint][optchr];
040:   return('?');
041: }
042: 
043: int getopt( int argc, char* const *argv, const char *optstr )
044: {
045:   static int optchr = 0;
046:   static int dash = 0; 
047: 
048:   const char *cp;
049: 
050:   if (optreset)
051:     optreset = optchr = dash = 0;
052:   if(optind >= argc)
053:     return(EOF);
054:   if(!dash && (argv[optind][0] !=  '-'))
055:     return(EOF);
056:   if(!dash && (argv[optind][0] ==  '-') && !argv[optind][1])
057:   {
058:     /*
059:      * use to specify stdin. Need to let pgm process this and
060:      * the following args
061:      */
062:     return(EOF);
063:   }
064:   if( (argv[optind][0] == '-') && (argv[optind][1] == '-') )
065:   {
066:     
067:     optind++;
068:     return(EOF);
069:   }
070: 
071:   if( !dash )
072:   {
073:     assert((argv[optind][0] == '-') && argv[optind][1]);
074:     dash = 1;
075:     optchr = 1;
076:   }
077: 
078:   // Check if the guy tries to do a -: kind of flag
079:   assert(dash);
080:   if( argv[optind][optchr] == ':' )
081:   {
082:     dash = 0;
083:     optind++;
084:     return( optiserr(argc, argv, optind-1, optstr, optchr, OPTERRCOLON) );
085:   }
086: 
087:   if( !(cp = strchr( optstr, argv[optind][optchr] ) ) )
088:   {
089:     int errind = optind;
090:     int errchr = optchr;
091: 
092:     if( !argv[optind][optchr+1] )
093:     {
094:       dash = 0;
095:       optind++;
096:     }
097:     else
098:       optchr++;
099:     return( optiserr( argc, argv, errind, optstr, errchr, OPTERRNF ) );
100:   }
101: 
102:   if( cp[1] == ':' )
103:   {
104:     dash = 0;
105:     optind++;
106:     if( optind == argc )
107:       return(optiserr(argc, argv, optind-1, optstr, optchr, OPTERRARG));
108:     optarg = argv[optind++];
109:     return(*cp);
110:   }
111:   else
112:   {
113:     if( !argv[optind][optchr+1] )
114:     {
115:       dash = 0;
116:       optind++;
117:     }
118:     else
119:       optchr++;
120:     return(*cp);
121:   }
122: 
123:   assert(0);
124:   return(0);
125: }
126: 
127: #endif
[출처] http://www.midgard-project.org/api-docs/midgard/core/cvs/getopt_8c-source.html