appkit  1.5.1
RoadNarrows Robotics Application Kit
CmdFormDef.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: CmdFormDef.h
10 //
11 /*! \file
12  *
13  * \brief Command line command form 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_FORM_DEF_H
57 #define _RNR_CMD_FORM_DEF_H
58 
59 #include <stdio.h>
60 #include <unistd.h>
61 #include <stdlib.h>
62 
63 #include <iostream>
64 #include <string>
65 #include <vector>
66 
67 #include "rnr/rnrconfig.h"
68 #include "rnr/log.h"
69 
70 #include "rnr/appkit/IOManip.h"
71 #include "rnr/appkit/CmdCore.h"
72 #include "rnr/appkit/CmdArgDef.h"
73 
74 /*!
75  * \brief RoadNarrows Robotics
76  */
77 namespace rnr
78 {
79  /*!
80  * \brief Commands
81  */
82  namespace cmd
83  {
84  //--------------------------------------------------------------------------
85  // CmdFormDef Class
86  //--------------------------------------------------------------------------
87 
88  /*!
89  * \brief Compiled command form defintion class.
90  */
91  class CmdFormDef
92  {
93  public:
94  /*!
95  * \brief Default constructor.
96  */
97  CmdFormDef();
98 
99  /*!
100  * \brief Initialization constructor.
101  */
102  CmdFormDef(const std::string &strSyntax);
103 
104  /*!
105  * \brief Copy constructor.
106  *
107  * \param src Source object.
108  */
109  CmdFormDef(const CmdFormDef &src);
110 
111  /*!
112  * \brief Destructor.
113  */
114  virtual ~CmdFormDef();
115 
116  /*!
117  * \brief Assignment operator.
118  *
119  * \param rhs Right-hand side object.
120  *
121  * \return *this
122  */
123  CmdFormDef &operator=(const CmdFormDef &rhs);
124 
125 
126  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
127  // Public Attribute Methods
128  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
129 
130  /*
131  * \brief Test if form definition is sufficiently defined.
132  *
133  * \return Returns true or false.
134  */
135  bool isDefined() const;
136 
137  /*!
138  * \brief Get parent command's unique id.
139  *
140  * \return Unique id.
141  */
142  int getParentCmdUid() const
143  {
144  return m_nCmdUid;
145  }
146 
147  /*!
148  * \brief Get forms's index.
149  *
150  * \return Zero based index.
151  */
152  int getIndex() const
153  {
154  return m_nIndex;
155  }
156 
157  /*!
158  * \brief Get form's extended usage syntax.
159  */
160  std::string getSyntax() const
161  {
162  return m_strSyntax;
163  }
164 
165  /*!
166  * \brief Get form's argument at index.
167  *
168  * \param nIndex Argument index.
169  *
170  * \return Command argument definition reference.
171  */
172  const CmdArgDef &at(const int nIndex) const;
173 
174  /*!
175  * \brief Index operator.
176  *
177  * Get form's argument at index.
178  *
179  * \param nIndex Argument index.
180  *
181  * \return Command argument definition reference.
182  */
183  const CmdArgDef &operator[](const int nIndex) const
184  {
185  return at(nIndex);
186  }
187 
188  /*!
189  * \brief Get the total number of arguments.
190  *
191  * The first argument (argv0) is the command.
192  *
193  * \return Number of arguments.
194  */
195  int numOfArgs() const
196  {
197  return (int)m_argDefs.size();
198  }
199 
200  /*!
201  * \brief Get the total number of required arguments.
202  *
203  * The required number includes the command argv0.
204  *
205  * Required arguments start at argument index 0.
206  *
207  * \return Number of arguments.
208  */
209  int numOfRequiredArgs() const
210  {
211  return m_nArgcReq;
212  }
213 
214  /*!
215  * \brief Get the total number of optional arguments.
216  *
217  * Optional arguments start at argument index numOfRequiredArgs().
218  *
219  * \return Number of arguments.
220  */
221  int numOfOptionalArgs() const
222  {
223  return m_nArgcOpt;
224  }
225 
226 
227  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
228  // Output Methods and Operators
229  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
230 
231  /*!
232  * \brief Insert object into output stream.
233  *
234  * \param os Output stream.
235  * \param formdef Object to insert.
236  *
237  * \return Reference to output stream.
238  */
239  friend std::ostream &operator<<(std::ostream &os,
240  const CmdFormDef &formdef);
241 
242  //
243  // Friends
244  //
245  friend class CmdDef;
246  friend class CommandLine;
247 
248  protected:
249  int m_nCmdUid; ///< parent command's unique id
250  int m_nIndex; ///< forms index
251  std::string m_strSyntax; ///< command extened usage syntax
252  ArgDefVec m_argDefs; ///< command argument definitions
253  int m_nArgcReq; ///< number of required arguments
254  int m_nArgcOpt; ///< number of optional arguments
255 
256  /*!
257  * \brief Reset form definition to pre-compiled state.
258  */
259  void reset();
260 
261  /*!
262  * \brief Set parent's command id.
263  *
264  * \param nCmdUid Command unique id.
265  */
266  void setParent(const int nCmdUid);
267 
268  /*!
269  * \brief Set forms's index.
270  *
271  * \param nIndex Zero based index.
272  */
273  void setIndex(const int nIndex);
274 
275  /*!
276  * \brief Set command's extended usage syntax.
277  *
278  * \param strSyntax Syntax string.
279  */
280  void setSyntax(const std::string &strSyntax);
281 
282  /*!
283  * \brief Push argument to end of argument list.
284  *
285  * \param argdef Command argument definition.
286  */
287  void pushArg(CmdArgDef &argdef);
288 
289  /*!
290  * \brief Get form's modifiable argument at index.
291  *
292  * Protected version of at().
293  *
294  * \param nIndex Argument index.
295  *
296  * \return Command argument definition reference.
297  */
298  CmdArgDef &argAt(const int nIndex);
299 
300  /*!
301  * \brief Get the last pushed argument.
302  *
303  * \return Argument definition reference.
304  */
305  CmdArgDef &lastArg();
306  }; // class CmdFormDef
307 
308  //
309  // Types and Data
310  //
311  typedef std::vector<CmdFormDef> CmdFormDefVec; ///< vector of command forms
312 
313  } // namespace cmd
314 } // namespace rnr
315 
316 #endif // _RNR_CMD_FORM_DEF_H
int numOfOptionalArgs() const
Get the total number of optional arguments.
Definition: CmdFormDef.h:221
std::string m_strSyntax
command extened usage syntax
Definition: CmdFormDef.h:251
CmdArgDef & argAt(const int nIndex)
Get form&#39;s modifiable argument at index.
Definition: CmdFormDef.cxx:188
int numOfRequiredArgs() const
Get the total number of required arguments.
Definition: CmdFormDef.h:209
void pushArg(CmdArgDef &argdef)
Push argument to end of argument list.
Definition: CmdFormDef.cxx:167
Command argument compiled definition class.
Definition: CmdArgDef.h:89
void setSyntax(const std::string &strSyntax)
Set command&#39;s extended usage syntax.
Definition: CmdFormDef.cxx:162
int getParentCmdUid() const
Get parent command&#39;s unique id.
Definition: CmdFormDef.h:142
CmdFormDef & operator=(const CmdFormDef &rhs)
Assignment operator.
Definition: CmdFormDef.cxx:127
int m_nIndex
forms index
Definition: CmdFormDef.h:250
Compiled command definition class.
Definition: CmdDef.h:88
int m_nCmdUid
parent command&#39;s unique id
Definition: CmdFormDef.h:249
int getIndex() const
Get forms&#39;s index.
Definition: CmdFormDef.h:152
void setParent(const int nCmdUid)
Set parent&#39;s command id.
Definition: CmdFormDef.cxx:152
Stream I/O manipulators and helpers.
CmdArgDef & lastArg()
Get the last pushed argument.
Definition: CmdFormDef.cxx:200
Command line argument definition class interface.
std::vector< CmdArgDef > ArgDefVec
vector of argument defs
Definition: CmdArgDef.h:511
virtual ~CmdFormDef()
Destructor.
Definition: CmdFormDef.cxx:123
const CmdArgDef & operator[](const int nIndex) const
Index operator.
Definition: CmdFormDef.h:183
int m_nArgcReq
number of required arguments
Definition: CmdFormDef.h:253
Command line core data types.
std::vector< CmdFormDef > CmdFormDefVec
vector of command forms
Definition: CmdFormDef.h:311
CmdFormDef()
Default constructor.
Definition: CmdFormDef.cxx:96
std::string getSyntax() const
Get form&#39;s extended usage syntax.
Definition: CmdFormDef.h:160
void reset()
Reset form definition to pre-compiled state.
Definition: CmdFormDef.cxx:145
friend std::ostream & operator<<(std::ostream &os, const CmdFormDef &formdef)
Insert object into output stream.
void setIndex(const int nIndex)
Set forms&#39;s index.
Definition: CmdFormDef.cxx:157
ArgDefVec m_argDefs
command argument definitions
Definition: CmdFormDef.h:252
RoadNarrows Robotics.
Definition: Camera.h:74
Compiled command form defintion class.
Definition: CmdFormDef.h:91
CommandLine class.
Definition: CommandLine.h:444
const CmdArgDef & at(const int nIndex) const
Get form&#39;s argument at index.
Definition: CmdFormDef.cxx:176
int m_nArgcOpt
number of optional arguments
Definition: CmdFormDef.h:254
int numOfArgs() const
Get the total number of arguments.
Definition: CmdFormDef.h:195