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"
15 //------------------------------------------------------------------------------------
16 // Details: CMICmdArgValOptionLong constructor.
22 CMICmdArgValOptionLong::CMICmdArgValOptionLong()
23 : m_nExpectingNOptions(0), m_eExpectingOptionType(eArgValType_invalid) {}
26 //------------------------------------------------------------------------------------
27 // Details: CMICmdArgValOptionLong constructor.
29 // Args: vrArgName - (R) Argument's name to search by.
30 // vbMandatory - (R) True = Yes must be present, false = optional
32 // vbHandleByCmd - (R) True = Command processes *this option, false =
37 CMICmdArgValOptionLong::CMICmdArgValOptionLong(const CMIUtilString &vrArgName,
38 const bool vbMandatory,
39 const bool vbHandleByCmd)
40 : CMICmdArgValListBase(vrArgName, vbMandatory, vbHandleByCmd),
41 m_nExpectingNOptions(0), m_eExpectingOptionType(eArgValType_invalid) {}
44 //------------------------------------------------------------------------------------
45 // Details: CMICmdArgValOptionLong constructor.
47 // Args: vrArgName - (R) Argument's name to search by.
48 // vbMandatory - (R) True = Yes must be present, false =
50 // vbHandleByCmd - (R) True = Command processes *this option,
51 // false = not handled.
52 // veType - (R) The type of argument to look for and
53 // create argument object of a certain type.
54 // vnExpectingNOptions - (R) The number of options expected to read
55 // following *this argument.
59 CMICmdArgValOptionLong::CMICmdArgValOptionLong(const CMIUtilString &vrArgName,
60 const bool vbMandatory,
61 const bool vbHandleByCmd,
62 const ArgValType_e veType,
63 const MIuint vnExpectingNOptions)
64 : CMICmdArgValListBase(vrArgName, vbMandatory, vbHandleByCmd),
65 m_nExpectingNOptions(vnExpectingNOptions),
66 m_eExpectingOptionType(veType) {}
69 //------------------------------------------------------------------------------------
70 // Details: CMICmdArgValOptionLong destructor.
76 CMICmdArgValOptionLong::~CMICmdArgValOptionLong() {
82 //------------------------------------------------------------------------------------
83 // Details: Tear down resources used by *this object.
89 void CMICmdArgValOptionLong::Destroy() {
91 VecArgObjPtr_t::const_iterator it = m_vecArgsExpected.begin();
92 while (it != m_vecArgsExpected.end()) {
93 CMICmdArgValBase *pOptionObj = *it;
99 m_vecArgsExpected.clear();
103 //------------------------------------------------------------------------------------
104 // Details: Parse the command's argument options string and try to extract the
106 // argument *this argument type is looking for.
108 // Args: vwArgContext - (RW) The command's argument options string.
109 // Return: MIstatus::success - Functional succeeded.
110 // MIstatus::failure - Functional failed.
113 bool CMICmdArgValOptionLong::Validate(CMICmdArgContext &vwArgContext) {
114 if (vwArgContext.IsEmpty())
115 return m_bMandatory ? MIstatus::failure : MIstatus::success;
117 if (vwArgContext.GetNumberArgsPresent() == 1) {
118 const CMIUtilString &rArg(vwArgContext.GetArgsLeftToParse());
119 if (IsArgLongOption(rArg) && ArgNameMatch(rArg)) {
122 if (!vwArgContext.RemoveArg(rArg))
123 return MIstatus::failure;
125 if (m_nExpectingNOptions == 0) {
127 return MIstatus::success;
130 m_bIsMissingOptions = true;
131 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()) {
141 const CMIUtilString &rArg(*it);
142 if (IsArgOptionCorrect(rArg) && ArgNameMatch(rArg)) {
145 if (!vwArgContext.RemoveArg(rArg))
146 return MIstatus::failure;
148 if (m_nExpectingNOptions != 0) {
149 if (ExtractExpectedOptions(vwArgContext, nArgIndex)) {
151 return MIstatus::success;
154 m_bIsMissingOptions = true;
155 return MIstatus::failure;
158 return MIstatus::success;
167 return MIstatus::failure;
171 //------------------------------------------------------------------------------------
172 // Details: Parse the text following *this argument and extract the options the
174 // CMICmdArgValListBase::m_eArgType forming argument objects for each
176 // options extracted.
178 // Args: vrwTxt - (RW) The command's argument options string.
179 // nArgIndex - (R) The Nth arg position in argument context from
181 // Return: MIstatus::success - Functional succeeded.
182 // MIstatus::failure - Functional failed.
185 bool CMICmdArgValOptionLong::ExtractExpectedOptions(CMICmdArgContext &vrwTxt,
186 const MIuint nArgIndex) {
187 CMIUtilString::VecString_t vecOptions = vrwTxt.GetArgs();
188 if (vecOptions.size() == 0)
189 return MIstatus::failure;
191 MIuint nArgIndexCnt = 0;
193 MIuint nTypeCnt2 = 0;
194 MIuint nFoundNOptionsCnt = 0;
195 CMIUtilString::VecString_t::const_iterator it = vecOptions.begin();
196 while (it != vecOptions.end()) {
197 // Move to the Nth argument position from left before do validation/checking
198 if (nArgIndexCnt++ == nArgIndex) {
200 const CMIUtilString &rOption(*it);
201 if (IsExpectedCorrectType(rOption, m_eExpectingOptionType)) {
203 CMICmdArgValBase *pOptionObj =
204 CreationObj(rOption, m_eExpectingOptionType);
205 if ((pOptionObj != nullptr) &&
206 vrwTxt.RemoveArgAtPos(rOption, nArgIndex)) {
208 m_vecArgsExpected.push_back(pOptionObj);
212 // Is the sequence 'options' of same type broken. Expecting the same type
215 if (nTypeCnt != nTypeCnt2)
216 return MIstatus::failure;
218 if (nFoundNOptionsCnt == m_nExpectingNOptions)
219 return MIstatus::success;
225 if (nFoundNOptionsCnt != m_nExpectingNOptions)
226 return MIstatus::failure;
228 return MIstatus::success;
232 //------------------------------------------------------------------------------------
233 // Details: Examine the string and determine if it is a valid long type option
235 // Long type argument looks like --someLongOption.
237 // Args: vrTxt - (R) Some text.
238 // Return: bool - True = yes valid arg, false = no.
241 bool CMICmdArgValOptionLong::IsArgLongOption(const CMIUtilString &vrTxt) const {
242 const bool bHavePosSlash = (vrTxt.find('/') != std::string::npos);
243 const bool bHaveBckSlash = (vrTxt.find('\\') != std::string::npos);
244 if (bHavePosSlash || bHaveBckSlash)
247 const size_t nPos = vrTxt.find("--");
251 if (vrTxt.length() < 3)
254 const CMIUtilString strArg = vrTxt.substr(2);
255 return !strArg.IsNumber();
259 //------------------------------------------------------------------------------------
260 // Details: Examine the string and determine if it is a valid long type option
262 // Long type argument looks like --someLongOption.
263 // Type: Overideable.
264 // Args: vrTxt - (R) Some text.
265 // Return: bool - True = yes valid arg, false = no.
268 bool CMICmdArgValOptionLong::IsArgOptionCorrect(
269 const CMIUtilString &vrTxt) const {
270 return IsArgLongOption(vrTxt);
274 //------------------------------------------------------------------------------------
275 // Details: Does the argument name of the argument being parsed ATM match the
277 // *this argument object.
279 // Args: vrTxt - (R) Some text.
280 // Return: bool - True = yes arg name matched, false = no.
283 bool CMICmdArgValOptionLong::ArgNameMatch(const CMIUtilString &vrTxt) const {
284 const CMIUtilString strArg = vrTxt.substr(2);
285 return (strArg == GetName());
289 //------------------------------------------------------------------------------------
290 // Details: Retrieve the list of CMICmdArgValBase derived option objects found
292 // *this long option argument. For example "list-thread-groups [
294 // where 1 is the list of expected option to follow.
297 // Return: CMICmdArgValListBase::VecArgObjPtr_t & - List of options.
300 const CMICmdArgValListBase::VecArgObjPtr_t &
301 CMICmdArgValOptionLong::GetExpectedOptions() const {
302 return m_vecArgsExpected;