appkit  1.5.1
RoadNarrows Robotics Application Kit
CmdArgDef.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: RoadNarrows Robotics Application Tool Kit
4 //
5 // Link: https://github.com/roadnarrows-robotics/rnr-sdk
6 //
7 // Library: librnr_appkit
8 //
9 // File: CmdArgDef.h
10 //
11 /*! \file
12  *
13  * \brief Command line argument definition class interface.
14  *
15  * \author Robin Knight (robin.knight@roadnarrows.com)
16  *
17  * \par Copyright
18  * \h_copy 2016-2017. RoadNarrows LLC.\n
19  * http://www.roadnarrows.com\n
20  * All Rights Reserved
21  *
22  * \par License:
23  * MIT
24  */
25 /*
26  * @EulaBegin@
27  *
28  * Permission is hereby granted, without written agreement and without
29  * license or royalty fees, to use, copy, modify, and distribute this
30  * software and its documentation for any purpose, provided that
31  * (1) The above copyright notice and the following two paragraphs
32  * appear in all copies of the source code and (2) redistributions
33  * including binaries reproduces these notices in the supporting
34  * documentation. Substantial modifications to this software may be
35  * copyrighted by their authors and need not follow the licensing terms
36  * described here, provided that the new terms are clearly indicated in
37  * all files where they apply.
38  *
39  * IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
40  * OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
41  * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
42  * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
43  * EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
44  * THE POSSIBILITY OF SUCH DAMAGE.
45  *
46  * THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
47  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
48  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
49  * "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
50  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
51  *
52  * @EulaEnd@
53  */
54 ////////////////////////////////////////////////////////////////////////////////
55 
56 #ifndef _RNR_CMD_ARG_DEF_H
57 #define _RNR_CMD_ARG_DEF_H
58 
59 #include <unistd.h>
60 #include <stdlib.h>
61 #include <stdio.h>
62 
63 #include <iostream>
64 #include <string>
65 #include <vector>
66 
68 #include "rnr/appkit/RegEx.h"
69 #include "rnr/appkit/CmdCore.h"
70 #include "rnr/appkit/CmdExtArg.h"
71 
72 /*!
73  * \brief RoadNarrows Robotics
74  */
75 namespace rnr
76 {
77  /*!
78  * \brief Commands
79  */
80  namespace cmd
81  {
82  //--------------------------------------------------------------------------
83  // CmdArgDef Class
84  //--------------------------------------------------------------------------
85 
86  /*!
87  * \brief Command argument compiled definition class.
88  */
89  class CmdArgDef
90  {
91  public:
92  /*!
93  * \brief Argument type enumeration.
94  */
95  enum ArgType
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  };
108 
109  /*!
110  * \brief Argument flag modifiers.
111  */
112  enum ArgFlags
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  };
119 
120  /*!
121  * \brief Number minimum and maximum (sub)range.
122  */
123  struct range
124  {
125  double min; ///< minimum value
126  double max; ///< maximum value
127  };
128 
129  typedef std::vector<range> RangeVec; ///< vector of subranges
130 
131  /*!
132  * \brief Default constructor.
133  */
134  CmdArgDef();
135 
136  /*!
137  * \brief Copy constructor.
138  *
139  * \param src Source object.
140  */
141  CmdArgDef(const CmdArgDef &src);
142 
143  /*!
144  * \brief Destructor.
145  */
146  virtual ~CmdArgDef();
147 
148  /*!
149  * \brief Assignment operator.
150  *
151  * \param rhs Right-hand side object.
152  *
153  * \return *this
154  */
155  CmdArgDef &operator=(const CmdArgDef &rhs);
156 
157 
158  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
159  // Public Attribute Methods
160  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
161 
162  /*!
163  * \brief Test if argument definition is sufficiently defined.
164  *
165  * \return Returns true or false.
166  */
167  bool isDefined() const;
168 
169  /*!
170  * \brief Get parent command's unique id.
171  *
172  * \return Unique id.
173  */
174  int getParentCmdUid() const
175  {
176  return m_nCmdUid;
177  }
178 
179  /*!
180  * \brief Get parent command form's index.
181  *
182  * \return Zero based index.
183  */
184  int getParentFormIndex() const
185  {
186  return m_nFormIndex;
187  }
188 
189  /*!
190  * \brief Get argument's command line index.
191  *
192  * \return Zero based index..
193  */
194  int getIndex() const
195  {
196  return m_nIndex;
197  }
198 
199  /*!
200  * \brief Get argument's name.
201  *
202  * \return Name string.
203  */
204  const std::string &getName() const
205  {
206  return m_strName;
207  }
208 
209  /*!
210  * \brief Get argument's type.
211  *
212  * \return Type enum.
213  */
214  ArgType getType() const
215  {
216  return m_eType;
217  }
218 
219  /*!
220  * \brief Get the number of mutually exclusive literals.
221  *
222  * \return Number.
223  */
224  int numOfLiterals() const
225  {
226  return (int)m_literals.size();
227  }
228 
229  /*!
230  * \brief Get literal value at index.
231  *
232  * \param nIndex Index of literal in list.
233  *
234  * \return String value.
235  */
236  const std::string &literalAt(const int nIndex) const;
237 
238  /*!
239  * \brief Get numeric range values.
240  *
241  * \return Vector of ranges.
242  */
243  const RangeVec &getRanges() const
244  {
245  return m_ranges;
246  }
247 
248  /*!
249  * \brief Get regular expression value.
250  *
251  * \return String value.
252  */
253  const std::string &getRegEx() const
254  {
255  return m_re.getRegEx();
256  }
257 
258  /*!
259  * \{
260  *
261  * \brief Check if value is in the specified range.
262  *
263  * \param value Value to check.
264  *
265  * \return Returns true or false.
266  */
267  bool inRange(const long value) const;
268 
269  bool inRange(const double value) const;
270  /*!
271  * \}
272  */
273 
274  /*!
275  * \brief Get argument's modifier flags.
276  *
277  * \return Bit-or'ed flags.
278  */
279  unsigned getFlags() const
280  {
281  return m_uFlags;
282  }
283 
284  /*!
285  * \brief Test if argument is the command argument (argv0).
286  *
287  * \return Returns true or false.
288  */
289  bool isCommand() const
290  {
291  return (m_uFlags & FlagCommand) != 0;
292  }
293 
294  /*!
295  * \brief Test if argument is an optional argument.
296  *
297  * \return Returns true or false.
298  */
299  bool isOptional() const
300  {
301  return (m_uFlags & FlagOptional) != 0;
302  }
303 
304  /*!
305  * \brief Construct literal list string.
306  *
307  * \param sep Seperator string between literals.
308  *
309  * \return Literal list string.
310  */
311  std::string constructLiteralList(const std::string sep = " ") const;
312 
313  /*!
314  * \brief Construct ranges string.
315  *
316  * \param sep Seperator string between ranges.
317  *
318  * \return Literal list string.
319  */
320  std::string constructRangeList(const std::string sep = ",") const;
321 
322  /*!
323  * \brief Construct syntax equivalent string from argument data.
324  *
325  * \return Syntax string.
326  */
327  std::string constructSyntax() const;
328 
329 
330  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
331  // Pattern Matching Methods
332  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
333 
334  /*!
335  * \brief Match argument string against argument definition pattern.
336  *
337  * The match weight returned is a heuristic measure of match strength.
338  * A value of 0.0 is no match, while anything > 0.0 is a match, with 1.0
339  * being the best possible fit. With a matched weight assigned to
340  * each command argument, the best matched command applied to the input
341  * can be heuristically determined.
342  *
343  * \par For Example:
344  * Given two arguments whose syntax are specified as:
345  * \verbatim
346  * girl
347  * <teen:word>
348  * \endverbatim
349  *
350  * Then the weights returned from the following match calls are:
351  * \verbatim
352  * arg girl: match("girl") --> 1.00
353  * arg teen: match("girl") --> 0.91
354  * arg girl: match("boy") --> 0.00
355  * arg teen: match("boy") --> 0.91
356  * \endverbatim
357  *
358  * \param strArg Argument value.
359  * \param bIgnoreCase Do [not] ignore case when applying pattern matching.
360  *
361  * \return Returns match weight [0.0 - 1.0].
362  */
363  double match(const std::string &strArg,
364  const bool bIgnoreCase = false) const;
365 
366  /*!
367  * \brief Match argument agains literal enumeration list.
368  *
369  * \param strArg Argument value.
370  * \param bIgnoreCase Do [not] ignore case when applying pattern matching.
371  *
372  * \return
373  * On success, returns literal list index >= 0. Otherwise -1 is returned.
374  */
375  int matchLiteral(const std::string &strArg,
376  const bool bIgnoreCase = false) const;
377 
378  /*!
379  * \brief Convert argument string to type.
380  *
381  * \param strArg Argument source string.
382  * \param bIgnoreCase Do [not] ignore case when applying pattern matching.
383  *
384  * \return Converted argument object.
385  */
386  CmdExtArg convert(const std::string &strArg,
387  const bool bIgnoreCase = false) const;
388 
389  /*!
390  * \brief Look up argument type, given argument type symbol.
391  *
392  * \param strSymbol Type Symbol.
393  *
394  * \return Type enum.
395  */
396  static CmdArgDef::ArgType lookupArgType(const std::string strSymbol);
397 
398  /*!
399  * \brief Look up argument symbol, given argument type.
400  *
401  * \param eType Type enum.
402  *
403  * \return Type symbol string.
404  */
405  static const std::string lookupArgSymbol(const CmdArgDef::ArgType eType);
406 
407  /*!
408  * \brief Look up argument flags, given flags value.
409  *
410  * \param uFlags Bit or'ed list of flags.
411  *
412  * \return String.
413  */
414  static const std::string lookupFlagNames(const unsigned uFlags);
415 
416 
417  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
418  // Output Methods and Operators
419  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
420 
421  /*!
422  * \brief Insert object into output stream.
423  *
424  * \param os Output stream.
425  * \param argdef Object to insert.
426  *
427  * \return Reference to output stream.
428  */
429  friend std::ostream &operator<<(std::ostream &os,
430  const CmdArgDef &argdef);
431 
432  //
433  // Friends
434  //
435  friend class CmdFormDef;
436  friend class CmdDef;
437  friend class CommandLine;
438 
439  protected:
440  int m_nCmdUid; ///< parent command's unique id
441  int m_nFormIndex; ///< parent command's form index
442  int m_nIndex; ///< argument index
443  std::string m_strName; ///< argument name
444  ArgType m_eType; ///< argument type
445  str::StringVec m_literals; ///< literal argument valid values
446  RangeVec m_ranges; ///< numeric argument valid ranges
447  RegEx m_re; ///< reg expression argument valid pattern
448  unsigned m_uFlags; ///< argument modifiers
449 
450  /*!
451  * \brief Set parent's command and form id's.
452  *
453  * \param nCmdUid Command unique id.
454  * \param nFormIndex Zero based form index within command.
455  */
456  void setParent(const int nCmdUid, const int nFormIndex);
457 
458  /*!
459  * \brief Set arguments's command line index.
460  *
461  * \param nIndex Zero based index.
462  */
463  void setIndex(const int nIndex);
464 
465  /*!
466  * \brief Set argument's name.
467  *
468  * \param strName Name string.
469  */
470  void setName(const std::string &strName);
471 
472  /*!
473  * \brief Set argument's type.
474  *
475  * \param eType Type enum.
476  */
477  void setType(const ArgType eType);
478 
479  /*!
480  * \brief Add literal value to list of argument values.
481  *
482  * \param strValue Value to add.
483  */
484  void addLiteralValue(const std::string &strValue);
485 
486  /*!
487  * \brief Set numeric range values.
488  *
489  * \param ranges Vector of ranges.
490  */
491  void setRanges(const RangeVec &ranges);
492 
493  /*!
494  * \brief Set regular expression value.
495  *
496  * \param re Regular expression.
497  */
498  void setRegEx(const RegEx &re);
499 
500  /*!
501  * \brief Or flags into argment modifier flags.
502  *
503  * \param uFlags Bit list of flags.
504  */
505  void orFlags(const unsigned uFlags);
506  }; // class CmdArgDef
507 
508  //
509  // Types and Data
510  //
511  typedef std::vector<CmdArgDef> ArgDefVec; ///< vector of argument defs
512 
513  } // namespace cmd
514 } // namespace rnr
515 
516 #endif // _RNR_CMD_ARG_DEF_H
argument has a mutually exclusive list
Definition: CmdArgDef.h:116
CmdArgDef()
Default constructor.
Definition: CmdArgDef.cxx:133
std::string constructSyntax() const
Construct syntax equivalent string from argument data.
Definition: CmdArgDef.cxx:284
Command line extended argument interface.
str::StringVec m_literals
literal argument valid values
Definition: CmdArgDef.h:445
Command EXTended ARGument class holding parsed command context and the raw and converted argmument va...
Definition: CmdExtArg.h:91
friend std::ostream & operator<<(std::ostream &os, const CmdArgDef &argdef)
Insert object into output stream.
std::vector< std::string > StringVec
Useful types.
Definition: StringTheory.h:83
floating point number (double)
Definition: CmdArgDef.h:104
double match(const std::string &strArg, const bool bIgnoreCase=false) const
Match argument string against argument definition pattern.
Definition: CmdArgDef.cxx:326
std::string constructLiteralList(const std::string sep=" ") const
Construct literal list string.
Definition: CmdArgDef.cxx:256
static const std::string lookupFlagNames(const unsigned uFlags)
Look up argument flags, given flags value.
Definition: CmdArgDef.cxx:599
CmdArgDef & operator=(const CmdArgDef &rhs)
Assignment operator.
Definition: CmdArgDef.cxx:159
int numOfLiterals() const
Get the number of mutually exclusive literals.
Definition: CmdArgDef.h:224
void setRanges(const RangeVec &ranges)
Set numeric range values.
Definition: CmdArgDef.cxx:217
double min
minimum value
Definition: CmdArgDef.h:125
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
ArgFlags
Argument flag modifiers.
Definition: CmdArgDef.h:112
Number minimum and maximum (sub)range.
Definition: CmdArgDef.h:123
Command argument compiled definition class.
Definition: CmdArgDef.h:89
unsigned m_uFlags
argument modifiers
Definition: CmdArgDef.h:448
const std::string & getRegEx() const
Get the pre-compiled regular expression.
Definition: RegEx.h:342
Regular Express Class.
Definition: RegEx.h:84
void setParent(const int nCmdUid, const int nFormIndex)
Set parent&#39;s command and form id&#39;s.
Definition: CmdArgDef.cxx:179
argument is optional
Definition: CmdArgDef.h:115
Of string spaces and their strangian operators.
void addLiteralValue(const std::string &strValue)
Add literal value to list of argument values.
Definition: CmdArgDef.cxx:200
void orFlags(const unsigned uFlags)
Or flags into argment modifier flags.
Definition: CmdArgDef.cxx:251
const std::string & literalAt(const int nIndex) const
Get literal value at index.
Definition: CmdArgDef.cxx:205
int m_nIndex
argument index
Definition: CmdArgDef.h:442
int m_nFormIndex
parent command&#39;s form index
Definition: CmdArgDef.h:441
void setType(const ArgType eType)
Set argument&#39;s type.
Definition: CmdArgDef.cxx:195
int getParentFormIndex() const
Get parent command form&#39;s index.
Definition: CmdArgDef.h:184
bool isCommand() const
Test if argument is the command argument (argv0).
Definition: CmdArgDef.h:289
Compiled command definition class.
Definition: CmdDef.h:88
static const std::string lookupArgSymbol(const CmdArgDef::ArgType eType)
Look up argument symbol, given argument type.
Definition: CmdArgDef.cxx:587
CmdExtArg convert(const std::string &strArg, const bool bIgnoreCase=false) const
Convert argument string to type.
Definition: CmdArgDef.cxx:482
unsigned getFlags() const
Get argument&#39;s modifier flags.
Definition: CmdArgDef.h:279
int getParentCmdUid() const
Get parent command&#39;s unique id.
Definition: CmdArgDef.h:174
virtual ~CmdArgDef()
Destructor.
Definition: CmdArgDef.cxx:155
RegEx m_re
reg expression argument valid pattern
Definition: CmdArgDef.h:447
ArgType m_eType
argument type
Definition: CmdArgDef.h:444
bool isOptional() const
Test if argument is an optional argument.
Definition: CmdArgDef.h:299
bool isDefined() const
Test if argument definition is sufficiently defined.
Definition: CmdArgDef.cxx:174
std::string m_strName
argument name
Definition: CmdArgDef.h:443
ArgType
Argument type enumeration.
Definition: CmdArgDef.h:95
argument is the command
Definition: CmdArgDef.h:114
int matchLiteral(const std::string &strArg, const bool bIgnoreCase=false) const
Match argument agains literal enumeration list.
Definition: CmdArgDef.cxx:454
std::vector< CmdArgDef > ArgDefVec
vector of argument defs
Definition: CmdArgDef.h:511
void setRegEx(const RegEx &re)
Set regular expression value.
Definition: CmdArgDef.cxx:246
identifier (C conforming)
Definition: CmdArgDef.h:101
ArgType getType() const
Get argument&#39;s type.
Definition: CmdArgDef.h:214
const std::string & getName() const
Get argument&#39;s name.
Definition: CmdArgDef.h:204
Command line core data types.
std::vector< range > RangeVec
vector of subranges
Definition: CmdArgDef.h:129
static CmdArgDef::ArgType lookupArgType(const std::string strSymbol)
Look up argument type, given argument type symbol.
Definition: CmdArgDef.cxx:575
The Regular Expression Class interface.
void setIndex(const int nIndex)
Set arguments&#39;s command line index.
Definition: CmdArgDef.cxx:185
std::string constructRangeList(const std::string sep=",") const
Construct ranges string.
Definition: CmdArgDef.cxx:270
const RangeVec & getRanges() const
Get numeric range values.
Definition: CmdArgDef.h:243
RoadNarrows Robotics.
Definition: Camera.h:74
Compiled command form defintion class.
Definition: CmdFormDef.h:91
any (quoted) character sequence
Definition: CmdArgDef.h:100
CommandLine class.
Definition: CommandLine.h:444
RangeVec m_ranges
numeric argument valid ranges
Definition: CmdArgDef.h:446
int getIndex() const
Get argument&#39;s command line index.
Definition: CmdArgDef.h:194
double max
maximum value
Definition: CmdArgDef.h:126
const std::string & getRegEx() const
Get regular expression value.
Definition: CmdArgDef.h:253
argument supports repetition
Definition: CmdArgDef.h:117
void setName(const std::string &strName)
Set argument&#39;s name.
Definition: CmdArgDef.cxx:190
any non-whitespace contiguous char sequence
Definition: CmdArgDef.h:99