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