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