tclap  1.2.5
DocBookOutput.h
Go to the documentation of this file.
1 // -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
2 
3 /******************************************************************************
4  *
5  * file: DocBookOutput.h
6  *
7  * Copyright (c) 2004, Michael E. Smoot
8  * Copyright (c) 2017, Google LLC
9  * All rights reserved.
10  *
11  * See the file COPYING in the top directory of this distribution for
12  * more information.
13  *
14  * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
15  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  *
22  *****************************************************************************/
23 
24 #ifndef TCLAP_DOCBOOKOUTPUT_H
25 #define TCLAP_DOCBOOKOUTPUT_H
26 
27 #include <string>
28 #include <vector>
29 #include <list>
30 #include <iostream>
31 #include <algorithm>
32 
33 #include <tclap/CmdLineInterface.h>
34 #include <tclap/CmdLineOutput.h>
35 #include <tclap/XorHandler.h>
36 #include <tclap/Arg.h>
37 
38 namespace TCLAP {
39 
45 {
46 
47  public:
48 
54  virtual void usage(CmdLineInterface& c);
55 
61  virtual void version(CmdLineInterface& c);
62 
69  virtual void failure(CmdLineInterface& c,
70  ArgException& e );
71 
73  protected:
74 
81  void substituteSpecialChars( std::string& s, char r, std::string& x );
82  void removeChar( std::string& s, char r);
83  void basename( std::string& s );
84 
85  void printShortArg(Arg* it);
86  void printLongArg(Arg* it);
87 
89 };
90 
91 
93 {
94  std::cout << _cmd.getVersion() << std::endl;
95 }
96 
98 {
99  std::list<Arg*> argList = _cmd.getArgList();
100  std::string progName = _cmd.getProgramName();
101  std::string xversion = _cmd.getVersion();
102  theDelimiter = _cmd.getDelimiter();
103  XorHandler xorHandler = _cmd.getXorHandler();
104  const std::vector< std::vector<Arg*> > xorList = xorHandler.getXorList();
105  basename(progName);
106 
107  std::cout << "<?xml version='1.0'?>" << std::endl;
108  std::cout << "<!DOCTYPE refentry PUBLIC \"-//OASIS//DTD DocBook XML V4.2//EN\"" << std::endl;
109  std::cout << "\t\"http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd\">" << std::endl << std::endl;
110 
111  std::cout << "<refentry>" << std::endl;
112 
113  std::cout << "<refmeta>" << std::endl;
114  std::cout << "<refentrytitle>" << progName << "</refentrytitle>" << std::endl;
115  std::cout << "<manvolnum>1</manvolnum>" << std::endl;
116  std::cout << "</refmeta>" << std::endl;
117 
118  std::cout << "<refnamediv>" << std::endl;
119  std::cout << "<refname>" << progName << "</refname>" << std::endl;
120  std::cout << "<refpurpose>" << _cmd.getMessage() << "</refpurpose>" << std::endl;
121  std::cout << "</refnamediv>" << std::endl;
122 
123  std::cout << "<refsynopsisdiv>" << std::endl;
124  std::cout << "<cmdsynopsis>" << std::endl;
125 
126  std::cout << "<command>" << progName << "</command>" << std::endl;
127 
128  // xor
129  for ( int i = 0; (unsigned int)i < xorList.size(); i++ )
130  {
131  std::cout << "<group choice='req'>" << std::endl;
132  for ( ArgVectorIterator it = xorList[i].begin();
133  it != xorList[i].end(); it++ )
134  printShortArg((*it));
135 
136  std::cout << "</group>" << std::endl;
137  }
138 
139  // rest of args
140  for (ArgListIterator it = argList.begin(); it != argList.end(); it++)
141  if ( !xorHandler.contains( (*it) ) )
142  printShortArg((*it));
143 
144  std::cout << "</cmdsynopsis>" << std::endl;
145  std::cout << "</refsynopsisdiv>" << std::endl;
146 
147  std::cout << "<refsect1>" << std::endl;
148  std::cout << "<title>Description</title>" << std::endl;
149  std::cout << "<para>" << std::endl;
150  std::cout << _cmd.getMessage() << std::endl;
151  std::cout << "</para>" << std::endl;
152  std::cout << "</refsect1>" << std::endl;
153 
154  std::cout << "<refsect1>" << std::endl;
155  std::cout << "<title>Options</title>" << std::endl;
156 
157  std::cout << "<variablelist>" << std::endl;
158 
159  for (ArgListIterator it = argList.begin(); it != argList.end(); it++)
160  printLongArg((*it));
161 
162  std::cout << "</variablelist>" << std::endl;
163  std::cout << "</refsect1>" << std::endl;
164 
165  std::cout << "<refsect1>" << std::endl;
166  std::cout << "<title>Version</title>" << std::endl;
167  std::cout << "<para>" << std::endl;
168  std::cout << xversion << std::endl;
169  std::cout << "</para>" << std::endl;
170  std::cout << "</refsect1>" << std::endl;
171 
172  std::cout << "</refentry>" << std::endl;
173 
174 }
175 
177  ArgException& e )
178 {
179  static_cast<void>(_cmd); // unused
180  std::cout << e.what() << std::endl;
181  throw ExitException(1);
182 }
183 
184 inline void DocBookOutput::substituteSpecialChars( std::string& s,
185  char r,
186  std::string& x )
187 {
188  size_t p;
189  while ( (p = s.find_first_of(r)) != std::string::npos )
190  {
191  s.erase(p,1);
192  s.insert(p,x);
193  }
194 }
195 
196 inline void DocBookOutput::removeChar( std::string& s, char r)
197 {
198  size_t p;
199  while ( (p = s.find_first_of(r)) != std::string::npos )
200  {
201  s.erase(p,1);
202  }
203 }
204 
205 inline void DocBookOutput::basename( std::string& s )
206 {
207  size_t p = s.find_last_of('/');
208  if ( p != std::string::npos )
209  {
210  s.erase(0, p + 1);
211  }
212 }
213 
215 {
216  std::string lt = "&lt;";
217  std::string gt = "&gt;";
218 
219  std::string id = a->shortID();
220  substituteSpecialChars(id,'<',lt);
221  substituteSpecialChars(id,'>',gt);
222  removeChar(id,'[');
223  removeChar(id,']');
224 
225  std::string choice = "opt";
226  if ( a->isRequired() )
227  choice = "plain";
228 
229  std::cout << "<arg choice='" << choice << '\'';
230  if ( a->acceptsMultipleValues() )
231  std::cout << " rep='repeat'";
232 
233 
234  std::cout << '>';
235  if ( !a->getFlag().empty() )
236  std::cout << a->flagStartChar() << a->getFlag();
237  else
238  std::cout << a->nameStartString() << a->getName();
239  if ( a->isValueRequired() )
240  {
241  std::string arg = a->shortID();
242  removeChar(arg,'[');
243  removeChar(arg,']');
244  removeChar(arg,'<');
245  removeChar(arg,'>');
246  removeChar(arg,'.');
247  arg.erase(0, arg.find_last_of(theDelimiter) + 1);
248  std::cout << theDelimiter;
249  std::cout << "<replaceable>" << arg << "</replaceable>";
250  }
251  std::cout << "</arg>" << std::endl;
252 
253 }
254 
255 inline void DocBookOutput::printLongArg(Arg* a)
256 {
257  std::string lt = "&lt;";
258  std::string gt = "&gt;";
259 
260  std::string desc = a->getDescription();
261  substituteSpecialChars(desc,'<',lt);
262  substituteSpecialChars(desc,'>',gt);
263 
264  std::cout << "<varlistentry>" << std::endl;
265 
266  if ( !a->getFlag().empty() )
267  {
268  std::cout << "<term>" << std::endl;
269  std::cout << "<option>";
270  std::cout << a->flagStartChar() << a->getFlag();
271  std::cout << "</option>" << std::endl;
272  std::cout << "</term>" << std::endl;
273  }
274 
275  std::cout << "<term>" << std::endl;
276  std::cout << "<option>";
277  std::cout << a->nameStartString() << a->getName();
278  if ( a->isValueRequired() )
279  {
280  std::string arg = a->shortID();
281  removeChar(arg,'[');
282  removeChar(arg,']');
283  removeChar(arg,'<');
284  removeChar(arg,'>');
285  removeChar(arg,'.');
286  arg.erase(0, arg.find_last_of(theDelimiter) + 1);
287  std::cout << theDelimiter;
288  std::cout << "<replaceable>" << arg << "</replaceable>";
289  }
290  std::cout << "</option>" << std::endl;
291  std::cout << "</term>" << std::endl;
292 
293  std::cout << "<listitem>" << std::endl;
294  std::cout << "<para>" << std::endl;
295  std::cout << desc << std::endl;
296  std::cout << "</para>" << std::endl;
297  std::cout << "</listitem>" << std::endl;
298 
299  std::cout << "</varlistentry>" << std::endl;
300 }
301 
302 } //namespace TCLAP
303 #endif
A simple class that defines and argument exception.
Definition: ArgException.h:38
const char * what() const
Returns the arg id and error text.
Definition: ArgException.h:81
A virtual base class that defines the essential data for all arguments.
Definition: Arg.h:56
virtual bool acceptsMultipleValues()
Use by output classes to determine whether an Arg accepts multiple values.
Definition: Arg.h:665
virtual bool isRequired() const
Indicates whether the argument is required.
Definition: Arg.h:562
virtual std::string shortID(const std::string &valueId="val") const
Returns a short ID for the usage.
Definition: Arg.h:496
The base class that manages the command line definition and passes along the parsing to the appropria...
virtual std::string & getVersion()=0
Returns the version string.
virtual XorHandler & getXorHandler()=0
Returns the XorHandler.
virtual std::string & getProgramName()=0
Returns the program name string.
virtual std::string & getMessage()=0
Returns the message string.
virtual std::list< Arg * > & getArgList()=0
Returns the argList.
virtual char getDelimiter()=0
Returns the delimiter string.
The interface that any output object must implement.
Definition: CmdLineOutput.h:45
A class that generates DocBook output for usage() method for the given CmdLine and its Args.
Definition: DocBookOutput.h:45
virtual void version(CmdLineInterface &c)
Prints the version to stdout.
Definition: DocBookOutput.h:92
virtual void failure(CmdLineInterface &c, ArgException &e)
Prints (to stderr) an error message, short usage Can be overridden to produce alternative behavior.
void substituteSpecialChars(std::string &s, char r, std::string &x)
Substitutes the char r for string x in string s.
void basename(std::string &s)
void printLongArg(Arg *it)
void printShortArg(Arg *it)
void removeChar(std::string &s, char r)
virtual void usage(CmdLineInterface &c)
Prints the usage to stdout.
Definition: DocBookOutput.h:97
Thrown when TCLAP thinks the program should exit.
Definition: ArgException.h:200
This class handles lists of Arg's that are to be XOR'd on the command line.
Definition: XorHandler.h:41
bool contains(const Arg *a)
Simply checks whether the Arg is contained in one of the arg lists.
Definition: XorHandler.h:143
const std::vector< std::vector< Arg * > > & getXorList() const
Definition: XorHandler.h:155
Definition: Arg.h:48
std::vector< Arg * >::const_iterator ArgVectorIterator
Typedef of an Arg vector iterator.
Definition: Arg.h:392
std::list< Arg * >::const_iterator ArgListIterator
Typedef of an Arg list iterator.
Definition: Arg.h:387