1 //===-- MICmdArgSet.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 "MICmdArgSet.h"
12 #include "MICmdArgValBase.h"
14 #include "MICmnResources.h"
17 //------------------------------------------------------------------------------------
18 // Details: CMICmdArgSet constructor.
24 CMICmdArgSet::CMICmdArgSet()
25 : m_bIsArgsPresentButNotHandledByCmd(false), m_constStrCommaSpc(", ") {}
28 //------------------------------------------------------------------------------------
29 // Details: CMICmdArgSet destructor.
35 CMICmdArgSet::~CMICmdArgSet() {
41 //------------------------------------------------------------------------------------
42 // Details: Release resources used by *this container object.
48 void CMICmdArgSet::Destroy() {
49 // Delete command argument objects
50 if (!m_setCmdArgs.empty()) {
51 SetCmdArgs_t::iterator it = m_setCmdArgs.begin();
52 while (it != m_setCmdArgs.end()) {
53 CMICmdArgValBase *pArg(*it);
62 m_setCmdArgsThatNotValid.clear();
63 m_setCmdArgsThatAreMissing.clear();
64 m_setCmdArgsNotHandledByCmd.clear();
65 m_setCmdArgsMissingInfo.clear();
66 m_bIsArgsPresentButNotHandledByCmd = false;
70 //------------------------------------------------------------------------------------
71 // Details: Retrieve the state flag indicating that the command set up ready to
73 // command arguments or options found that one or more arguments was
75 // present but not handled. This is given as a warning in the MI log
79 // Return: bool - True = one or more args not handled, false = all args handled
82 bool CMICmdArgSet::IsArgsPresentButNotHandledByCmd() const {
83 return m_bIsArgsPresentButNotHandledByCmd;
87 //------------------------------------------------------------------------------------
88 // Details: Add the list of command's arguments to parse and validate another
91 // Args: vArg - (R) A command argument object.
95 void CMICmdArgSet::Add(CMICmdArgValBase *vArg) { m_setCmdArgs.push_back(vArg); }
98 //------------------------------------------------------------------------------------
99 // Details: After validating an options line of text (the context) and there is
101 // it is likely a mandatory command argument that is required is
103 // function returns the argument that should be present.
106 // Return: SetCmdArgs_t & - Set of argument objects.
109 const CMICmdArgSet::SetCmdArgs_t &CMICmdArgSet::GetArgsThatAreMissing() const {
110 return m_setCmdArgsThatAreMissing;
114 //------------------------------------------------------------------------------------
115 // Details: After validating an options line of text (the context) and there is
117 // it may be because one or more arguments were unable to extract a
119 // function returns the argument that were found to be invalid.
122 // Return: SetCmdArgs_t & - Set of argument objects.
125 const CMICmdArgSet::SetCmdArgs_t &CMICmdArgSet::GetArgsThatInvalid() const {
126 return m_setCmdArgsThatNotValid;
130 //------------------------------------------------------------------------------------
131 // Details: The list of argument or option (objects) that were specified by the
133 // and so recognised when parsed but were not handled. Ideally the
135 // should handle all arguments and options presented to it. The command
137 // warning to the MI log file to say that these options were not
139 // Used as one way to determine option that maybe should really be
141 // and not just ignored.
144 // Return: SetCmdArgs_t & - Set of argument objects.
147 const CMICmdArgSet::SetCmdArgs_t &CMICmdArgSet::GetArgsNotHandledByCmd() const {
148 return m_setCmdArgsNotHandledByCmd;
152 //------------------------------------------------------------------------------------
153 // Details: Given a set of command argument objects parse the context option
155 // find those argument and retrieve their value. If the function fails
157 // GetArgsThatAreMissing() to see which commands that were mandatory
159 // missing or failed to parse.
161 // Args: vStrMiCmd - (R) Command's name.
162 // vCmdArgsText - (RW) A command's options or argument.
163 // Return: MIstatus::success - Functional succeeded.
164 // MIstatus::failure - Functional failed.
167 bool CMICmdArgSet::Validate(const CMIUtilString &vStrMiCmd,
168 CMICmdArgContext &vwCmdArgsText) {
169 m_cmdArgContext = vwCmdArgsText;
171 // Iterate all the arguments or options required by a command
172 SetCmdArgs_t::const_iterator it = m_setCmdArgs.begin();
173 while (it != m_setCmdArgs.end()) {
174 CMICmdArgValBase *pArg = *it;
176 if (!pArg->Validate(vwCmdArgsText)) {
177 if (pArg->GetFound()) {
178 if (pArg->GetIsMissingOptions())
179 m_setCmdArgsMissingInfo.push_back(pArg);
180 else if (!pArg->GetValid())
181 m_setCmdArgsThatNotValid.push_back(pArg);
182 } else if (pArg->GetIsMandatory())
183 m_setCmdArgsThatAreMissing.push_back(pArg);
186 if (pArg->GetFound() && !pArg->GetIsHandledByCmd()) {
187 m_bIsArgsPresentButNotHandledByCmd = true;
188 m_setCmdArgsNotHandledByCmd.push_back(pArg);
195 // report any issues with arguments/options
196 if (IsArgsPresentButNotHandledByCmd())
197 WarningArgsNotHandledbyCmdLogFile(vStrMiCmd);
199 return ValidationFormErrorMessages(vwCmdArgsText);
203 //------------------------------------------------------------------------------------
204 // Details: Having validated the command's options text and failed for some
206 // the error message made up with the faults found.
208 // vCmdArgsText - (RW) A command's options or argument.
209 // Return: MIstatus::success - Functional succeeded.
210 // MIstatus::failure - Functional failed.
213 bool CMICmdArgSet::ValidationFormErrorMessages(
214 const CMICmdArgContext &vwCmdArgsText) {
215 CMIUtilString strListMissing;
216 CMIUtilString strListInvalid;
217 CMIUtilString strListMissingInfo;
218 const bool bArgsMissing = (m_setCmdArgsThatAreMissing.size() > 0);
219 const bool bArgsInvalid = (m_setCmdArgsThatNotValid.size() > 0);
220 const bool bArgsMissingInfo = (m_setCmdArgsMissingInfo.size() > 0);
221 if (!(bArgsMissing || bArgsInvalid || bArgsMissingInfo))
222 return MIstatus::success;
225 SetCmdArgs_t::const_iterator it = m_setCmdArgsThatAreMissing.begin();
226 while (it != m_setCmdArgsThatAreMissing.end()) {
228 strListMissing += m_constStrCommaSpc;
230 const CMICmdArgValBase *pArg(*it);
231 strListMissing += pArg->GetName();
239 SetCmdArgs_t::const_iterator it = m_setCmdArgsThatNotValid.begin();
240 while (it != m_setCmdArgsThatNotValid.end()) {
242 strListMissing += m_constStrCommaSpc;
244 const CMICmdArgValBase *pArg(*it);
245 strListInvalid += pArg->GetName();
251 if (bArgsMissingInfo) {
253 SetCmdArgs_t::const_iterator it = m_setCmdArgsMissingInfo.begin();
254 while (it != m_setCmdArgsMissingInfo.end()) {
256 strListMissingInfo += m_constStrCommaSpc;
258 const CMICmdArgValBase *pArg(*it);
259 strListMissingInfo += pArg->GetName();
266 bool bHaveOneError = false;
267 CMIUtilString strError = MIRSRC(IDS_CMD_ARGS_ERR_PREFIX_MSG);
268 if (bArgsMissing && bArgsInvalid) {
269 bHaveOneError = true;
271 CMIUtilString::Format(MIRSRC(IDS_CMD_ARGS_ERR_VALIDATION_MAN_INVALID),
272 strListMissing.c_str(), strListInvalid.c_str());
277 bHaveOneError = true;
278 strError += CMIUtilString::Format(
279 MIRSRC(IDS_CMD_ARGS_ERR_VALIDATION_MANDATORY), strListMissing.c_str());
281 if (bArgsMissingInfo) {
284 bHaveOneError = true;
286 CMIUtilString::Format(MIRSRC(IDS_CMD_ARGS_ERR_VALIDATION_MISSING_INF),
287 strListMissingInfo.c_str());
292 bHaveOneError = true;
293 strError += CMIUtilString::Format(
294 MIRSRC(IDS_CMD_ARGS_ERR_VALIDATION_INVALID), strListInvalid.c_str());
296 if (!vwCmdArgsText.IsEmpty()) {
299 bHaveOneError = true;
301 CMIUtilString::Format(MIRSRC(IDS_CMD_ARGS_ERR_CONTEXT_NOT_ALL_EATTEN),
302 vwCmdArgsText.GetArgsLeftToParse().c_str());
306 SetErrorDescription(strError);
307 return MIstatus::failure;
310 return MIstatus::success;
314 //------------------------------------------------------------------------------------
315 // Details: Ask if the command's argument options text had any arguments.
318 // Return: bool - True = Has one or more arguments present, false = no
322 bool CMICmdArgSet::IsArgContextEmpty() const {
323 return m_cmdArgContext.IsEmpty();
327 //------------------------------------------------------------------------------------
328 // Details: Retrieve the number of arguments that are being used for the
332 // Return: size_t - Argument count.
335 size_t CMICmdArgSet::GetCount() const { return m_setCmdArgs.size(); }
338 //------------------------------------------------------------------------------------
339 // Details: Given a set of command argument objects retrieve the argument with
343 // Args: vpArg - (W) A pointer to a command's argument object.
344 // Return: True - Argument found.
345 // False - Argument not found.
348 bool CMICmdArgSet::GetArg(const CMIUtilString &vArgName,
349 CMICmdArgValBase *&vpArg) const {
351 SetCmdArgs_t::const_iterator it = m_setCmdArgs.begin();
352 while (it != m_setCmdArgs.end()) {
353 CMICmdArgValBase *pArg(*it);
354 if (pArg->GetName() == vArgName) {
368 //------------------------------------------------------------------------------------
369 // Details: Write a warning message to the MI Log file about the command's
371 // options that were found present but not handled.
373 // Args: vrCmdName - (R) The command's name.
377 void CMICmdArgSet::WarningArgsNotHandledbyCmdLogFile(
378 const CMIUtilString &vrCmdName) {
379 #if MICONFIG_GIVE_WARNING_CMD_ARGS_NOT_HANDLED
381 CMIUtilString strArgsNotHandled;
382 const CMICmdArgSet::SetCmdArgs_t &rSetArgs = GetArgsNotHandledByCmd();
384 CMICmdArgSet::SetCmdArgs_t::const_iterator it = rSetArgs.begin();
385 while (it != rSetArgs.end()) {
387 strArgsNotHandled += m_constStrCommaSpc;
388 const CMICmdArgValBase *pArg = *it;
389 strArgsNotHandled += pArg->GetName();
395 const CMIUtilString strWarningMsg(
396 CMIUtilString::Format(MIRSRC(IDS_CMD_WRN_ARGS_NOT_HANDLED),
397 vrCmdName.c_str(), strArgsNotHandled.c_str()));
398 m_pLog->WriteLog(strWarningMsg);
400 #endif // MICONFIG_GIVE_WARNING_CMD_ARGS_NOT_HANDLED