appkit  1.5.1
RoadNarrows Robotics Application Kit
CmdDef.cxx
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.cxx
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 #include <stdio.h>
57 #include <unistd.h>
58 #include <stdlib.h>
59 
60 #include <iostream>
61 #include <sstream>
62 #include <string>
63 
64 #include "rnr/rnrconfig.h"
65 #include "rnr/log.h"
66 
68 #include "rnr/appkit/IOManip.h"
69 #include "rnr/appkit/CmdCore.h"
70 #include "rnr/appkit/CmdExtArg.h"
71 #include "rnr/appkit/CmdArgDef.h"
72 #include "rnr/appkit/CmdFormDef.h"
73 #include "rnr/appkit/CmdDef.h"
74 
75 using namespace std;
76 using namespace rnr;
77 using namespace rnr::io;
78 using namespace rnr::cmd;
79 
80 // -----------------------------------------------------------------------------
81 // Support
82 // -----------------------------------------------------------------------------
83 
84 namespace rnr
85 {
86  namespace cmd
87  {
88  static CmdFormDef noformdef; ///< constant "no form def" form definition
89  } // namespace cmd
90 } // namespace rnr
91 
92 
93 // -----------------------------------------------------------------------------
94 // CmdDef Class
95 // -----------------------------------------------------------------------------
96 
97 CmdDef::CmdDef()
98 {
99  m_nUid = NoUid;
100 }
101 
102 CmdDef::CmdDef(const CmdDef &src)
103 {
104  m_nUid = src.m_nUid;
105  m_strName = src.m_strName;
106  m_strSyntax = src.m_strSyntax;
107  m_strSynopsis = src.m_strSynopsis;
108  m_strLongDesc = src.m_strLongDesc;
109  m_formDefs = src.m_formDefs;
110 }
111 
112 CmdDef::~CmdDef()
113 {
114 }
115 
116 CmdDef &CmdDef::operator=(const CmdDef &rhs)
117 {
118  m_nUid = rhs.m_nUid;
119  m_strName = rhs.m_strName;
120  m_strSyntax = rhs.m_strSyntax;
121  m_strSynopsis = rhs.m_strSynopsis;
122  m_strLongDesc = rhs.m_strLongDesc;
123  m_formDefs = rhs.m_formDefs;
124 }
125 
126 bool CmdDef::isDefined() const
127 {
128  return (m_nUid >= 0) &&
129  !m_strName.empty() &&
130  (numOfForms() > 0) &&
131  m_formDefs[0].isDefined();
132 }
133 
134 void CmdDef::reset()
135 {
136  for(size_t i = 0; i < m_formDefs.size(); ++i)
137  {
138  m_formDefs[i].reset();
139  }
140 }
141 
142 void CmdDef::setUid(const int uid)
143 {
144  m_nUid = uid;
145 }
146 
147 void CmdDef::setName(const string &strName)
148 {
149  m_strName = strName;
150 }
151 
152 void CmdDef::addHelp(const char *sSynopsis, const char *sLongDesc)
153 {
154  string strSynopsis(sSynopsis != NULL? sSynopsis: "");
155  string strLongDesc(sLongDesc != NULL? sLongDesc: "");
156 
157  addHelp(strSynopsis, strLongDesc);
158 }
159 
160 void CmdDef::addHelp(const string &strSynopsis, const string &strLongDesc)
161 {
162  m_strSynopsis = strSynopsis;
163  m_strLongDesc = strLongDesc;
164 }
165 
166 void CmdDef::pushForm(CmdFormDef &formdef)
167 {
168  int nIndex = numOfForms();
169 
170  formdef.setParent(m_nUid);
171  formdef.setIndex(nIndex);
172  m_formDefs.push_back(formdef);
173 }
174 
175 const CmdFormDef &CmdDef::at(const int nIndex) const
176 {
177  if( (nIndex >= 0) && (nIndex < m_formDefs.size()) )
178  {
179  return m_formDefs[nIndex];
180  }
181  else
182  {
183  return noformdef;
184  }
185 }
186 
187 CmdFormDef &CmdDef::formAt(const int nIndex)
188 {
189  if( (nIndex >= 0) && (nIndex < m_formDefs.size()) )
190  {
191  return m_formDefs[nIndex];
192  }
193  else
194  {
195  return noformdef;
196  }
197 }
198 
199 ostream &rnr::cmd::operator<<(ostream &os, const CmdDef &cmddef)
200 {
201  string sep;
202 
203  os << indent() << "{" << endl;
204 
205  os << deltaindent(2);
206 
207  os << indent() << "uid = " << cmddef.m_nUid << endl;
208  os << indent() << "name = " << cmddef.m_strName << endl;
209  os << indent() << "syntax = " << cmddef.m_strSyntax << endl;
210  os << indent() << "synopsis = " << cmddef.m_strSynopsis << endl;
211  os << indent() << "description = " << cmddef.m_strLongDesc << endl;
212  os << indent() << "forms[" << cmddef.numOfForms() << "] = " << endl;
213  os << indent() << "{" << endl;
214 
215  os << deltaindent(2);
216 
217  for(size_t i = 0; i < cmddef.m_formDefs.size(); ++i)
218  {
219  os << sep << cmddef.m_formDefs[i];
220 
221  if( sep.empty() )
222  {
223  sep = ",\n";
224  }
225  }
226  os << endl;
227 
228  os << deltaindent(-2);
229 
230  os << indent() << "}" << endl;
231 
232  os << deltaindent(-2);
233 
234  os << indent() << "}";
235 
236  return os;
237 }
Command line command form definition class interface.
Command line extended argument interface.
std::string m_strSynopsis
short command synopsis
Definition: CmdDef.h:231
int m_nUid
command unique id
Definition: CmdDef.h:228
osManipIndent indent()
Left indent at current stream indentation level.
Definition: IOManip.cxx:115
std::string m_strLongDesc
long command description
Definition: CmdDef.h:232
const int NoUid
Special values.
Definition: CmdCore.h:116
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
Commands.
Definition: CmdAddOns.h:81
std::string m_strSyntax
parsable command extended usage syntax
Definition: CmdDef.h:230
void setParent(const int nCmdUid)
Set parent&#39;s command id.
Definition: CmdFormDef.cxx:152
Stream I/O manipulators and helpers.
Command line command definition class interface.
Command line argument definition class interface.
Command line core data types.
int numOfForms() const
Get the total number command forms.
Definition: CmdDef.h:184
osManipIndent deltaindent(const long nDelta)
Set relative delta indentation level.
Definition: IOManip.cxx:99
void setIndex(const int nIndex)
Set forms&#39;s index.
Definition: CmdFormDef.cxx:157
RoadNarrows Robotics.
Definition: Camera.h:74
Compiled command form defintion class.
Definition: CmdFormDef.h:91
std::ostream & operator<<(std::ostream &os, const timespec &obj)
A timespec insertion operator.
Input/Output.
Definition: IOManip.h:77
CmdFormDefVec m_formDefs
vector of command forms
Definition: CmdDef.h:233
static CmdFormDef noformdef
constant "no form def" form definition
Definition: CmdDef.cxx:88