appkit  1.5.1
RoadNarrows Robotics Application Kit
CmdDef.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: CmdDef.h
10 //
11 /*! \file
12  *
13  * \brief Command line command 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_DEF_H
57 #define _RNR_CMD_DEF_H
58 
59 #include <stdlib.h>
60 
61 #include <iostream>
62 #include <string>
63 #include <vector>
64 
66 #include "rnr/appkit/CmdCore.h"
67 #include "rnr/appkit/CmdExtArg.h"
68 #include "rnr/appkit/CmdArgDef.h"
69 #include "rnr/appkit/CmdFormDef.h"
70 
71 /*!
72  * \brief RoadNarrows Robotics
73  */
74 namespace rnr
75 {
76  /*!
77  * \brief Commands
78  */
79  namespace cmd
80  {
81  //--------------------------------------------------------------------------
82  // CmdDef Class
83  //--------------------------------------------------------------------------
84 
85  /*!
86  * \brief Compiled command definition class.
87  */
88  class CmdDef
89  {
90  public:
91  /*!
92  * \brief Default constructor.
93  */
94  CmdDef();
95 
96  /*!
97  * \brief Copy constructor.
98  *
99  * \param src Source object.
100  */
101  CmdDef(const CmdDef &src);
102 
103  /*!
104  * \brief Destructor.
105  */
106  virtual ~CmdDef();
107 
108  /*!
109  * \brief Assignment operator.
110  *
111  * \param rhs Right-hand side object.
112  *
113  * \return *this
114  */
115  CmdDef &operator=(const CmdDef &rhs);
116 
117 
118  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
119  // Public Attribute Methods
120  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
121 
122  /*
123  * \brief Test if command definition is sufficiently defined.
124  *
125  * \return Returns true or false.
126  */
127  bool isDefined() const;
128 
129  /*!
130  * \brief Get command's unique id.
131  *
132  * \return Unique id.
133  */
134  int getUid() const
135  {
136  return m_nUid;
137  }
138 
139  /*!
140  * \brief Return command's name.
141  *
142  * \return Name string.
143  */
144  const std::string &getName() const
145  {
146  return m_strName;
147  }
148 
149  /*!
150  * \brief Return command's syntax usage.
151  *
152  * \return Syntax string.
153  */
154  const std::string &getSyntax() const
155  {
156  return m_strSyntax;
157  }
158 
159  /*!
160  * \brief Return command's synopsis.
161  *
162  * \return Synopsis string.
163  */
164  const std::string &getSynopsis() const
165  {
166  return m_strSynopsis;
167  }
168 
169  /*!
170  * \brief Return command's long description.
171  *
172  * \return Long description string.
173  */
174  const std::string &getLongDesc() const
175  {
176  return m_strLongDesc;
177  }
178 
179  /*!
180  * \brief Get the total number command forms.
181  *
182  * \return Number of forms.
183  */
184  int numOfForms() const
185  {
186  return (int)m_formDefs.size();
187  }
188 
189  /*!
190  * \brief Get command form at index.
191  *
192  * \param nIndex Form's index.
193  *
194  * \return Command form definition reference.
195  */
196  const CmdFormDef &at(const int nIndex) const;
197 
198  /*!
199  * \brief Index operator.
200  *
201  * Get command form at index.
202  *
203  * \param nIndex Argument index.
204  *
205  * \return Command form definition reference.
206  */
207  const CmdFormDef &operator[](const int nIndex) const
208  {
209  return at(nIndex);
210  }
211 
212  /*!
213  * \brief Insert object into output stream.
214  *
215  * \param os Output stream.
216  * \param cmddef Object to insert.
217  *
218  * \return Reference to output stream.
219  */
220  friend std::ostream &operator<<(std::ostream &os, const CmdDef &cmddef);
221 
222  //
223  // Friends
224  //
225  friend class CommandLine;
226 
227  protected:
228  int m_nUid; ///< command unique id
229  std::string m_strName; ///< command name
230  std::string m_strSyntax; ///< parsable command extended usage syntax
231  std::string m_strSynopsis; ///< short command synopsis
232  std::string m_strLongDesc; ///< long command description
233  CmdFormDefVec m_formDefs; ///< vector of command forms
234 
235  /*!
236  * \brief Reset command definition to pre-compiled state.
237  */
238  void reset();
239 
240  /*!
241  * \brief Set command's unique id.
242  *
243  * \param uid Unique id.
244  */
245  void setUid(const int uid);
246 
247  /*!
248  * \brief Set command's name.
249  *
250  * \param strName Name string.
251  */
252  void setName(const std::string &strName);
253 
254  ///@{
255  /*!
256  * \brief Add help to command.
257  *
258  * \param sSynopsis Short command synopsis.
259  * \param sLongDesc Long command description.
260  * \param strSynopsis Short command synopsis.
261  * \param strLongDesc Long command description.
262  */
263  void addHelp(const char *sSynopsis, const char *sLongDesc);
264 
265  void addHelp(const std::string &strSynopsis,
266  const std::string &strLongDesc);
267  ///@}
268 
269  /*!
270  * \brief Push new command form to end of form list.
271  *
272  * \param formdef Command form definition.
273  */
274  void pushForm(CmdFormDef &formdef);
275 
276  /*!
277  * \brief Get command modifiable form at index.
278  *
279  * Protected version of at().
280  *
281  * \param nIndex Form's index.
282  *
283  * \return Command form definition reference.
284  */
285  CmdFormDef &formAt(const int nIndex);
286 
287  }; // class CmdDef
288 
289  //
290  // Types and Data
291  //
292 
293  } // namespace cmd
294 } // namespace rnr
295 
296 #endif // _RNR_CMD_DEF_H
Command line command form definition class interface.
CmdFormDef & formAt(const int nIndex)
Get command modifiable form at index.
Definition: CmdDef.cxx:187
Command line extended argument interface.
void reset()
Reset command definition to pre-compiled state.
Definition: CmdDef.cxx:134
const CmdFormDef & operator[](const int nIndex) const
Index operator.
Definition: CmdDef.h:207
std::string m_strSynopsis
short command synopsis
Definition: CmdDef.h:231
int getUid() const
Get command&#39;s unique id.
Definition: CmdDef.h:134
int m_nUid
command unique id
Definition: CmdDef.h:228
virtual ~CmdDef()
Destructor.
Definition: CmdDef.cxx:112
std::string m_strLongDesc
long command description
Definition: CmdDef.h:232
const std::string & getSynopsis() const
Return command&#39;s synopsis.
Definition: CmdDef.h:164
const CmdFormDef & at(const int nIndex) const
Get command form at index.
Definition: CmdDef.cxx:175
CmdDef()
Default constructor.
Definition: CmdDef.cxx:97
Of string spaces and their strangian operators.
Compiled command definition class.
Definition: CmdDef.h:88
std::string m_strName
command name
Definition: CmdDef.h:229
std::string m_strSyntax
parsable command extended usage syntax
Definition: CmdDef.h:230
CmdDef & operator=(const CmdDef &rhs)
Assignment operator.
Definition: CmdDef.cxx:116
void addHelp(const char *sSynopsis, const char *sLongDesc)
Add help to command.
Definition: CmdDef.cxx:152
Command line argument definition class interface.
void setName(const std::string &strName)
Set command&#39;s name.
Definition: CmdDef.cxx:147
const std::string & getName() const
Return command&#39;s name.
Definition: CmdDef.h:144
void pushForm(CmdFormDef &formdef)
Push new command form to end of form list.
Definition: CmdDef.cxx:166
const std::string & getLongDesc() const
Return command&#39;s long description.
Definition: CmdDef.h:174
void setUid(const int uid)
Set command&#39;s unique id.
Definition: CmdDef.cxx:142
Command line core data types.
std::vector< CmdFormDef > CmdFormDefVec
vector of command forms
Definition: CmdFormDef.h:311
int numOfForms() const
Get the total number command forms.
Definition: CmdDef.h:184
const std::string & getSyntax() const
Return command&#39;s syntax usage.
Definition: CmdDef.h:154
RoadNarrows Robotics.
Definition: Camera.h:74
Compiled command form defintion class.
Definition: CmdFormDef.h:91
CommandLine class.
Definition: CommandLine.h:444
friend std::ostream & operator<<(std::ostream &os, const CmdDef &cmddef)
Insert object into output stream.
CmdFormDefVec m_formDefs
vector of command forms
Definition: CmdDef.h:233