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 // File: MICmdArgSet.cpp
13 // Overview: CMICmdArgSet implementation.
15 // Environment: Compilers: Visual C++ 12.
16 // gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1
17 // Libraries: See MIReadmetxt.
23 #include "MICmdArgSet.h"
24 #include "MICmdArgValBase.h"
25 #include "MICmnResources.h"
28 //++ ------------------------------------------------------------------------------------
29 // Details: CMICmdArgSet constructor.
35 CMICmdArgSet::CMICmdArgSet( void )
36 : m_bIsArgsPresentButNotHandledByCmd( false )
37 , m_constStrCommaSpc( ", " )
41 //++ ------------------------------------------------------------------------------------
42 // Details: CMICmdArgSet destructor.
48 CMICmdArgSet::~CMICmdArgSet( void )
54 //++ ------------------------------------------------------------------------------------
55 // Details: Release resources used by *this container object.
61 void CMICmdArgSet::Destroy( void )
63 // Delete command argument objects
64 if( !m_setCmdArgs.empty() )
66 SetCmdArgs_t::iterator it = m_setCmdArgs.begin();
67 while( it != m_setCmdArgs.end() )
69 CMICmdArgValBase * pArg( *it );
78 m_setCmdArgsThatNotValid.clear();
79 m_setCmdArgsThatAreMissing.clear();
80 m_setCmdArgsNotHandledByCmd.clear();
81 m_setCmdArgsMissingInfo.clear();
82 m_bIsArgsPresentButNotHandledByCmd = false;
85 //++ ------------------------------------------------------------------------------------
86 // Details: Retrieve the state flag indicating that the command set up ready to parse
87 // command arguments or options found that one or more arguments was indeed
88 // present but not handled. This is given as a warning in the MI log file.
91 // Return: bool - True = one or more args not handled, false = all args handled
94 bool CMICmdArgSet::IsArgsPresentButNotHandledByCmd( void ) const
96 return m_bIsArgsPresentButNotHandledByCmd;
99 //++ ------------------------------------------------------------------------------------
100 // Details: Add the list of command's arguments to parse and validate another one.
102 // Args: vArg - (R) A command argument object.
103 // Return: MIstatus::success - Functional succeeded.
104 // MIstatus::failure - Functional failed.
107 bool CMICmdArgSet::Add( const CMICmdArgValBase & vArg )
109 CMICmdArgValBase * pArg = const_cast< CMICmdArgValBase * >( &vArg );
110 m_setCmdArgs.push_back( pArg );
112 return MIstatus::success;
115 //++ ------------------------------------------------------------------------------------
116 // Details: After validating an options line of text (the context) and there is a failure,
117 // it is likely a mandatory command argument that is required is missing. This
118 // function returns the argument that should be present.
121 // Return: SetCmdArgs_t & - Set of argument objects.
124 const CMICmdArgSet::SetCmdArgs_t & CMICmdArgSet::GetArgsThatAreMissing( void ) const
126 return m_setCmdArgsThatAreMissing;
129 //++ ------------------------------------------------------------------------------------
130 // Details: After validating an options line of text (the context) and there is a failure,
131 // it may be because one or more arguments were unable to extract a value. This
132 // function returns the argument that were found to be invalid.
135 // Return: SetCmdArgs_t & - Set of argument objects.
138 const CMICmdArgSet::SetCmdArgs_t & CMICmdArgSet::GetArgsThatInvalid( void ) const
140 return m_setCmdArgsThatNotValid;
143 //++ ------------------------------------------------------------------------------------
144 // Details: The list of argument or option (objects) that were specified by the command
145 // and so recognised when parsed but were not handled. Ideally the command
146 // should handle all arguments and options presented to it. The command sends
147 // warning to the MI log file to say that these options were not handled.
148 // Used as one way to determine option that maybe should really be implemented
149 // and not just ignored.
152 // Return: SetCmdArgs_t & - Set of argument objects.
155 const CMICmdArgSet::SetCmdArgs_t & CMICmdArgSet::GetArgsNotHandledByCmd( void ) const
157 return m_setCmdArgsNotHandledByCmd;
160 //++ ------------------------------------------------------------------------------------
161 // Details: Given a set of command argument objects parse the context option string to
162 // find those argument and retrieve their value. If the function fails call
163 // GetArgsThatAreMissing() to see which commands that were mandatory were
164 // missing or failed to parse.
166 // Args: vStrMiCmd - (R) Command's name.
167 // vCmdArgsText - (RW) A command's options or argument.
168 // Return: MIstatus::success - Functional succeeded.
169 // MIstatus::failure - Functional failed.
172 bool CMICmdArgSet::Validate( const CMIUtilString & vStrMiCmd, CMICmdArgContext & vwCmdArgsText )
174 m_cmdArgContext = vwCmdArgsText;
176 // Iterate all the arguments or options required by a command
177 const MIuint nArgs = vwCmdArgsText.GetNumberArgsPresent();
178 MIuint nArgsMandatoryCnt = 0;
179 SetCmdArgs_t::const_iterator it = m_setCmdArgs.begin();
180 while( it != m_setCmdArgs.end() )
182 const CMICmdArgValBase * pArg( *it );
183 const CMIUtilString & rArgName( pArg->GetName() ); MIunused( rArgName );
184 if( pArg->GetIsMandatory() )
186 if( !const_cast< CMICmdArgValBase * >( pArg )->Validate( vwCmdArgsText ) )
188 if( pArg->GetIsMandatory() && !pArg->GetFound() )
189 m_setCmdArgsThatAreMissing.push_back( const_cast< CMICmdArgValBase * >( pArg ) );
190 else if( pArg->GetFound() )
192 if( pArg->GetIsMissingOptions() )
193 m_setCmdArgsMissingInfo.push_back( const_cast< CMICmdArgValBase * >( pArg ) );
194 else if( !pArg->GetValid() )
195 m_setCmdArgsThatNotValid.push_back( const_cast< CMICmdArgValBase * >( pArg ) );
198 if( pArg->GetFound() && !pArg->GetIsHandledByCmd() )
200 m_bIsArgsPresentButNotHandledByCmd = true;
201 m_setCmdArgsNotHandledByCmd.push_back( const_cast< CMICmdArgValBase * >( pArg ) );
208 // Check that one or more argument objects have any issues to report...
210 if( nArgs < nArgsMandatoryCnt )
212 SetErrorDescription( CMIUtilString::Format( MIRSRC( IDS_CMD_ARGS_ERR_N_OPTIONS_REQUIRED ), nArgsMandatoryCnt ) );
213 return MIstatus::failure;
216 if( IsArgsPresentButNotHandledByCmd() )
217 WarningArgsNotHandledbyCmdLogFile( vStrMiCmd );
219 return ValidationFormErrorMessages( vwCmdArgsText );
222 //++ ------------------------------------------------------------------------------------
223 // Details: Having validated the command's options text and failed for some reason form
224 // the error message made up with the faults found.
226 // vCmdArgsText - (RW) A command's options or argument.
227 // Return: MIstatus::success - Functional succeeded.
228 // MIstatus::failure - Functional failed.
231 bool CMICmdArgSet::ValidationFormErrorMessages( const CMICmdArgContext & vwCmdArgsText )
233 CMIUtilString strListMissing;
234 CMIUtilString strListInvalid;
235 CMIUtilString strListMissingInfo;
236 const bool bArgsMissing = (m_setCmdArgsThatAreMissing.size() > 0);
237 const bool bArgsInvalid = (m_setCmdArgsThatNotValid.size() > 0);
238 const bool bArgsMissingInfo = (m_setCmdArgsMissingInfo.size() > 0);
239 if( !(bArgsMissing || bArgsInvalid || bArgsMissingInfo) )
240 return MIstatus::success;
244 SetCmdArgs_t::const_iterator it = m_setCmdArgsThatAreMissing.begin();
245 while( it != m_setCmdArgsThatAreMissing.end() )
248 strListMissing += m_constStrCommaSpc;
250 const CMICmdArgValBase * pArg( *it );
251 strListMissing += pArg->GetName();
260 SetCmdArgs_t::const_iterator it = m_setCmdArgsThatNotValid.begin();
261 while( it != m_setCmdArgsThatNotValid.end() )
264 strListMissing += m_constStrCommaSpc;
266 const CMICmdArgValBase * pArg( *it );
267 strListInvalid += pArg->GetName();
273 if( bArgsMissingInfo )
276 SetCmdArgs_t::const_iterator it = m_setCmdArgsMissingInfo.begin();
277 while( it != m_setCmdArgsMissingInfo.end() )
280 strListMissingInfo += m_constStrCommaSpc;
282 const CMICmdArgValBase * pArg( *it );
283 strListMissingInfo += pArg->GetName();
290 bool bHaveOneError = false;
291 CMIUtilString strError = MIRSRC( IDS_CMD_ARGS_ERR_PREFIX_MSG );
292 if( bArgsMissing && bArgsInvalid )
294 bHaveOneError = true;
295 strError += CMIUtilString::Format( MIRSRC( IDS_CMD_ARGS_ERR_VALIDATION_MAN_INVALID ), strListMissing.c_str(), strListInvalid.c_str() );
301 bHaveOneError = true;
302 strError += CMIUtilString::Format( MIRSRC( IDS_CMD_ARGS_ERR_VALIDATION_MANDATORY ), strListMissing.c_str() );
304 if( bArgsMissingInfo )
308 bHaveOneError = true;
309 strError += CMIUtilString::Format( MIRSRC( IDS_CMD_ARGS_ERR_VALIDATION_MISSING_INF ), strListMissingInfo.c_str() );
315 bHaveOneError = true;
316 strError += CMIUtilString::Format( MIRSRC( IDS_CMD_ARGS_ERR_VALIDATION_INVALID ), strListInvalid.c_str() );
318 if( !vwCmdArgsText.IsEmpty() )
322 bHaveOneError = true;
323 strError += CMIUtilString::Format( MIRSRC( IDS_CMD_ARGS_ERR_CONTEXT_NOT_ALL_EATTEN ), vwCmdArgsText.GetArgsLeftToParse().c_str() );
328 SetErrorDescription( strError );
329 return MIstatus::failure;
332 return MIstatus::success;
335 //++ ------------------------------------------------------------------------------------
336 // Details: Ask if the command's argument options text had any arguments.
339 // Return: bool - True = Has one or more arguments present, false = no arguments.
342 bool CMICmdArgSet::IsArgContextEmpty( void ) const
344 return m_cmdArgContext.IsEmpty();
347 //++ ------------------------------------------------------------------------------------
348 // Details: Retrieve the number of arguments that are being used for the command.
351 // Return: MIuint - Argument count.
354 MIuint CMICmdArgSet::GetCount( void ) const
356 return m_setCmdArgs.size();
359 //++ ------------------------------------------------------------------------------------
360 // Details: Given a set of command argument objects retrieve the argument with the
363 // Args: vpArg - (W) A pointer to a command's argument object.
364 // Return: True - Argument found.
365 // False - Argument not found.
368 bool CMICmdArgSet::GetArg( const CMIUtilString & vArgName, CMICmdArgValBase *& vpArg ) const
371 SetCmdArgs_t::const_iterator it = m_setCmdArgs.begin();
372 while( it != m_setCmdArgs.end() )
374 CMICmdArgValBase * pArg( *it );
375 if( pArg->GetName() == vArgName )
389 //++ ------------------------------------------------------------------------------------
390 // Details: Write a warning message to the MI Log file about the command's arguments or
391 // options that were found present but not handled.
393 // Args: vrCmdName - (R) The command's name.
397 void CMICmdArgSet::WarningArgsNotHandledbyCmdLogFile( const CMIUtilString & vrCmdName )
399 #if MICONFIG_GIVE_WARNING_CMD_ARGS_NOT_HANDLED
401 CMIUtilString strArgsNotHandled;
402 const CMICmdArgSet::SetCmdArgs_t & rSetArgs = GetArgsNotHandledByCmd();
404 CMICmdArgSet::SetCmdArgs_t::const_iterator it = rSetArgs.begin();
405 while( it != rSetArgs.end() )
408 strArgsNotHandled += m_constStrCommaSpc;
409 const CMICmdArgValBase * pArg = *it;
410 strArgsNotHandled += pArg->GetName();
416 const CMIUtilString strWarningMsg( CMIUtilString::Format( MIRSRC( IDS_CMD_WRN_ARGS_NOT_HANDLED ), vrCmdName.c_str(), strArgsNotHandled.c_str() ) );
417 m_pLog->WriteLog( strWarningMsg );
419 #endif // MICONFIG_GIVE_WARNING_CMD_ARGS_NOT_HANDLED