1 |
// XGetopt.cpp Version 1.2
|
2 |
//
|
3 |
// Author: Hans Dietrich
|
4 |
// hdietrich2@hotmail.com
|
5 |
//
|
6 |
// Description:
|
7 |
// XGetopt.cpp implements getopt(), a function to parse command lines.
|
8 |
//
|
9 |
// History
|
10 |
// Version 1.2 - 2003 May 17
|
11 |
// - Added Unicode support
|
12 |
//
|
13 |
// Version 1.1 - 2002 March 10
|
14 |
// - Added example to XGetopt.cpp module header
|
15 |
//
|
16 |
// This software is released into the public domain.
|
17 |
// You are free to use it in any way you like.
|
18 |
//
|
19 |
// This software is provided "as is" with no expressed
|
20 |
// or implied warranty. I accept no liability for any
|
21 |
// damage or loss of business that this software may cause.
|
22 |
//
|
23 |
///////////////////////////////////////////////////////////////////////////////
|
24 |
|
25 |
|
26 |
|
27 |
///////////////////////////////////////////////////////////////////////////////
|
28 |
// if you are not using precompiled headers then include these lines:
|
29 |
#include <windows.h>
|
30 |
#include <stdio.h>
|
31 |
///////////////////////////////////////////////////////////////////////////////
|
32 |
|
33 |
|
34 |
#include "XGetopt.h"
|
35 |
|
36 |
|
37 |
///////////////////////////////////////////////////////////////////////////////
|
38 |
//
|
39 |
// X G e t o p t . c p p
|
40 |
//
|
41 |
//
|
42 |
// NAME
|
43 |
// getopt -- parse command line options
|
44 |
//
|
45 |
// SYNOPSIS
|
46 |
// int getopt(int argc, char *argv[], char *optstring)
|
47 |
//
|
48 |
// extern char *optarg;
|
49 |
// extern int optind;
|
50 |
//
|
51 |
// DESCRIPTION
|
52 |
// The getopt() function parses the command line arguments. Its
|
53 |
// arguments argc and argv are the argument count and array as
|
54 |
// passed into the application on program invocation. In the case
|
55 |
// of Visual C++ programs, argc and argv are available via the
|
56 |
// variables __argc and __argv (double underscores), respectively.
|
57 |
// getopt returns the next option letter in argv that matches a
|
58 |
// letter in optstring. (Note: Unicode programs should use
|
59 |
// __targv instead of __argv. Also, all character and string
|
60 |
// literals should be enclosed in _T( ) ).
|
61 |
//
|
62 |
// optstring is a string of recognized option letters; if a letter
|
63 |
// is followed by a colon, the option is expected to have an argument
|
64 |
// that may or may not be separated from it by white space. optarg
|
65 |
// is set to point to the start of the option argument on return from
|
66 |
// getopt.
|
67 |
//
|
68 |
// Option letters may be combined, e.g., "-ab" is equivalent to
|
69 |
// "-a -b". Option letters are case sensitive.
|
70 |
//
|
71 |
// getopt places in the external variable optind the argv index
|
72 |
// of the next argument to be processed. optind is initialized
|
73 |
// to 0 before the first call to getopt.
|
74 |
//
|
75 |
// When all options have been processed (i.e., up to the first
|
76 |
// non-option argument), getopt returns EOF, optarg will point
|
77 |
// to the argument, and optind will be set to the argv index of
|
78 |
// the argument. If there are no non-option arguments, optarg
|
79 |
// will be set to NULL.
|
80 |
//
|
81 |
// The special option "--" may be used to delimit the end of the
|
82 |
// options; EOF will be returned, and "--" (and everything after it)
|
83 |
// will be skipped.
|
84 |
//
|
85 |
// RETURN VALUE
|
86 |
// For option letters contained in the string optstring, getopt
|
87 |
// will return the option letter. getopt returns a question mark (?)
|
88 |
// when it encounters an option letter not included in optstring.
|
89 |
// EOF is returned when processing is finished.
|
90 |
//
|
91 |
// BUGS
|
92 |
// 1) Long options are not supported.
|
93 |
// 2) The GNU double-colon extension is not supported.
|
94 |
// 3) The environment variable POSIXLY_CORRECT is not supported.
|
95 |
// 4) The + syntax is not supported.
|
96 |
// 5) The automatic permutation of arguments is not supported.
|
97 |
// 6) This implementation of getopt() returns EOF if an error is
|
98 |
// encountered, instead of -1 as the latest standard requires.
|
99 |
//
|
100 |
// EXAMPLE
|
101 |
// BOOL CMyApp::ProcessCommandLine(int argc, char *argv[])
|
102 |
// {
|
103 |
// int c;
|
104 |
//
|
105 |
// while ((c = getopt(argc, argv, "aBn:")) != EOF)
|
106 |
// {
|
107 |
// switch (c)
|
108 |
// {
|
109 |
// case 'a':
|
110 |
// TRACE("option a\n");
|
111 |
// //
|
112 |
// // set some flag here
|
113 |
// //
|
114 |
// break;
|
115 |
//
|
116 |
// case 'B':
|
117 |
// TRACE( "option B\n");
|
118 |
// //
|
119 |
// // set some other flag here
|
120 |
// //
|
121 |
// break;
|
122 |
//
|
123 |
// case 'n':
|
124 |
// TRACE("option n: value=%d\n", atoi(optarg));
|
125 |
// //
|
126 |
// // do something with value here
|
127 |
// //
|
128 |
// break;
|
129 |
//
|
130 |
// case '?':
|
131 |
// TRACE("ERROR: illegal option %s\n", argv[optind-1]);
|
132 |
// return FALSE;
|
133 |
// break;
|
134 |
//
|
135 |
// default:
|
136 |
// TRACE("WARNING: no handler for option %c\n", c);
|
137 |
// return FALSE;
|
138 |
// break;
|
139 |
// }
|
140 |
// }
|
141 |
// //
|
142 |
// // check for non-option args here
|
143 |
// //
|
144 |
// return TRUE;
|
145 |
// }
|
146 |
//
|
147 |
///////////////////////////////////////////////////////////////////////////////
|
148 |
|
149 |
char *optarg; // global argument pointer
|
150 |
int optind = 0; // global argv index
|
151 |
|
152 |
int getopt(int argc, char *argv[], char *optstring)
|
153 |
{
|
154 |
static char *next = NULL;
|
155 |
if (optind == 0)
|
156 |
next = NULL;
|
157 |
|
158 |
optarg = NULL;
|
159 |
|
160 |
if (next == NULL || *next == '\0')
|
161 |
{
|
162 |
if (optind == 0)
|
163 |
optind++;
|
164 |
|
165 |
if (optind >= argc || argv[optind][0] != '-' || argv[optind][1] == '\0')
|
166 |
{
|
167 |
optarg = NULL;
|
168 |
if (optind < argc)
|
169 |
optarg = argv[optind];
|
170 |
return EOF;
|
171 |
}
|
172 |
|
173 |
if (strcmp(argv[optind], "--") == 0)
|
174 |
{
|
175 |
optind++;
|
176 |
optarg = NULL;
|
177 |
if (optind < argc)
|
178 |
optarg = argv[optind];
|
179 |
return EOF;
|
180 |
}
|
181 |
|
182 |
next = argv[optind];
|
183 |
next++; // skip past -
|
184 |
optind++;
|
185 |
}
|
186 |
|
187 |
char c = *next++;
|
188 |
char *cp = strchr(optstring, c);
|
189 |
|
190 |
if (cp == NULL || c == ':')
|
191 |
return '?';
|
192 |
|
193 |
cp++;
|
194 |
if (*cp == ':')
|
195 |
{
|
196 |
if (*next != '\0')
|
197 |
{
|
198 |
optarg = next;
|
199 |
next = NULL;
|
200 |
}
|
201 |
else if (optind < argc)
|
202 |
{
|
203 |
optarg = argv[optind];
|
204 |
optind++;
|
205 |
}
|
206 |
else
|
207 |
{
|
208 |
return '?';
|
209 |
}
|
210 |
}
|
211 |
|
212 |
return c;
|
213 |
}
|