[4] | 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 | 3. This notice may not be removed or altered from any source
|
---|
| 27 | distribution.
|
---|
| 28 |
|
---|
| 29 | -------------------------
|
---|
| 30 |
|
---|
| 31 | Example :
|
---|
| 32 |
|
---|
| 33 | Our example application uses a command line that has two
|
---|
| 34 | required switches and two optional switches. The app should abort
|
---|
| 35 | if the required switches are not present and continue with default
|
---|
| 36 | values if the optional switches are not present.
|
---|
| 37 |
|
---|
| 38 | Sample command line :
|
---|
| 39 | MyApp.exe -p1 text1 text2 -p2 "this is a big argument" -opt1 -55 -opt2
|
---|
| 40 |
|
---|
| 41 | Switches -p1 and -p2 are required.
|
---|
| 42 | p1 has two arguments and p2 has one.
|
---|
| 43 |
|
---|
| 44 | Switches -opt1 and -opt2 are optional.
|
---|
| 45 | opt1 requires a numeric argument.
|
---|
| 46 | opt2 has no arguments.
|
---|
| 47 |
|
---|
| 48 | Also, assume that the app displays a 'help' screen if the '-h' switch
|
---|
| 49 | is present on the command line.
|
---|
| 50 |
|
---|
| 51 | #include "CmdLine.h"
|
---|
| 52 |
|
---|
| 53 | void main(int argc, char **argv)
|
---|
| 54 | {
|
---|
| 55 | // our cmd line parser object
|
---|
| 56 | CCmdLine cmdLine;
|
---|
| 57 |
|
---|
| 58 | // parse argc,argv
|
---|
| 59 | if (cmdLine.SplitLine(argc, argv) < 1)
|
---|
| 60 | {
|
---|
| 61 | // no switches were given on the command line, abort
|
---|
| 62 | ASSERT(0);
|
---|
| 63 | exit(-1);
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | // test for the 'help' case
|
---|
| 67 | if (cmdLine.HasSwitch("-h"))
|
---|
| 68 | {
|
---|
| 69 | show_help();
|
---|
| 70 | exit(0);
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | // get the required arguments
|
---|
| 74 | StringType p1_1, p1_2, p2_1;
|
---|
| 75 | try
|
---|
| 76 | {
|
---|
| 77 | // if any of these fail, we'll end up in the catch() block
|
---|
| 78 | p1_1 = cmdLine.GetArgument("-p1", 0);
|
---|
| 79 | p1_2 = cmdLine.GetArgument("-p1", 1);
|
---|
| 80 | p2_1 = cmdLine.GetArgument("-p2", 0);
|
---|
| 81 |
|
---|
| 82 | }
|
---|
| 83 | catch (...)
|
---|
| 84 | {
|
---|
| 85 | // one of the required arguments was missing, abort
|
---|
| 86 | ASSERT(0);
|
---|
| 87 | exit(-1);
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | // get the optional parameters
|
---|
| 91 |
|
---|
| 92 | // convert to an int, default to '100'
|
---|
| 93 | int iOpt1Val = atoi(cmdLine.GetSafeArgument("-opt1", 0, 100));
|
---|
| 94 |
|
---|
| 95 | // since opt2 has no arguments, just test for the presence of
|
---|
| 96 | // the '-opt2' switch
|
---|
| 97 | bool bOptVal2 = cmdLine.HasSwitch("-opt2");
|
---|
| 98 |
|
---|
| 99 | .... and so on....
|
---|
| 100 |
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | If this class is used in an MFC application, StringType is CString, else
|
---|
| 104 | it uses the STL 'string' type.
|
---|
| 105 |
|
---|
| 106 | If this is an MFC app, you can use the __argc and __argv macros from
|
---|
| 107 | you CYourWinApp::InitInstance() function in place of the standard argc
|
---|
| 108 | and argv variables.
|
---|
| 109 |
|
---|
| 110 | ------------------------------------------------------*/
|
---|
| 111 | #ifndef SACMDSH
|
---|
| 112 | #define SACMDSH
|
---|
| 113 |
|
---|
| 114 | #define StringType string
|
---|
| 115 |
|
---|
| 116 | // tell the compiler to shut up
|
---|
| 117 | #pragma warning(disable:4786)
|
---|
| 118 |
|
---|
| 119 | //#include <iostream> // you may need this
|
---|
| 120 | #include <map>
|
---|
| 121 | #include <string>
|
---|
| 122 | #include <vector>
|
---|
| 123 | using namespace std ;
|
---|
| 124 |
|
---|
| 125 | // handy little container for our argument vector
|
---|
| 126 | struct CCmdParam
|
---|
| 127 | {
|
---|
| 128 | vector<StringType> m_strings;
|
---|
| 129 | };
|
---|
| 130 |
|
---|
| 131 | // this class is actually a map of strings to vectors
|
---|
| 132 | typedef map<StringType, CCmdParam> _CCmdLine;
|
---|
| 133 |
|
---|
| 134 | // the command line parser class
|
---|
| 135 | class CCmdLine : public _CCmdLine
|
---|
| 136 | {
|
---|
| 137 |
|
---|
| 138 | public:
|
---|
| 139 | /*------------------------------------------------------
|
---|
| 140 | int CCmdLine::SplitLine(int argc, char **argv)
|
---|
| 141 |
|
---|
| 142 | parse the command line into switches and arguments.
|
---|
| 143 |
|
---|
| 144 | returns number of switches found
|
---|
| 145 | ------------------------------------------------------*/
|
---|
| 146 | int SplitLine(int argc, char **argv);
|
---|
| 147 |
|
---|
| 148 | /*------------------------------------------------------
|
---|
| 149 | bool CCmdLine::HasSwitch(const char *pSwitch)
|
---|
| 150 |
|
---|
| 151 | was the switch found on the command line ?
|
---|
| 152 |
|
---|
| 153 | ex. if the command line is : app.exe -a p1 p2 p3 -b p4 -c -d p5
|
---|
| 154 |
|
---|
| 155 | call return
|
---|
| 156 | ---- ------
|
---|
| 157 | cmdLine.HasSwitch("-a") true
|
---|
| 158 | cmdLine.HasSwitch("-z") false
|
---|
| 159 | ------------------------------------------------------*/
|
---|
| 160 | bool HasSwitch(const char *pSwitch);
|
---|
| 161 |
|
---|
| 162 | /*------------------------------------------------------
|
---|
| 163 |
|
---|
| 164 | StringType CCmdLine::GetSafeArgument(const char *pSwitch, int iIdx, const char *pDefault)
|
---|
| 165 |
|
---|
| 166 | fetch an argument associated with a switch . if the parameter at
|
---|
| 167 | index iIdx is not found, this will return the default that you
|
---|
| 168 | provide.
|
---|
| 169 |
|
---|
| 170 | example :
|
---|
| 171 |
|
---|
| 172 | command line is : app.exe -a p1 p2 p3 -b p4 -c -d p5
|
---|
| 173 |
|
---|
| 174 | call return
|
---|
| 175 | ---- ------
|
---|
| 176 | cmdLine.GetSafeArgument("-a", 0, "zz") p1
|
---|
| 177 | cmdLine.GetSafeArgument("-a", 1, "zz") p2
|
---|
| 178 | cmdLine.GetSafeArgument("-b", 0, "zz") p4
|
---|
| 179 | cmdLine.GetSafeArgument("-b", 1, "zz") zz
|
---|
| 180 |
|
---|
| 181 | ------------------------------------------------------*/
|
---|
| 182 |
|
---|
| 183 | StringType GetSafeArgument(const char *pSwitch, int iIdx, const char *pDefault);
|
---|
| 184 |
|
---|
| 185 | /*------------------------------------------------------
|
---|
| 186 |
|
---|
| 187 | StringType CCmdLine::GetArgument(const char *pSwitch, int iIdx)
|
---|
| 188 |
|
---|
| 189 | fetch a argument associated with a switch. throws an exception
|
---|
| 190 | of (int)0, if the parameter at index iIdx is not found.
|
---|
| 191 |
|
---|
| 192 | example :
|
---|
| 193 |
|
---|
| 194 | command line is : app.exe -a p1 p2 p3 -b p4 -c -d p5
|
---|
| 195 |
|
---|
| 196 | call return
|
---|
| 197 | ---- ------
|
---|
| 198 | cmdLine.GetArgument("-a", 0) p1
|
---|
| 199 | cmdLine.GetArgument("-b", 1) throws (int)0, returns an empty string
|
---|
| 200 |
|
---|
| 201 | ------------------------------------------------------*/
|
---|
| 202 | StringType GetArgument(const char *pSwitch, int iIdx);
|
---|
| 203 |
|
---|
| 204 | /*------------------------------------------------------
|
---|
| 205 | int CCmdLine::GetArgumentCount(const char *pSwitch)
|
---|
| 206 |
|
---|
| 207 | returns the number of arguments found for a given switch.
|
---|
| 208 |
|
---|
| 209 | returns -1 if the switch was not found
|
---|
| 210 |
|
---|
| 211 | ------------------------------------------------------*/
|
---|
| 212 | int GetArgumentCount(const char *pSwitch);
|
---|
| 213 |
|
---|
| 214 | protected:
|
---|
| 215 | /*------------------------------------------------------
|
---|
| 216 |
|
---|
| 217 | protected member function
|
---|
| 218 | test a parameter to see if it's a switch :
|
---|
| 219 |
|
---|
| 220 | switches are of the form : -x
|
---|
| 221 | where 'x' is one or more characters.
|
---|
| 222 | the first character of a switch must be non-numeric!
|
---|
| 223 |
|
---|
| 224 | ------------------------------------------------------*/
|
---|
| 225 | bool IsSwitch(const char *pParam);
|
---|
| 226 | };
|
---|
| 227 |
|
---|
| 228 | #endif
|
---|