appkit  1.5.1
RoadNarrows Robotics Application Kit
CmdFormDef.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: CmdFormDef.cxx
10 //
11 /*! \file
12  *
13  * \brief Command line command form definition class implementation.
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 <string>
62 #include <vector>
63 
64 #include "rnr/rnrconfig.h"
65 #include "rnr/log.h"
66 
67 #include "rnr/appkit/IOManip.h"
68 #include "rnr/appkit/CmdCore.h"
69 #include "rnr/appkit/CmdArgDef.h"
70 #include "rnr/appkit/CmdFormDef.h"
71 
72 using namespace std;
73 using namespace rnr;
74 using namespace rnr::io;
75 using namespace rnr::cmd;
76 
77 
78 // -----------------------------------------------------------------------------
79 // Support
80 // -----------------------------------------------------------------------------
81 
82 namespace rnr
83 {
84  namespace cmd
85  {
86  static CmdArgDef noargdef; ///< constant "no arg def" argument definition
87 
88  } // namespace cmd
89 } // namespace rnr
90 
91 
92 // -----------------------------------------------------------------------------
93 // CmdFormDef Class
94 // -----------------------------------------------------------------------------
95 
96 CmdFormDef::CmdFormDef()
97 {
98  m_nCmdUid = NoUid;
99  m_nIndex = NoIndex;
100  m_nArgcReq = 0;
101  m_nArgcOpt = 0;
102 }
103 
104 CmdFormDef::CmdFormDef(const string &strSyntax) :
105  m_strSyntax(strSyntax)
106 {
107  m_nCmdUid = NoUid;
108  m_nIndex = NoIndex;
109  m_nArgcReq = 0;
110  m_nArgcOpt = 0;
111 }
112 
113 CmdFormDef::CmdFormDef(const CmdFormDef &src)
114 {
115  m_nCmdUid = src.m_nCmdUid;
116  m_nIndex = src.m_nIndex;
117  m_strSyntax = src.m_strSyntax;
118  m_argDefs = src.m_argDefs;
119  m_nArgcReq = src.m_nArgcReq;
120  m_nArgcOpt = src.m_nArgcOpt;
121 }
122 
123 CmdFormDef::~CmdFormDef()
124 {
125 }
126 
127 CmdFormDef &CmdFormDef::operator=(const CmdFormDef &rhs)
128 {
129  m_nCmdUid = rhs.m_nCmdUid;
130  m_nIndex = rhs.m_nIndex;
131  m_strSyntax = rhs.m_strSyntax;
132  m_argDefs = rhs.m_argDefs;
133  m_nArgcReq = rhs.m_nArgcReq;
134  m_nArgcOpt = rhs.m_nArgcOpt;
135 }
136 
137 bool CmdFormDef::isDefined() const
138 {
139  return (m_nIndex >= 0) &&
140  !m_strSyntax.empty() &&
141  (m_argDefs.size() > 0) &&
142  m_argDefs[0].isDefined();
143 }
144 
145 void CmdFormDef::reset()
146 {
147  m_argDefs.clear();
148  m_nArgcReq = 0;
149  m_nArgcOpt = 0;
150 }
151 
152 void CmdFormDef::setParent(const int nCmdUid)
153 {
154  m_nCmdUid = nCmdUid;
155 }
156 
157 void CmdFormDef::setIndex(const int nIndex)
158 {
159  m_nIndex = nIndex;
160 }
161 
162 void CmdFormDef::setSyntax(const string &strSyntax)
163 {
164  m_strSyntax = strSyntax;
165 }
166 
167 void CmdFormDef::pushArg(CmdArgDef &argdef)
168 {
169  int nIndex = numOfArgs();
170 
171  argdef.setParent(m_nCmdUid, m_nIndex);
172  argdef.setIndex(nIndex);
173  m_argDefs.push_back(argdef);
174 }
175 
176 const CmdArgDef &CmdFormDef::at(const int nIndex) const
177 {
178  if( (nIndex >= 0) && (nIndex < m_argDefs.size()) )
179  {
180  return m_argDefs[nIndex];
181  }
182  else
183  {
184  return noargdef;
185  }
186 }
187 
188 CmdArgDef &CmdFormDef::argAt(const int nIndex)
189 {
190  if( (nIndex >= 0) && (nIndex < m_argDefs.size()) )
191  {
192  return m_argDefs[nIndex];
193  }
194  else
195  {
196  return noargdef;
197  }
198 }
199 
200 CmdArgDef &CmdFormDef::lastArg()
201 {
202  return m_argDefs.empty()? noargdef: m_argDefs.back();
203 }
204 
205 ostream &rnr::cmd::operator<<(ostream &os, const CmdFormDef &formdef)
206 {
207  string sep;
208 
209  os << indent() << "{" << endl;
210 
211  os << deltaindent(2);
212 
213  os << indent() << "cmduid = " << formdef.m_nCmdUid << endl;
214  os << indent() << "index = " << formdef.m_nIndex << endl;
215  os << indent() << "syntax = " << formdef.m_strSyntax << endl;
216  os << indent() << "argc = " << formdef.numOfArgs() << "(total)"
217  << " " << formdef.numOfRequiredArgs() << "(required)"
218  << " " << formdef.numOfOptionalArgs() << "(optional)"
219  << endl;
220 
221  os << indent() << "args[" << formdef.numOfArgs() << "] =" << endl;
222  os << indent() << "{" << endl;
223 
224  os << deltaindent(2);
225 
226  for(size_t i = 0; i < formdef.m_argDefs.size(); ++i)
227  {
228  os << sep << formdef.m_argDefs[i];
229  if( sep.empty() )
230  {
231  sep = ",\n";
232  }
233  }
234  os << endl;
235 
236  os << deltaindent(-2);
237 
238  os << indent() << "}" << endl;
239 
240  os << deltaindent(-2);
241 
242  os << indent() << "}";
243 
244  return os;
245 }
246 
Command line command form definition class interface.
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
int numOfRequiredArgs() const
Get the total number of required arguments.
Definition: CmdFormDef.h:209
osManipIndent indent()
Left indent at current stream indentation level.
Definition: IOManip.cxx:115
Command argument compiled definition class.
Definition: CmdArgDef.h:89
const int NoUid
Special values.
Definition: CmdCore.h:116
void setParent(const int nCmdUid, const int nFormIndex)
Set parent&#39;s command and form id&#39;s.
Definition: CmdArgDef.cxx:179
int m_nIndex
forms index
Definition: CmdFormDef.h:250
int m_nCmdUid
parent command&#39;s unique id
Definition: CmdFormDef.h:249
Commands.
Definition: CmdAddOns.h:81
Stream I/O manipulators and helpers.
const int NoIndex
no index
Definition: CmdCore.h:117
Command line argument definition class interface.
int m_nArgcReq
number of required arguments
Definition: CmdFormDef.h:253
Command line core data types.
static CmdArgDef noargdef
constant "no arg def" argument definition
Definition: CmdFormDef.cxx:86
osManipIndent deltaindent(const long nDelta)
Set relative delta indentation level.
Definition: IOManip.cxx:99
void setIndex(const int nIndex)
Set arguments&#39;s command line index.
Definition: CmdArgDef.cxx:185
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
std::ostream & operator<<(std::ostream &os, const timespec &obj)
A timespec insertion operator.
int m_nArgcOpt
number of optional arguments
Definition: CmdFormDef.h:254
Input/Output.
Definition: IOManip.h:77
int numOfArgs() const
Get the total number of arguments.
Definition: CmdFormDef.h:195