1 //===-- MICmdArgValOptionLong.cpp -------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 #include "MICmdArgValOptionLong.h"
12 #include "MICmdArgContext.h"
14 //++ ------------------------------------------------------------------------------------
15 // Details: CMICmdArgValOptionLong constructor.
21 CMICmdArgValOptionLong::CMICmdArgValOptionLong(void)
22 : m_nExpectingNOptions(0)
23 , m_eExpectingOptionType(eArgValType_invalid)
27 //++ ------------------------------------------------------------------------------------
28 // Details: CMICmdArgValOptionLong constructor.
30 // Args: vrArgName - (R) Argument's name to search by.
31 // vbMandatory - (R) True = Yes must be present, false = optional argument.
32 // vbHandleByCmd - (R) True = Command processes *this option, false = not handled.
36 CMICmdArgValOptionLong::CMICmdArgValOptionLong(const CMIUtilString &vrArgName, const bool vbMandatory, const bool vbHandleByCmd)
37 : CMICmdArgValListBase(vrArgName, vbMandatory, vbHandleByCmd)
38 , m_nExpectingNOptions(0)
39 , m_eExpectingOptionType(eArgValType_invalid)
43 //++ ------------------------------------------------------------------------------------
44 // Details: CMICmdArgValOptionLong constructor.
46 // Args: vrArgName - (R) Argument's name to search by.
47 // vbMandatory - (R) True = Yes must be present, false = optional argument.
48 // vbHandleByCmd - (R) True = Command processes *this option, false = not handled.
49 // veType - (R) The type of argument to look for and create argument object of a certain type.
50 // vnExpectingNOptions - (R) The number of options expected to read following *this argument.
54 CMICmdArgValOptionLong::CMICmdArgValOptionLong(const CMIUtilString &vrArgName, const bool vbMandatory, const bool vbHandleByCmd,
55 const ArgValType_e veType, const MIuint vnExpectingNOptions)
56 : CMICmdArgValListBase(vrArgName, vbMandatory, vbHandleByCmd)
57 , m_nExpectingNOptions(vnExpectingNOptions)
58 , m_eExpectingOptionType(veType)
62 //++ ------------------------------------------------------------------------------------
63 // Details: CMICmdArgValOptionLong destructor.
69 CMICmdArgValOptionLong::~CMICmdArgValOptionLong(void)
75 //++ ------------------------------------------------------------------------------------
76 // Details: Tear down resources used by *this object.
83 CMICmdArgValOptionLong::Destroy(void)
86 VecArgObjPtr_t::const_iterator it = m_vecArgsExpected.begin();
87 while (it != m_vecArgsExpected.end())
89 CMICmdArgValBase *pOptionObj = *it;
95 m_vecArgsExpected.clear();
98 //++ ------------------------------------------------------------------------------------
99 // Details: Parse the command's argument options string and try to extract the long
100 // argument *this argument type is looking for.
102 // Args: vwArgContext - (RW) The command's argument options string.
103 // Return: MIstatus::success - Functional succeeded.
104 // MIstatus::failure - Functional failed.
108 CMICmdArgValOptionLong::Validate(CMICmdArgContext &vwArgContext)
110 if (vwArgContext.IsEmpty())
111 return m_bMandatory ? MIstatus::failure : MIstatus::success;
113 if (vwArgContext.GetNumberArgsPresent() == 1)
115 const CMIUtilString &rArg(vwArgContext.GetArgsLeftToParse());
116 if (IsArgLongOption(rArg) && ArgNameMatch(rArg))
120 if (!vwArgContext.RemoveArg(rArg))
121 return MIstatus::failure;
123 if (m_nExpectingNOptions == 0)
126 return MIstatus::success;
129 m_bIsMissingOptions = true;
130 return MIstatus::failure;
133 return MIstatus::failure;
136 // More than one option...
137 MIuint nArgIndex = 0;
138 const CMIUtilString::VecString_t vecOptions(vwArgContext.GetArgs());
139 CMIUtilString::VecString_t::const_iterator it = vecOptions.begin();
140 while (it != vecOptions.end())
142 const CMIUtilString &rArg(*it);
143 if (IsArgOptionCorrect(rArg) && ArgNameMatch(rArg))
147 if (!vwArgContext.RemoveArg(rArg))
148 return MIstatus::failure;
150 if (m_nExpectingNOptions != 0)
152 if (ExtractExpectedOptions(vwArgContext, nArgIndex))
155 return MIstatus::success;
158 m_bIsMissingOptions = true;
159 return MIstatus::failure;
164 return MIstatus::success;
173 return MIstatus::failure;
176 //++ ------------------------------------------------------------------------------------
177 // Details: Parse the text following *this argument and extract the options the values of
178 // CMICmdArgValListBase::m_eArgType forming argument objects for each of those
179 // options extracted.
181 // Args: vrwTxt - (RW) The command's argument options string.
182 // nArgIndex - (R) The Nth arg position in argument context from the left.
183 // Return: MIstatus::success - Functional succeeded.
184 // MIstatus::failure - Functional failed.
188 CMICmdArgValOptionLong::ExtractExpectedOptions(CMICmdArgContext &vrwTxt, const MIuint nArgIndex)
190 CMIUtilString::VecString_t vecOptions;
191 MIuint nOptionsPresent = 0;
192 if ((m_eExpectingOptionType != eArgValType_StringQuoted) && (m_eExpectingOptionType != eArgValType_StringQuotedNumber) &&
193 (m_eExpectingOptionType != eArgValType_StringQuotedNumberPath))
194 nOptionsPresent = vrwTxt.GetArgsLeftToParse().Split(" ", vecOptions);
196 nOptionsPresent = vrwTxt.GetArgsLeftToParse().SplitConsiderQuotes(" ", vecOptions);
197 if (nOptionsPresent == 0)
198 return MIstatus::failure;
200 MIuint nArgIndexCnt = 0;
202 MIuint nTypeCnt2 = 0;
203 MIuint nFoundNOptionsCnt = 0;
204 CMIUtilString::VecString_t::const_iterator it = vecOptions.begin();
205 while (it != vecOptions.end())
207 // Move to the Nth argument position from left before do validation/checking
208 if (nArgIndexCnt++ == nArgIndex)
211 const CMIUtilString &rOption(*it);
212 if (IsExpectedCorrectType(rOption, m_eExpectingOptionType))
215 CMICmdArgValBase *pOptionObj = CreationObj(rOption, m_eExpectingOptionType);
216 if ((pOptionObj != nullptr) && vrwTxt.RemoveArgAtPos(rOption, nArgIndex))
219 m_vecArgsExpected.push_back(pOptionObj);
223 // Is the sequence 'options' of same type broken. Expecting the same type until the
225 if (nTypeCnt != nTypeCnt2)
226 return MIstatus::failure;
228 if (nFoundNOptionsCnt == m_nExpectingNOptions)
229 return MIstatus::success;
235 if (nFoundNOptionsCnt != m_nExpectingNOptions)
236 return MIstatus::failure;
238 return MIstatus::success;
241 //++ ------------------------------------------------------------------------------------
242 // Details: Examine the string and determine if it is a valid long type option argument.
243 // Long type argument looks like --someLongOption.
245 // Args: vrTxt - (R) Some text.
246 // Return: bool - True = yes valid arg, false = no.
250 CMICmdArgValOptionLong::IsArgLongOption(const CMIUtilString &vrTxt) const
252 const bool bHavePosSlash = (vrTxt.find_first_of("/") != std::string::npos);
253 const bool bHaveBckSlash = (vrTxt.find_first_of("\\") != std::string::npos);
254 if (bHavePosSlash || bHaveBckSlash)
257 const size_t nPos = vrTxt.find_first_of("--");
261 if (vrTxt.length() < 3)
264 const CMIUtilString strArg = vrTxt.substr(2).c_str();
265 if (strArg.IsNumber())
271 //++ ------------------------------------------------------------------------------------
272 // Details: Examine the string and determine if it is a valid long type option argument.
273 // Long type argument looks like --someLongOption.
274 // Type: Overideable.
275 // Args: vrTxt - (R) Some text.
276 // Return: bool - True = yes valid arg, false = no.
280 CMICmdArgValOptionLong::IsArgOptionCorrect(const CMIUtilString &vrTxt) const
282 return IsArgLongOption(vrTxt);
285 //++ ------------------------------------------------------------------------------------
286 // Details: Does the argument name of the argument being parsed ATM match the name of
287 // *this argument object.
289 // Args: vrTxt - (R) Some text.
290 // Return: bool - True = yes arg name matched, false = no.
294 CMICmdArgValOptionLong::ArgNameMatch(const CMIUtilString &vrTxt) const
296 const CMIUtilString strArg = vrTxt.substr(2).c_str();
297 return (strArg == GetName());
300 //++ ------------------------------------------------------------------------------------
301 // Details: Retrieve the list of CMICmdArgValBase derived option objects found following
302 // *this long option argument. For example "list-thread-groups [ --recurse 1 ]"
303 // where 1 is the list of expected option to follow.
306 // Return: CMICmdArgValListBase::VecArgObjPtr_t & - List of options.
309 const CMICmdArgValListBase::VecArgObjPtr_t &
310 CMICmdArgValOptionLong::GetExpectedOptions(void) const
312 return m_vecArgsExpected;