1 | /*------------------------------------------------------
|
---|
2 | CCmdLine
|
---|
3 |
|
---|
4 | A utility for parsing command lines.
|
---|
5 |
|
---|
6 | Copyright (C) 1999 Chris Losinger, Smaller Animals Software.
|
---|
7 | http://www.smalleranimals.com
|
---|
8 |
|
---|
9 | This software is provided 'as-is', without any express
|
---|
10 | or implied warranty. In no event will the authors be
|
---|
11 | held liable for any damages arising from the use of this software.
|
---|
12 |
|
---|
13 | Permission is granted to anyone to use this software
|
---|
14 | for any purpose, including commercial applications, and
|
---|
15 | to alter it and redistribute it freely, subject to the
|
---|
16 | following restrictions:
|
---|
17 |
|
---|
18 | 1. The origin of this software must not be misrepresented;
|
---|
19 | you must not claim that you wrote the original software.
|
---|
20 | If you use this software in a product, an acknowledgment
|
---|
21 | in the product documentation would be appreciated but is not required.
|
---|
22 |
|
---|
23 | 2. Altered source versions must be plainly marked as such,
|
---|
24 | and must not be misrepresented as being the original software.
|
---|
25 |
|
---|
26 |
|
---|
27 | 3. This notice may not be removed or altered from any source
|
---|
28 | distribution.
|
---|
29 |
|
---|
30 | See SACmds.h for more info.
|
---|
31 | ------------------------------------------------------*/
|
---|
32 |
|
---|
33 | // if you're using MFC, you'll need to un-comment this line
|
---|
34 | // #include "stdafx.h"
|
---|
35 |
|
---|
36 | #include "CCmdLine.h"
|
---|
37 | #ifdef WIN32
|
---|
38 | #include "crtdbg.h"
|
---|
39 | #endif
|
---|
40 |
|
---|
41 | #if defined(linux) || defined(__APPLE__)
|
---|
42 | #include <ctype.h>
|
---|
43 | #endif
|
---|
44 |
|
---|
45 | /*------------------------------------------------------
|
---|
46 | int CCmdLine::SplitLine(int argc, char **argv)
|
---|
47 |
|
---|
48 | parse the command line into switches and arguments
|
---|
49 |
|
---|
50 | returns number of switches found
|
---|
51 | ------------------------------------------------------*/
|
---|
52 | int CCmdLine::SplitLine(int argc, char **argv)
|
---|
53 | {
|
---|
54 | clear();
|
---|
55 |
|
---|
56 | StringType curParam; // current argv[x]
|
---|
57 |
|
---|
58 | // skip the exe name (start with i = 1)
|
---|
59 | for (int i = 1; i < argc; i++)
|
---|
60 | {
|
---|
61 | // if it's a switch, start a new CCmdLine
|
---|
62 | if (IsSwitch(argv[i]))
|
---|
63 | {
|
---|
64 | curParam = argv[i];
|
---|
65 |
|
---|
66 | StringType arg;
|
---|
67 |
|
---|
68 | // look at next input string to see if it's a switch or an argument
|
---|
69 | if (i + 1 < argc)
|
---|
70 |
|
---|
71 | {
|
---|
72 | if (!IsSwitch(argv[i + 1]))
|
---|
73 | {
|
---|
74 | // it's an argument, not a switch
|
---|
75 | arg = argv[i + 1];
|
---|
76 |
|
---|
77 | // skip to next
|
---|
78 | i++;
|
---|
79 | }
|
---|
80 | else
|
---|
81 | {
|
---|
82 | arg = "";
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | // add it
|
---|
87 | CCmdParam cmd;
|
---|
88 |
|
---|
89 | // only add non-empty args
|
---|
90 | if (arg != "")
|
---|
91 | {
|
---|
92 | cmd.m_strings.push_back(arg);
|
---|
93 | }
|
---|
94 |
|
---|
95 | // add the CCmdParam to 'this'
|
---|
96 | pair<CCmdLine::iterator, bool> res = insert(CCmdLine::value_type(curParam, cmd));
|
---|
97 |
|
---|
98 | }
|
---|
99 | else
|
---|
100 | {
|
---|
101 | // it's not a new switch, so it must be more stuff for the last switch
|
---|
102 |
|
---|
103 | // ...let's add it
|
---|
104 | CCmdLine::iterator theIterator;
|
---|
105 |
|
---|
106 | // get an iterator for the current param
|
---|
107 | theIterator = find(curParam);
|
---|
108 | if (theIterator!=end())
|
---|
109 | {
|
---|
110 | (*theIterator).second.m_strings.push_back(argv[i]);
|
---|
111 | }
|
---|
112 | else
|
---|
113 | {
|
---|
114 | // ??
|
---|
115 | }
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | return size();
|
---|
120 | }
|
---|
121 |
|
---|
122 | /*------------------------------------------------------
|
---|
123 |
|
---|
124 | protected member function
|
---|
125 | test a parameter to see if it's a switch :
|
---|
126 |
|
---|
127 | switches are of the form : -x
|
---|
128 | where 'x' is one or more characters.
|
---|
129 | the first character of a switch must be non-numeric!
|
---|
130 |
|
---|
131 | ------------------------------------------------------*/
|
---|
132 |
|
---|
133 | bool CCmdLine::IsSwitch(const char *pParam)
|
---|
134 | {
|
---|
135 | if (pParam==NULL)
|
---|
136 | return false;
|
---|
137 |
|
---|
138 | // switches must non-empty
|
---|
139 | // must have at least one character after the '-'
|
---|
140 | int len = strlen(pParam);
|
---|
141 | if (len <= 1)
|
---|
142 | {
|
---|
143 | return false;
|
---|
144 | }
|
---|
145 |
|
---|
146 | // switches always start with '-'
|
---|
147 | if (pParam[0]=='-')
|
---|
148 | {
|
---|
149 | // allow negative numbers as arguments.
|
---|
150 | // ie., don't count them as switches
|
---|
151 | return (!isdigit(pParam[1]));
|
---|
152 | }
|
---|
153 | else
|
---|
154 | {
|
---|
155 | return false;
|
---|
156 | }
|
---|
157 | }
|
---|
158 |
|
---|
159 | /*------------------------------------------------------
|
---|
160 | bool CCmdLine::HasSwitch(const char *pSwitch)
|
---|
161 |
|
---|
162 | was the switch found on the command line ?
|
---|
163 |
|
---|
164 | ex. if the command line is : app.exe -a p1 p2 p3 -b p4 -c -d p5
|
---|
165 |
|
---|
166 | call return
|
---|
167 | ---- ------
|
---|
168 | cmdLine.HasSwitch("-a") true
|
---|
169 | cmdLine.HasSwitch("-z") false
|
---|
170 | ------------------------------------------------------*/
|
---|
171 |
|
---|
172 | bool CCmdLine::HasSwitch(const char *pSwitch)
|
---|
173 | {
|
---|
174 | CCmdLine::iterator theIterator;
|
---|
175 | theIterator = find(pSwitch);
|
---|
176 | return (theIterator!=end());
|
---|
177 | }
|
---|
178 |
|
---|
179 | /*------------------------------------------------------
|
---|
180 |
|
---|
181 | StringType CCmdLine::GetSafeArgument(const char *pSwitch, int iIdx, const char *pDefault)
|
---|
182 |
|
---|
183 | fetch an argument associated with a switch . if the parameter at
|
---|
184 | index iIdx is not found, this will return the default that you
|
---|
185 | provide.
|
---|
186 |
|
---|
187 | example :
|
---|
188 |
|
---|
189 | command line is : app.exe -a p1 p2 p3 -b p4 -c -d p5
|
---|
190 |
|
---|
191 | call return
|
---|
192 | ---- ------
|
---|
193 | cmdLine.GetSafeArgument("-a", 0, "zz") p1
|
---|
194 | cmdLine.GetSafeArgument("-a", 1, "zz") p2
|
---|
195 | cmdLine.GetSafeArgument("-b", 0, "zz") p4
|
---|
196 | cmdLine.GetSafeArgument("-b", 1, "zz") zz
|
---|
197 |
|
---|
198 | ------------------------------------------------------*/
|
---|
199 |
|
---|
200 | StringType CCmdLine::GetSafeArgument(const char *pSwitch, int iIdx, const char *pDefault)
|
---|
201 | {
|
---|
202 | StringType sRet;
|
---|
203 |
|
---|
204 | if (pDefault!=NULL)
|
---|
205 | sRet = pDefault;
|
---|
206 |
|
---|
207 | try
|
---|
208 | {
|
---|
209 | sRet = GetArgument(pSwitch, iIdx);
|
---|
210 | }
|
---|
211 | catch (...)
|
---|
212 | {
|
---|
213 | }
|
---|
214 |
|
---|
215 | return sRet;
|
---|
216 | }
|
---|
217 |
|
---|
218 | /*------------------------------------------------------
|
---|
219 |
|
---|
220 | StringType CCmdLine::GetArgument(const char *pSwitch, int iIdx)
|
---|
221 |
|
---|
222 | fetch a argument associated with a switch. throws an exception
|
---|
223 | of (int)0, if the parameter at index iIdx is not found.
|
---|
224 |
|
---|
225 | example :
|
---|
226 |
|
---|
227 | command line is : app.exe -a p1 p2 p3 -b p4 -c -d p5
|
---|
228 |
|
---|
229 | call return
|
---|
230 | ---- ------
|
---|
231 | cmdLine.GetArgument("-a", 0) p1
|
---|
232 | cmdLine.GetArgument("-b", 1) throws (int)0, returns an empty string
|
---|
233 |
|
---|
234 | ------------------------------------------------------*/
|
---|
235 |
|
---|
236 | StringType CCmdLine::GetArgument(const char *pSwitch, int iIdx)
|
---|
237 | {
|
---|
238 | if (HasSwitch(pSwitch))
|
---|
239 | {
|
---|
240 | CCmdLine::iterator theIterator;
|
---|
241 |
|
---|
242 | theIterator = find(pSwitch);
|
---|
243 | if (theIterator!=end())
|
---|
244 | {
|
---|
245 | if ((*theIterator).second.m_strings.size() > iIdx)
|
---|
246 | {
|
---|
247 | return (*theIterator).second.m_strings[iIdx];
|
---|
248 | }
|
---|
249 | }
|
---|
250 | }
|
---|
251 |
|
---|
252 | throw (int)0;
|
---|
253 |
|
---|
254 | return "";
|
---|
255 | }
|
---|
256 |
|
---|
257 | /*------------------------------------------------------
|
---|
258 | int CCmdLine::GetArgumentCount(const char *pSwitch)
|
---|
259 |
|
---|
260 | returns the number of arguments found for a given switch.
|
---|
261 |
|
---|
262 | returns -1 if the switch was not found
|
---|
263 |
|
---|
264 | ------------------------------------------------------*/
|
---|
265 |
|
---|
266 | int CCmdLine::GetArgumentCount(const char *pSwitch)
|
---|
267 | {
|
---|
268 | int iArgumentCount = -1;
|
---|
269 |
|
---|
270 | if (HasSwitch(pSwitch))
|
---|
271 | {
|
---|
272 | CCmdLine::iterator theIterator;
|
---|
273 |
|
---|
274 | theIterator = find(pSwitch);
|
---|
275 | if (theIterator!=end())
|
---|
276 | {
|
---|
277 | iArgumentCount = (*theIterator).second.m_strings.size();
|
---|
278 | }
|
---|
279 | }
|
---|
280 |
|
---|
281 | return iArgumentCount;
|
---|
282 | }
|
---|