MCPcopy Create free account
hub / github.com/Tripwire/tripwire-open-source / Parse

Method Parse

src/core/cmdlineparser.cpp:92–248  ·  view source on GitHub ↗

////////////////////////////////////////////////////////////////////////// Parse //////////////////////////////////////////////////////////////////////////

Source from the content-addressed store, hash-verified

90// Parse
91///////////////////////////////////////////////////////////////////////////////
92void cCmdLineParser::Parse(int argc, const TCHAR* const* argv)
93{
94 // clear out any existing data
95 mArgData.clear();
96
97 const TCHAR* pCurArg = 0;
98 bool bProcessedFinalParams = false; // gets set to true when the parameters to the command line are processed
99
100 // I assume argv[0] is the executable name...
101 for (int i = 1; i < argc; i++)
102 {
103 if (argv[i][0] == _T('-'))
104 {
105 pCurArg = argv[i];
106
107 // this is a switch; find it in the table...
108 cArgInfo argInfo;
109 if (!mArgTable.Lookup(TSTRING(&argv[i][1]), argInfo))
110 {
111 // unknown switch!
112 throw eCmdLineInvalidArg(TSS_GetString(cCore, core::STR_ERR2_BAD_ARG_PARAMS) + pCurArg);
113 }
114 //
115 // make sure this hasn't been specified yet...
116 //
117 if (ArgInList(argInfo.mId))
118 {
119 // Make sure it isn't okay for this one to appear more than once...
120 std::set<int>::iterator it = mMultipleAllowed.find(argInfo.mId);
121 if (it == mMultipleAllowed.end())
122 {
123 // It wasn't in our list of allowed params, so error.
124 throw eCmdLineMultiArg(TSS_GetString(cCore, core::STR_ERR2_BAD_ARG_PARAMS) + argv[i]);
125 }
126 }
127 //
128 // add it to the list..
129 //
130 mArgData.push_back(cArgData(argInfo.mId, TSTRING(argv[i])));
131 cArgData& curArg = mArgData.back();
132 switch (argInfo.mNumParams)
133 {
134 case PARAM_NONE:
135 // make sure there are no parameters to this, but be careful because
136 // it is legal to start the parameters to the executable here.
137 if ((i + 1 < argc) && (argv[i + 1][0] != _T('-')))
138 {
139 // search for any more parameters
140 // TODO: In the future we may want to support a '--' switch that specifies the start
141 // of parameters to the executable.
142 for (int j = i + 2; j < argc; ++j)
143 {
144 if (argv[j][0] == _T('-'))
145 {
146 // >0 parameter passed !
147 throw eCmdLineBadParam(TSS_GetString(cCore, core::STR_ERR2_BAD_ARG_PARAMS) + pCurArg);
148 }
149 }

Callers 5

_tmainFunction · 0.80
_tmainFunction · 0.80
_tmainFunction · 0.80
_tmainFunction · 0.80
test_parseFunction · 0.80

Calls 4

cArgDataClass · 0.85
endMethod · 0.80
LookupMethod · 0.45
findMethod · 0.45

Tested by 1

test_parseFunction · 0.64