appkit  1.5.1
RoadNarrows Robotics Application Kit
rnr::cmd::CmdArgDef Class Reference

Command argument compiled definition class. More...

#include <CmdArgDef.h>

Classes

struct  range
 Number minimum and maximum (sub)range. More...
 

Public Types

enum  ArgType {
  ArgTypeUndef = 0,
  ArgTypeLiteral,
  ArgTypeWord,
  ArgTypeMultiWord,
  ArgTypeIdentifier,
  ArgTypeBoolean,
  ArgTypeInteger,
  ArgTypeFpn,
  ArgTypeFile,
  ArgTypeRegEx
}
 Argument type enumeration. More...
 
enum  ArgFlags {
  FlagCommand = 0x0001,
  FlagOptional = 0x0002,
  FlagXorList = 0x0004,
  FlagRepeat = 0x0008
}
 Argument flag modifiers. More...
 
typedef std::vector< rangeRangeVec
 vector of subranges
 

Public Member Functions

 CmdArgDef ()
 Default constructor.
 
 CmdArgDef (const CmdArgDef &src)
 Copy constructor. More...
 
virtual ~CmdArgDef ()
 Destructor.
 
CmdArgDefoperator= (const CmdArgDef &rhs)
 Assignment operator. More...
 
bool isDefined () const
 Test if argument definition is sufficiently defined. More...
 
int getParentCmdUid () const
 Get parent command's unique id. More...
 
int getParentFormIndex () const
 Get parent command form's index. More...
 
int getIndex () const
 Get argument's command line index. More...
 
const std::string & getName () const
 Get argument's name. More...
 
ArgType getType () const
 Get argument's type. More...
 
int numOfLiterals () const
 Get the number of mutually exclusive literals. More...
 
const std::string & literalAt (const int nIndex) const
 Get literal value at index. More...
 
const RangeVecgetRanges () const
 Get numeric range values. More...
 
const std::string & getRegEx () const
 Get regular expression value. More...
 
unsigned getFlags () const
 Get argument's modifier flags. More...
 
bool isCommand () const
 Test if argument is the command argument (argv0). More...
 
bool isOptional () const
 Test if argument is an optional argument. More...
 
std::string constructLiteralList (const std::string sep=" ") const
 Construct literal list string. More...
 
std::string constructRangeList (const std::string sep=",") const
 Construct ranges string. More...
 
std::string constructSyntax () const
 Construct syntax equivalent string from argument data. More...
 
double match (const std::string &strArg, const bool bIgnoreCase=false) const
 Match argument string against argument definition pattern. More...
 
int matchLiteral (const std::string &strArg, const bool bIgnoreCase=false) const
 Match argument agains literal enumeration list. More...
 
CmdExtArg convert (const std::string &strArg, const bool bIgnoreCase=false) const
 Convert argument string to type. More...
 
bool inRange (const long value) const
 Check if value is in the specified range. More...
 
bool inRange (const double value) const
 

Static Public Member Functions

static CmdArgDef::ArgType lookupArgType (const std::string strSymbol)
 Look up argument type, given argument type symbol. More...
 
static const std::string lookupArgSymbol (const CmdArgDef::ArgType eType)
 Look up argument symbol, given argument type. More...
 
static const std::string lookupFlagNames (const unsigned uFlags)
 Look up argument flags, given flags value. More...
 

Protected Member Functions

void setParent (const int nCmdUid, const int nFormIndex)
 Set parent's command and form id's. More...
 
void setIndex (const int nIndex)
 Set arguments's command line index. More...
 
void setName (const std::string &strName)
 Set argument's name. More...
 
void setType (const ArgType eType)
 Set argument's type. More...
 
void addLiteralValue (const std::string &strValue)
 Add literal value to list of argument values. More...
 
void setRanges (const RangeVec &ranges)
 Set numeric range values. More...
 
void setRegEx (const RegEx &re)
 Set regular expression value. More...
 
void orFlags (const unsigned uFlags)
 Or flags into argment modifier flags. More...
 

Protected Attributes

int m_nCmdUid
 parent command's unique id
 
int m_nFormIndex
 parent command's form index
 
int m_nIndex
 argument index
 
std::string m_strName
 argument name
 
ArgType m_eType
 argument type
 
str::StringVec m_literals
 literal argument valid values
 
RangeVec m_ranges
 numeric argument valid ranges
 
RegEx m_re
 reg expression argument valid pattern
 
unsigned m_uFlags
 argument modifiers
 

Friends

class CmdFormDef
 
class CmdDef
 
class CommandLine
 
std::ostream & operator<< (std::ostream &os, const CmdArgDef &argdef)
 Insert object into output stream. More...
 

Detailed Description

Command argument compiled definition class.

Definition at line 89 of file CmdArgDef.h.

Member Enumeration Documentation

Argument flag modifiers.

Enumerator
FlagCommand 

argument is the command

FlagOptional 

argument is optional

FlagXorList 

argument has a mutually exclusive list

FlagRepeat 

argument supports repetition

Definition at line 112 of file CmdArgDef.h.

113  {
114  FlagCommand = 0x0001, ///< argument is the command
115  FlagOptional = 0x0002, ///< argument is optional
116  FlagXorList = 0x0004, ///< argument has a mutually exclusive list
117  FlagRepeat = 0x0008 ///< argument supports repetition
118  };
argument has a mutually exclusive list
Definition: CmdArgDef.h:116
argument is optional
Definition: CmdArgDef.h:115
argument is the command
Definition: CmdArgDef.h:114
argument supports repetition
Definition: CmdArgDef.h:117

Argument type enumeration.

Enumerator
ArgTypeUndef 

undefined argument type

ArgTypeLiteral 

literal constant

ArgTypeWord 

any non-whitespace contiguous char sequence

ArgTypeMultiWord 

any (quoted) character sequence

ArgTypeIdentifier 

identifier (C conforming)

ArgTypeBoolean 

boolean (bool)

ArgTypeInteger 

integer (long)

ArgTypeFpn 

floating point number (double)

ArgTypeFile 

file path

ArgTypeRegEx 

regular expression

Definition at line 95 of file CmdArgDef.h.

96  {
97  ArgTypeUndef = 0, ///< undefined argument type
98  ArgTypeLiteral, ///< literal constant
99  ArgTypeWord, ///< any non-whitespace contiguous char sequence
100  ArgTypeMultiWord, ///< any (quoted) character sequence
101  ArgTypeIdentifier, ///< identifier (C conforming)
102  ArgTypeBoolean, ///< boolean (bool)
103  ArgTypeInteger, ///< integer (long)
104  ArgTypeFpn, ///< floating point number (double)
105  ArgTypeFile, ///< file path
106  ArgTypeRegEx ///< regular expression
107  };
floating point number (double)
Definition: CmdArgDef.h:104
undefined argument type
Definition: CmdArgDef.h:97
identifier (C conforming)
Definition: CmdArgDef.h:101
any (quoted) character sequence
Definition: CmdArgDef.h:100
any non-whitespace contiguous char sequence
Definition: CmdArgDef.h:99

Constructor & Destructor Documentation

CmdArgDef::CmdArgDef ( const CmdArgDef src)

Copy constructor.

Parameters
srcSource object.

Definition at line 142 of file CmdArgDef.cxx.

References m_eType, m_literals, m_nCmdUid, m_nFormIndex, m_nIndex, m_ranges, m_re, m_strName, and m_uFlags.

143 {
144  m_nCmdUid = src.m_nCmdUid;
146  m_nIndex = src.m_nIndex;
147  m_strName = src.m_strName;
148  m_eType = src.m_eType;
149  m_literals = src.m_literals;
150  m_ranges = src.m_ranges;
151  m_re = src.m_re;
152  m_uFlags = src.m_uFlags;
153 }
str::StringVec m_literals
literal argument valid values
Definition: CmdArgDef.h:445
int m_nCmdUid
parent command&#39;s unique id
Definition: CmdArgDef.h:440
unsigned m_uFlags
argument modifiers
Definition: CmdArgDef.h:448
int m_nIndex
argument index
Definition: CmdArgDef.h:442
int m_nFormIndex
parent command&#39;s form index
Definition: CmdArgDef.h:441
RegEx m_re
reg expression argument valid pattern
Definition: CmdArgDef.h:447
ArgType m_eType
argument type
Definition: CmdArgDef.h:444
std::string m_strName
argument name
Definition: CmdArgDef.h:443
RangeVec m_ranges
numeric argument valid ranges
Definition: CmdArgDef.h:446

Member Function Documentation

void CmdArgDef::addLiteralValue ( const std::string &  strValue)
protected

Add literal value to list of argument values.

Parameters
strValueValue to add.

Definition at line 200 of file CmdArgDef.cxx.

Referenced by rnr::cmd::CommandLine::parseLiteralArg(), and rnr::cmd::CommandLine::parseXorListArg().

201 {
202  m_literals.push_back(strValue);
203 }
str::StringVec m_literals
literal argument valid values
Definition: CmdArgDef.h:445
string CmdArgDef::constructLiteralList ( const std::string  sep = " ") const

Construct literal list string.

Parameters
sepSeperator string between literals.
Returns
Literal list string.

Definition at line 256 of file CmdArgDef.cxx.

Referenced by isOptional(), lookupFlagNames(), and rnr::cmd::CommandLine::matchCommandForm().

257 {
258  stringstream ss;
259  string pre;
260 
261  for(size_t i = 0; i < numOfLiterals(); ++i)
262  {
263  ss << pre << literalAt(i);
264  pre = sep;
265  }
266 
267  return ss.str();
268 }
int numOfLiterals() const
Get the number of mutually exclusive literals.
Definition: CmdArgDef.h:224
const std::string & literalAt(const int nIndex) const
Get literal value at index.
Definition: CmdArgDef.cxx:205
string CmdArgDef::constructRangeList ( const std::string  sep = ",") const

Construct ranges string.

Parameters
sepSeperator string between ranges.
Returns
Literal list string.

Definition at line 270 of file CmdArgDef.cxx.

Referenced by isOptional(), lookupFlagNames(), and rnr::cmd::CommandLine::matchCommandForm().

271 {
272  stringstream ss;
273  string pre;
274 
275  for(size_t i = 0; i < m_ranges.size(); ++i)
276  {
277  ss << pre << m_ranges[i].min << ":" << m_ranges[i].max;
278  pre = sep;
279  }
280 
281  return ss.str();
282 }
RangeVec m_ranges
numeric argument valid ranges
Definition: CmdArgDef.h:446
string CmdArgDef::constructSyntax ( ) const

Construct syntax equivalent string from argument data.

Returns
Syntax string.

Definition at line 284 of file CmdArgDef.cxx.

Referenced by isOptional().

285 {
286  stringstream ss;
287  string sep;
288 
289  switch( m_eType )
290  {
291  case ArgTypeLiteral:
292  ss << "{" << constructLiteralList(" | ") << "}";
293  break;
294  case ArgTypeWord:
295  case ArgTypeMultiWord:
296  case ArgTypeIdentifier:
297  case ArgTypeBoolean:
298  case ArgTypeFile:
299  ss << "<" << m_strName << ":" << lookupArgSymbol(m_eType) << ">";
300  break;
301  case ArgTypeInteger:
302  case ArgTypeFpn:
303  ss << "<" << m_strName << ":" << lookupArgSymbol(m_eType);
304  if( m_ranges.size() > 0 )
305  {
306  ss << "(" << constructRangeList() << ")";
307  }
308  ss << ">";
309  break;
310  case ArgTypeRegEx:
311  ss << "<" << m_strName << ":" << lookupArgSymbol(m_eType);
312  if( m_re.isValid() )
313  {
314  ss << "(" << m_re.getRegEx() << ")";
315  }
316  ss << ">";
317  break;
318  default:
319  ss << "<" << m_strName << ">";
320  break;
321  }
322 
323  return ss.str();
324 }
floating point number (double)
Definition: CmdArgDef.h:104
std::string constructLiteralList(const std::string sep=" ") const
Construct literal list string.
Definition: CmdArgDef.cxx:256
const std::string & getRegEx() const
Get the pre-compiled regular expression.
Definition: RegEx.h:342
static const std::string lookupArgSymbol(const CmdArgDef::ArgType eType)
Look up argument symbol, given argument type.
Definition: CmdArgDef.cxx:587
RegEx m_re
reg expression argument valid pattern
Definition: CmdArgDef.h:447
ArgType m_eType
argument type
Definition: CmdArgDef.h:444
std::string m_strName
argument name
Definition: CmdArgDef.h:443
identifier (C conforming)
Definition: CmdArgDef.h:101
std::string constructRangeList(const std::string sep=",") const
Construct ranges string.
Definition: CmdArgDef.cxx:270
bool isValid() const
Test if in a valid state (i.e. compiled).
Definition: RegEx.h:354
any (quoted) character sequence
Definition: CmdArgDef.h:100
RangeVec m_ranges
numeric argument valid ranges
Definition: CmdArgDef.h:446
any non-whitespace contiguous char sequence
Definition: CmdArgDef.h:99
CmdExtArg CmdArgDef::convert ( const std::string &  strArg,
const bool  bIgnoreCase = false 
) const

Convert argument string to type.

Parameters
strArgArgument source string.
bIgnoreCaseDo [not] ignore case when applying pattern matching.
Returns
Converted argument object.

Definition at line 482 of file CmdArgDef.cxx.

References rnr::cmd::CmdExtArg::b(), rnr::cmd::CmdExtArg::e(), rnr::cmd::CmdExtArg::f(), rnr::cmd::CmdExtArg::i(), rnr::str::lowercase(), rnr::cmd::CmdExtArg::s(), rnr::str::tobool(), rnr::str::todouble(), and rnr::str::tolong().

Referenced by isOptional(), and rnr::cmd::CommandLine::matchCommandForm().

483 {
484  CmdExtArg cvt(m_nCmdUid, m_nFormIndex, m_nIndex, 0, strArg);
485 
486  switch( m_eType )
487  {
488  //
489  // Converted literal value is the literal enum list index.
490  //
491  case ArgTypeLiteral:
492  {
493  long val;
494 
495  if( (val = matchLiteral(strArg, bIgnoreCase)) >= 0 )
496  {
497  cvt.e(val);
498  }
499  }
500  break;
501 
502  //
503  // The converted value is the argument for these argument types.
504  //
505  case ArgTypeWord:
506  case ArgTypeMultiWord:
507  case ArgTypeIdentifier:
508  case ArgTypeFile:
509  case ArgTypeRegEx:
510  cvt.s(strArg);
511  break;
512 
513  //
514  // Converted to boolean.
515  //
516  case ArgTypeBoolean:
517  {
518  bool val;
519  int rc;
520 
521  if( bIgnoreCase )
522  {
523  rc = tobool(lowercase(strArg), val);
524  }
525  else
526  {
527  rc = tobool(strArg, val);
528  }
529  if( rc == OK )
530  {
531  cvt.b(val);
532  }
533  }
534  break;
535 
536  //
537  // Convert to integer.
538  //
539  case ArgTypeInteger:
540  {
541  long val;
542 
543  if( tolong(strArg, val) == OK )
544  {
545  cvt.i(val);
546  }
547  }
548  break;
549 
550  //
551  // Convert to floating-point number.
552  //
553  case ArgTypeFpn:
554  {
555  double val;
556 
557  if( todouble(strArg, val) == OK )
558  {
559  cvt.f(val);
560  }
561  }
562  break;
563 
564  //
565  // Unknown or uninitialized.
566  //
567  case ArgTypeUndef:
568  default:
569  break;
570  }
571 
572  return cvt;
573 }
int tolong(const std::string &str, long &val)
Convert string to a long integer.
Command EXTended ARGument class holding parsed command context and the raw and converted argmument va...
Definition: CmdExtArg.h:91
floating point number (double)
Definition: CmdArgDef.h:104
int tobool(const std::string &str, bool &val)
Convert string to boolean.
std::string lowercase(const std::string &str)
Convert in-place string to lower case.
int m_nCmdUid
parent command&#39;s unique id
Definition: CmdArgDef.h:440
undefined argument type
Definition: CmdArgDef.h:97
int todouble(const std::string &str, double &val)
Convert string to a double-precision floating-point number.
int m_nIndex
argument index
Definition: CmdArgDef.h:442
int m_nFormIndex
parent command&#39;s form index
Definition: CmdArgDef.h:441
ArgType m_eType
argument type
Definition: CmdArgDef.h:444
int matchLiteral(const std::string &strArg, const bool bIgnoreCase=false) const
Match argument agains literal enumeration list.
Definition: CmdArgDef.cxx:454
identifier (C conforming)
Definition: CmdArgDef.h:101
any (quoted) character sequence
Definition: CmdArgDef.h:100
any non-whitespace contiguous char sequence
Definition: CmdArgDef.h:99
unsigned rnr::cmd::CmdArgDef::getFlags ( ) const
inline

Get argument's modifier flags.

Returns
Bit-or'ed flags.

Definition at line 279 of file CmdArgDef.h.

References m_uFlags.

280  {
281  return m_uFlags;
282  }
unsigned m_uFlags
argument modifiers
Definition: CmdArgDef.h:448
int rnr::cmd::CmdArgDef::getIndex ( ) const
inline

Get argument's command line index.

Returns
Zero based index..

Definition at line 194 of file CmdArgDef.h.

References m_nIndex.

Referenced by rnr::cmd::CommandLine::matchCommandForm().

195  {
196  return m_nIndex;
197  }
int m_nIndex
argument index
Definition: CmdArgDef.h:442
const std::string& rnr::cmd::CmdArgDef::getName ( ) const
inline

Get argument's name.

Returns
Name string.

Definition at line 204 of file CmdArgDef.h.

References m_strName.

Referenced by rnr::cmd::CommandLine::getArgName(), and rnr::cmd::CommandLine::parseArgv0().

205  {
206  return m_strName;
207  }
std::string m_strName
argument name
Definition: CmdArgDef.h:443
int rnr::cmd::CmdArgDef::getParentCmdUid ( ) const
inline

Get parent command's unique id.

Returns
Unique id.

Definition at line 174 of file CmdArgDef.h.

References m_nCmdUid.

Referenced by rnr::cmd::CommandLine::rlArgDefs().

175  {
176  return m_nCmdUid;
177  }
int m_nCmdUid
parent command&#39;s unique id
Definition: CmdArgDef.h:440
int rnr::cmd::CmdArgDef::getParentFormIndex ( ) const
inline

Get parent command form's index.

Returns
Zero based index.

Definition at line 184 of file CmdArgDef.h.

References m_nFormIndex.

Referenced by rnr::cmd::CommandLine::rlArgDefs().

185  {
186  return m_nFormIndex;
187  }
int m_nFormIndex
parent command&#39;s form index
Definition: CmdArgDef.h:441
const RangeVec& rnr::cmd::CmdArgDef::getRanges ( ) const
inline

Get numeric range values.

Returns
Vector of ranges.

Definition at line 243 of file CmdArgDef.h.

References m_ranges.

Referenced by rnr::cmd::CommandLine::matchCommandForm().

244  {
245  return m_ranges;
246  }
RangeVec m_ranges
numeric argument valid ranges
Definition: CmdArgDef.h:446
const std::string& rnr::cmd::CmdArgDef::getRegEx ( ) const
inline

Get regular expression value.

Returns
String value.

Definition at line 253 of file CmdArgDef.h.

References rnr::RegEx::getRegEx(), inRange(), and m_re.

Referenced by rnr::cmd::CommandLine::matchCommandForm().

254  {
255  return m_re.getRegEx();
256  }
const std::string & getRegEx() const
Get the pre-compiled regular expression.
Definition: RegEx.h:342
RegEx m_re
reg expression argument valid pattern
Definition: CmdArgDef.h:447
ArgType rnr::cmd::CmdArgDef::getType ( ) const
inline

Get argument's type.

Returns
Type enum.

Definition at line 214 of file CmdArgDef.h.

References m_eType.

Referenced by rnr::cmd::CommandLine::getArgDefType(), rnr::cmd::CommandLine::matchCommandForm(), and rnr::cmd::CommandLine::parseArgv0().

215  {
216  return m_eType;
217  }
ArgType m_eType
argument type
Definition: CmdArgDef.h:444
bool CmdArgDef::inRange ( const long  value) const

Check if value is in the specified range.

Parameters
valueValue to check.
Returns
Returns true or false.

Definition at line 222 of file CmdArgDef.cxx.

Referenced by getRegEx().

223 {
224  return inRange((double)value);
225 }
bool inRange(const long value) const
Check if value is in the specified range.
Definition: CmdArgDef.cxx:222
bool rnr::cmd::CmdArgDef::isCommand ( ) const
inline

Test if argument is the command argument (argv0).

Returns
Returns true or false.

Definition at line 289 of file CmdArgDef.h.

References FlagCommand, and m_uFlags.

290  {
291  return (m_uFlags & FlagCommand) != 0;
292  }
unsigned m_uFlags
argument modifiers
Definition: CmdArgDef.h:448
argument is the command
Definition: CmdArgDef.h:114
bool CmdArgDef::isDefined ( ) const

Test if argument definition is sufficiently defined.

Returns
Returns true or false.

Definition at line 174 of file CmdArgDef.cxx.

175 {
176  return (m_nIndex >= 0) && !m_strName.empty() && (m_eType != ArgTypeUndef);
177 }
undefined argument type
Definition: CmdArgDef.h:97
int m_nIndex
argument index
Definition: CmdArgDef.h:442
ArgType m_eType
argument type
Definition: CmdArgDef.h:444
std::string m_strName
argument name
Definition: CmdArgDef.h:443
bool rnr::cmd::CmdArgDef::isOptional ( ) const
inline

Test if argument is an optional argument.

Returns
Returns true or false.

Definition at line 299 of file CmdArgDef.h.

References constructLiteralList(), constructRangeList(), constructSyntax(), convert(), FlagOptional, lookupArgSymbol(), lookupArgType(), lookupFlagNames(), m_uFlags, match(), matchLiteral(), and operator<<.

Referenced by rnr::cmd::CommandLine::matchCommandForm().

300  {
301  return (m_uFlags & FlagOptional) != 0;
302  }
unsigned m_uFlags
argument modifiers
Definition: CmdArgDef.h:448
argument is optional
Definition: CmdArgDef.h:115
const string & CmdArgDef::literalAt ( const int  nIndex) const

Get literal value at index.

Parameters
nIndexIndex of literal in list.
Returns
String value.

Definition at line 205 of file CmdArgDef.cxx.

References rnr::cmd::noliteral.

Referenced by numOfLiterals(), and rnr::cmd::CommandLine::parseArgv0().

206 {
207  if( (nIndex >= 0) && (nIndex < m_literals.size()) )
208  {
209  return m_literals[nIndex];
210  }
211  else
212  {
213  return noliteral;
214  }
215 }
static const string noliteral
no literal string
Definition: CmdArgDef.cxx:95
str::StringVec m_literals
literal argument valid values
Definition: CmdArgDef.h:445
const string CmdArgDef::lookupArgSymbol ( const CmdArgDef::ArgType  eType)
static

Look up argument symbol, given argument type.

Parameters
eTypeType enum.
Returns
Type symbol string.

Definition at line 587 of file CmdArgDef.cxx.

References rnr::cmd::ArgTypeLookupTbl, rnr::cmd::NameValuePair::m_sName, and rnr::cmd::undefstring.

Referenced by isOptional(), lookupFlagNames(), rnr::cmd::CommandLine::matchCommandForm(), and rnr::cmd::CommandLine::parseVarMod().

588 {
589  for(int i = 0; ArgTypeLookupTbl[i].m_sName != NULL; ++i)
590  {
591  if( eType == ArgTypeLookupTbl[i].m_nValue )
592  {
593  return ArgTypeLookupTbl[i].m_sName;
594  }
595  }
596  return undefstring;
597 }
const std::string undefstring
"undef" string
const char * m_sName
name
Definition: CmdCore.h:175
static NameValuePair ArgTypeLookupTbl[]
Argument type - string symbol lookup table.
Definition: CmdArgDef.cxx:100
CmdArgDef::ArgType CmdArgDef::lookupArgType ( const std::string  strSymbol)
static

Look up argument type, given argument type symbol.

Parameters
strSymbolType Symbol.
Returns
Type enum.

Definition at line 575 of file CmdArgDef.cxx.

References rnr::cmd::ArgTypeLookupTbl, rnr::cmd::NameValuePair::m_nValue, and rnr::cmd::NameValuePair::m_sName.

Referenced by isOptional(), and rnr::cmd::CommandLine::parseVarType().

576 {
577  for(int i = 0; ArgTypeLookupTbl[i].m_sName != NULL; ++i)
578  {
579  if( strSymbol == ArgTypeLookupTbl[i].m_sName )
580  {
582  }
583  }
585 }
undefined argument type
Definition: CmdArgDef.h:97
const char * m_sName
name
Definition: CmdCore.h:175
ArgType
Argument type enumeration.
Definition: CmdArgDef.h:95
static NameValuePair ArgTypeLookupTbl[]
Argument type - string symbol lookup table.
Definition: CmdArgDef.cxx:100
const string CmdArgDef::lookupFlagNames ( const unsigned  uFlags)
static

Look up argument flags, given flags value.

Parameters
uFlagsBit or'ed list of flags.
Returns
String.

Definition at line 599 of file CmdArgDef.cxx.

References rnr::cmd::ArgFlagLookupTbl, constructLiteralList(), constructRangeList(), rnr::io::deltaindent(), rnr::io::indent(), lookupArgSymbol(), lookupFlagNames(), m_eType, m_literals, m_nCmdUid, m_nFormIndex, m_nIndex, m_ranges, m_re, rnr::cmd::NameValuePair::m_sName, m_strName, m_uFlags, and operator<<().

Referenced by isOptional(), and lookupFlagNames().

600 {
601  stringstream ss;
602  string sep;
603 
604  for(int i = 0; ArgFlagLookupTbl[i].m_sName != NULL; ++i)
605  {
606  if( uFlags & ArgFlagLookupTbl[i].m_nValue )
607  {
608  ss << sep << ArgFlagLookupTbl[i].m_sName;
609  sep = "|";
610  }
611  }
612  return ss.str();
613 }
const char * m_sName
name
Definition: CmdCore.h:175
static NameValuePair ArgFlagLookupTbl[]
Argument flag modifiers - name lookup table.
Definition: CmdArgDef.cxx:117
double CmdArgDef::match ( const std::string &  strArg,
const bool  bIgnoreCase = false 
) const

Match argument string against argument definition pattern.

The match weight returned is a heuristic measure of match strength. A value of 0.0 is no match, while anything > 0.0 is a match, with 1.0 being the best possible fit. With a matched weight assigned to each command argument, the best matched command applied to the input can be heuristically determined.

For Example:
Given two arguments whose syntax are specified as:
* girl
* <teen:word>
* 

Then the weights returned from the following match calls are:

* arg girl: match("girl") --> 1.00
* arg teen: match("girl") --> 0.91
* arg girl: match("boy")  --> 0.00
* arg teen: match("boy")  --> 0.91
* 
Parameters
strArgArgument value.
bIgnoreCaseDo [not] ignore case when applying pattern matching.
Returns
Returns match weight [0.0 - 1.0].

Definition at line 326 of file CmdArgDef.cxx.

References rnr::cmd::isIdentifier(), rnr::str::lowercase(), rnr::str::tobool(), rnr::str::todouble(), and rnr::str::tolong().

Referenced by isOptional(), rnr::cmd::CommandLine::matchCommand(), rnr::cmd::CommandLine::matchCommandForm(), and rnr::cmd::CommandLine::rlArgDefs().

327 {
328  double fWeight = 0.0;
329 
330  switch( m_eType )
331  {
332  //
333  // Literal constant.
334  //
335  case ArgTypeLiteral:
336  if( matchLiteral(strArg, bIgnoreCase) >= 0 )
337  {
338  fWeight = 1.0;
339  }
340  break;
341 
342  //
343  // Contiguous block of non-whitespace characters.
344  //
345  case ArgTypeWord:
346  if( strArg.find_first_of(" \t\r\n\v\f") == string::npos )
347  {
348  fWeight = 0.91;
349  }
350  break;
351 
352  //
353  // Any string.
354  //
355  case ArgTypeMultiWord:
356  fWeight = 0.90;
357  break;
358 
359  //
360  // C conforming identifier.
361  //
362  case ArgTypeIdentifier:
363  if( isIdentifier(strArg) )
364  {
365  fWeight = 0.94;
366  }
367  break;
368 
369  //
370  // Boolean.
371  //
372  case ArgTypeBoolean:
373  {
374  bool val;
375  int rc;
376  if( bIgnoreCase )
377  {
378  rc = tobool(lowercase(strArg), val);
379  }
380  else
381  {
382  rc = tobool(strArg, val);
383  }
384 
385  if( rc == OK )
386  {
387  fWeight = 0.95;
388  }
389  }
390  break;
391 
392  //
393  // (Signed) integer in base 8, 10, or 16.
394  //
395  case ArgTypeInteger:
396  {
397  long val;
398  if( (tolong(strArg, val) == OK) && inRange(val) )
399  {
400  fWeight = 0.95;
401  }
402  }
403  break;
404 
405  //
406  // Floating-point number.
407  //
408  case ArgTypeFpn:
409  {
410  double val;
411  if( (todouble(strArg, val) == OK) && inRange(val) )
412  {
413  fWeight = 0.95;
414  }
415  }
416  break;
417 
418  //
419  // Regular expression.
420  //
421  case ArgTypeRegEx:
422  if( m_re.isValid() && m_re.match(strArg) )
423  {
424  fWeight = 0.93;
425  }
426  break;
427 
428  //
429  // Filename.
430  //
431  case ArgTypeFile:
432  fWeight = 0.91;
433  break;
434 
435  //
436  // Bug.
437  //
438  case ArgTypeUndef:
439  default:
440  stringstream ss;
441  ss
442  << "Bug: "
443  << "cmduid " << m_nCmdUid << ", "
444  << "form " << m_nFormIndex << ", "
445  << "arg " << m_nIndex << ": "
446  << m_eType << ": Unknown type.";
447  LOGERROR("%s", ss.str().c_str());
448  break;
449  }
450 
451  return fWeight;
452 }
int tolong(const std::string &str, long &val)
Convert string to a long integer.
floating point number (double)
Definition: CmdArgDef.h:104
int tobool(const std::string &str, bool &val)
Convert string to boolean.
std::string lowercase(const std::string &str)
Convert in-place string to lower case.
bool inRange(const long value) const
Check if value is in the specified range.
Definition: CmdArgDef.cxx:222
int m_nCmdUid
parent command&#39;s unique id
Definition: CmdArgDef.h:440
undefined argument type
Definition: CmdArgDef.h:97
int todouble(const std::string &str, double &val)
Convert string to a double-precision floating-point number.
int m_nIndex
argument index
Definition: CmdArgDef.h:442
int m_nFormIndex
parent command&#39;s form index
Definition: CmdArgDef.h:441
RegEx m_re
reg expression argument valid pattern
Definition: CmdArgDef.h:447
ArgType m_eType
argument type
Definition: CmdArgDef.h:444
int matchLiteral(const std::string &strArg, const bool bIgnoreCase=false) const
Match argument agains literal enumeration list.
Definition: CmdArgDef.cxx:454
identifier (C conforming)
Definition: CmdArgDef.h:101
bool match(const std::string &strInput, const int nFlags=ReFlagDefaults)
Match the input string against the regular expression.
bool isIdentifier(const std::string &str)
Test if string is a valid identifier.
bool isValid() const
Test if in a valid state (i.e. compiled).
Definition: RegEx.h:354
any (quoted) character sequence
Definition: CmdArgDef.h:100
any non-whitespace contiguous char sequence
Definition: CmdArgDef.h:99
int CmdArgDef::matchLiteral ( const std::string &  strArg,
const bool  bIgnoreCase = false 
) const

Match argument agains literal enumeration list.

Parameters
strArgArgument value.
bIgnoreCaseDo [not] ignore case when applying pattern matching.
Returns
On success, returns literal list index >= 0. Otherwise -1 is returned.

Definition at line 454 of file CmdArgDef.cxx.

References rnr::str::lowercase().

Referenced by isOptional().

455 {
456  if( bIgnoreCase )
457  {
458  string strICaseArg = lowercase(strArg);
459 
460  for(size_t i = 0; i < numOfLiterals(); ++i)
461  {
462  if( strICaseArg == lowercase(literalAt(i)) )
463  {
464  return (int)i;
465  }
466  }
467  }
468  else
469  {
470  for(size_t i = 0; i < numOfLiterals(); ++i)
471  {
472  if( strArg == literalAt(i) )
473  {
474  return (int)i;
475  }
476  }
477  }
478 
479  return -1;
480 }
int numOfLiterals() const
Get the number of mutually exclusive literals.
Definition: CmdArgDef.h:224
std::string lowercase(const std::string &str)
Convert in-place string to lower case.
const std::string & literalAt(const int nIndex) const
Get literal value at index.
Definition: CmdArgDef.cxx:205
int rnr::cmd::CmdArgDef::numOfLiterals ( ) const
inline

Get the number of mutually exclusive literals.

Returns
Number.

Definition at line 224 of file CmdArgDef.h.

References literalAt(), and m_literals.

225  {
226  return (int)m_literals.size();
227  }
str::StringVec m_literals
literal argument valid values
Definition: CmdArgDef.h:445
CmdArgDef & CmdArgDef::operator= ( const CmdArgDef rhs)

Assignment operator.

Parameters
rhsRight-hand side object.
Returns
*this

Definition at line 159 of file CmdArgDef.cxx.

References m_eType, m_literals, m_nCmdUid, m_nFormIndex, m_nIndex, m_ranges, m_re, m_strName, and m_uFlags.

160 {
161  m_nCmdUid = rhs.m_nCmdUid;
163  m_nIndex = rhs.m_nIndex;
164  m_strName = rhs.m_strName;
165  m_eType = rhs.m_eType;
166  m_literals = rhs.m_literals;
167  m_ranges = rhs.m_ranges;
168  m_re = rhs.m_re;
169  m_uFlags = rhs.m_uFlags;
170 
171  return *this;
172 }
str::StringVec m_literals
literal argument valid values
Definition: CmdArgDef.h:445
int m_nCmdUid
parent command&#39;s unique id
Definition: CmdArgDef.h:440
unsigned m_uFlags
argument modifiers
Definition: CmdArgDef.h:448
int m_nIndex
argument index
Definition: CmdArgDef.h:442
int m_nFormIndex
parent command&#39;s form index
Definition: CmdArgDef.h:441
RegEx m_re
reg expression argument valid pattern
Definition: CmdArgDef.h:447
ArgType m_eType
argument type
Definition: CmdArgDef.h:444
std::string m_strName
argument name
Definition: CmdArgDef.h:443
RangeVec m_ranges
numeric argument valid ranges
Definition: CmdArgDef.h:446
void CmdArgDef::orFlags ( const unsigned  uFlags)
protected

Or flags into argment modifier flags.

Parameters
uFlagsBit list of flags.

Definition at line 251 of file CmdArgDef.cxx.

Referenced by rnr::cmd::CommandLine::parseArgv0(), rnr::cmd::CommandLine::parseOptionalArgList(), and rnr::cmd::CommandLine::parseXorListArg().

252 {
253  m_uFlags |= uFlags;
254 }
unsigned m_uFlags
argument modifiers
Definition: CmdArgDef.h:448
void CmdArgDef::setIndex ( const int  nIndex)
protected

Set arguments's command line index.

Parameters
nIndexZero based index.

Definition at line 185 of file CmdArgDef.cxx.

Referenced by rnr::cmd::CmdFormDef::pushArg().

186 {
187  m_nIndex = nIndex;
188 }
int m_nIndex
argument index
Definition: CmdArgDef.h:442
void CmdArgDef::setName ( const std::string &  strName)
protected

Set argument's name.

Parameters
strNameName string.

Definition at line 190 of file CmdArgDef.cxx.

Referenced by rnr::cmd::CommandLine::parseLiteralArg(), rnr::cmd::CommandLine::parseVariableArg(), and rnr::cmd::CommandLine::parseXorListArg().

191 {
192  m_strName = strName;
193 }
std::string m_strName
argument name
Definition: CmdArgDef.h:443
void CmdArgDef::setParent ( const int  nCmdUid,
const int  nFormIndex 
)
protected

Set parent's command and form id's.

Parameters
nCmdUidCommand unique id.
nFormIndexZero based form index within command.

Definition at line 179 of file CmdArgDef.cxx.

Referenced by rnr::cmd::CmdFormDef::pushArg().

180 {
181  m_nCmdUid = nCmdUid;
182  m_nFormIndex = nFormIndex;
183 }
int m_nCmdUid
parent command&#39;s unique id
Definition: CmdArgDef.h:440
int m_nFormIndex
parent command&#39;s form index
Definition: CmdArgDef.h:441
void CmdArgDef::setRanges ( const RangeVec ranges)
protected

Set numeric range values.

Parameters
rangesVector of ranges.

Definition at line 217 of file CmdArgDef.cxx.

Referenced by rnr::cmd::CommandLine::parseVariableArg().

218 {
219  m_ranges = ranges;
220 }
RangeVec m_ranges
numeric argument valid ranges
Definition: CmdArgDef.h:446
void CmdArgDef::setRegEx ( const RegEx re)
protected

Set regular expression value.

Parameters
reRegular expression.

Definition at line 246 of file CmdArgDef.cxx.

Referenced by rnr::cmd::CommandLine::parseVariableArg().

247 {
248  m_re = re;
249 }
RegEx m_re
reg expression argument valid pattern
Definition: CmdArgDef.h:447
void CmdArgDef::setType ( const ArgType  eType)
protected

Set argument's type.

Parameters
eTypeType enum.

Definition at line 195 of file CmdArgDef.cxx.

Referenced by rnr::cmd::CommandLine::parseLiteralArg(), rnr::cmd::CommandLine::parseVariableArg(), and rnr::cmd::CommandLine::parseXorListArg().

196 {
197  m_eType = eType;
198 }
ArgType m_eType
argument type
Definition: CmdArgDef.h:444

Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  os,
const CmdArgDef argdef 
)
friend

Insert object into output stream.

Parameters
osOutput stream.
argdefObject to insert.
Returns
Reference to output stream.

Referenced by isOptional().


The documentation for this class was generated from the following files: