1 // XGetopt.cpp Version 1.2
\r
3 // Author: Hans Dietrich
\r
4 // hdietrich2@hotmail.com
\r
7 // XGetopt.cpp implements getopt(), a function to parse command lines.
\r
10 // Version 1.2 - 2003 May 17
\r
11 // - Added Unicode support
\r
13 // Version 1.1 - 2002 March 10
\r
14 // - Added example to XGetopt.cpp module header
\r
16 // This software is released into the public domain.
\r
17 // You are free to use it in any way you like.
\r
19 // This software is provided "as is" with no expressed
\r
20 // or implied warranty. I accept no liability for any
\r
21 // damage or loss of business that this software may cause.
\r
23 ///////////////////////////////////////////////////////////////////////////////
\r
27 ///////////////////////////////////////////////////////////////////////////////
\r
28 // if you are not using precompiled headers then include these lines:
\r
29 #include <windows.h>
\r
31 ///////////////////////////////////////////////////////////////////////////////
\r
34 #include "XGetopt.h"
\r
37 ///////////////////////////////////////////////////////////////////////////////
\r
39 // X G e t o p t . c p p
\r
43 // getopt -- parse command line options
\r
46 // int getopt(int argc, char *argv[], char *optstring)
\r
48 // extern char *optarg;
\r
49 // extern int optind;
\r
52 // The getopt() function parses the command line arguments. Its
\r
53 // arguments argc and argv are the argument count and array as
\r
54 // passed into the application on program invocation. In the case
\r
55 // of Visual C++ programs, argc and argv are available via the
\r
56 // variables __argc and __argv (double underscores), respectively.
\r
57 // getopt returns the next option letter in argv that matches a
\r
58 // letter in optstring. (Note: Unicode programs should use
\r
59 // __targv instead of __argv. Also, all character and string
\r
60 // literals should be enclosed in _T( ) ).
\r
62 // optstring is a string of recognized option letters; if a letter
\r
63 // is followed by a colon, the option is expected to have an argument
\r
64 // that may or may not be separated from it by white space. optarg
\r
65 // is set to point to the start of the option argument on return from
\r
68 // Option letters may be combined, e.g., "-ab" is equivalent to
\r
69 // "-a -b". Option letters are case sensitive.
\r
71 // getopt places in the external variable optind the argv index
\r
72 // of the next argument to be processed. optind is initialized
\r
73 // to 0 before the first call to getopt.
\r
75 // When all options have been processed (i.e., up to the first
\r
76 // non-option argument), getopt returns EOF, optarg will point
\r
77 // to the argument, and optind will be set to the argv index of
\r
78 // the argument. If there are no non-option arguments, optarg
\r
79 // will be set to NULL.
\r
81 // The special option "--" may be used to delimit the end of the
\r
82 // options; EOF will be returned, and "--" (and everything after it)
\r
86 // For option letters contained in the string optstring, getopt
\r
87 // will return the option letter. getopt returns a question mark (?)
\r
88 // when it encounters an option letter not included in optstring.
\r
89 // EOF is returned when processing is finished.
\r
92 // 1) Long options are not supported.
\r
93 // 2) The GNU double-colon extension is not supported.
\r
94 // 3) The environment variable POSIXLY_CORRECT is not supported.
\r
95 // 4) The + syntax is not supported.
\r
96 // 5) The automatic permutation of arguments is not supported.
\r
97 // 6) This implementation of getopt() returns EOF if an error is
\r
98 // encountered, instead of -1 as the latest standard requires.
\r
101 // BOOL CMyApp::ProcessCommandLine(int argc, char *argv[])
\r
105 // while ((c = getopt(argc, argv, "aBn:")) != EOF)
\r
110 // TRACE("option a\n");
\r
112 // // set some flag here
\r
117 // TRACE( "option B\n");
\r
119 // // set some other flag here
\r
124 // TRACE("option n: value=%d\n", atoi(optarg));
\r
126 // // do something with value here
\r
131 // TRACE("ERROR: illegal option %s\n", argv[optind-1]);
\r
136 // TRACE("WARNING: no handler for option %c\n", c);
\r
142 // // check for non-option args here
\r
147 ///////////////////////////////////////////////////////////////////////////////
\r
149 char *optarg; // global argument pointer
\r
150 int optind = 0; // global argv index
\r
152 int getopt(int argc, char *argv[], char *optstring)
\r
154 static char *next = NULL;
\r
163 if (next == NULL || *next == '\0')
\r
168 if (optind >= argc || argv[optind][0] != '-' || argv[optind][1] == '\0')
\r
172 optarg = argv[optind];
\r
176 if (strcmp(argv[optind], "--") == 0)
\r
181 optarg = argv[optind];
\r
185 next = argv[optind];
\r
186 next++; // skip past -
\r
191 cp = strchr(optstring, c);
\r
193 if (cp == NULL || c == ':')
\r
204 else if (optind < argc)
\r
206 optarg = argv[optind];
\r