tclap 1.2.1

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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 reverved.
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 protected:
72
79 void substituteSpecialChars( std::string& s, char r, std::string& x );
80 void removeChar( std::string& s, char r);
81 void basename( std::string& s );
82
83 void printShortArg(Arg* it);
84 void printLongArg(Arg* it);
85
86 char theDelimiter;
87 };
88
89
90 inline void DocBookOutput::version(CmdLineInterface& _cmd)
91 {
92 std::cout << _cmd.getVersion() << std::endl;
93 }
94
95 inline void DocBookOutput::usage(CmdLineInterface& _cmd )
96 {
97 std::list<Arg*> argList = _cmd.getArgList();
98 std::string progName = _cmd.getProgramName();
99 std::string xversion = _cmd.getVersion();
100 theDelimiter = _cmd.getDelimiter();
101 XorHandler xorHandler = _cmd.getXorHandler();
102 std::vector< std::vector<Arg*> > xorList = xorHandler.getXorList();
103 basename(progName);
104
105 std::cout << "<?xml version='1.0'?>" << std::endl;
106 std::cout << "<!DOCTYPE refentry PUBLIC \"-//OASIS//DTD DocBook XML V4.2//EN\"" << std::endl;
107 std::cout << "\t\"http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd\">" << std::endl << std::endl;
108
109 std::cout << "<refentry>" << std::endl;
110
111 std::cout << "<refmeta>" << std::endl;
112 std::cout << "<refentrytitle>" << progName << "</refentrytitle>" << std::endl;
113 std::cout << "<manvolnum>1</manvolnum>" << std::endl;
114 std::cout << "</refmeta>" << std::endl;
115
116 std::cout << "<refnamediv>" << std::endl;
117 std::cout << "<refname>" << progName << "</refname>" << std::endl;
118 std::cout << "<refpurpose>" << _cmd.getMessage() << "</refpurpose>" << std::endl;
119 std::cout << "</refnamediv>" << std::endl;
120
121 std::cout << "<refsynopsisdiv>" << std::endl;
122 std::cout << "<cmdsynopsis>" << std::endl;
123
124 std::cout << "<command>" << progName << "</command>" << std::endl;
125
126 // xor
127 for ( int i = 0; (unsigned int)i < xorList.size(); i++ )
128 {
129 std::cout << "<group choice='req'>" << std::endl;
130 for ( ArgVectorIterator it = xorList[i].begin();
131 it != xorList[i].end(); it++ )
132 printShortArg((*it));
133
134 std::cout << "</group>" << std::endl;
135 }
136
137 // rest of args
138 for (ArgListIterator it = argList.begin(); it != argList.end(); it++)
139 if ( !xorHandler.contains( (*it) ) )
140 printShortArg((*it));
141
142 std::cout << "</cmdsynopsis>" << std::endl;
143 std::cout << "</refsynopsisdiv>" << std::endl;
144
145 std::cout << "<refsect1>" << std::endl;
146 std::cout << "<title>Description</title>" << std::endl;
147 std::cout << "<para>" << std::endl;
148 std::cout << _cmd.getMessage() << std::endl;
149 std::cout << "</para>" << std::endl;
150 std::cout << "</refsect1>" << std::endl;
151
152 std::cout << "<refsect1>" << std::endl;
153 std::cout << "<title>Options</title>" << std::endl;
154
155 std::cout << "<variablelist>" << std::endl;
156
157 for (ArgListIterator it = argList.begin(); it != argList.end(); it++)
158 printLongArg((*it));
159
160 std::cout << "</variablelist>" << std::endl;
161 std::cout << "</refsect1>" << std::endl;
162
163 std::cout << "<refsect1>" << std::endl;
164 std::cout << "<title>Version</title>" << std::endl;
165 std::cout << "<para>" << std::endl;
166 std::cout << xversion << std::endl;
167 std::cout << "</para>" << std::endl;
168 std::cout << "</refsect1>" << std::endl;
169
170 std::cout << "</refentry>" << std::endl;
171
172 }
173
174 inline void DocBookOutput::failure( CmdLineInterface& _cmd,
175 ArgException& e )
176 {
177 static_cast<void>(_cmd); // unused
178 std::cout << e.what() << std::endl;
179 throw ExitException(1);
180 }
181
182 inline void DocBookOutput::substituteSpecialChars( std::string& s,
183 char r,
184 std::string& x )
185 {
186 size_t p;
187 while ( (p = s.find_first_of(r)) != std::string::npos )
188 {
189 s.erase(p,1);
190 s.insert(p,x);
191 }
192 }
193
194 inline void DocBookOutput::removeChar( std::string& s, char r)
195 {
196 size_t p;
197 while ( (p = s.find_first_of(r)) != std::string::npos )
198 {
199 s.erase(p,1);
200 }
201 }
202
203 inline void DocBookOutput::basename( std::string& s )
204 {
205 size_t p = s.find_last_of('/');
206 if ( p != std::string::npos )
207 {
208 s.erase(0, p + 1);
209 }
210 }
211
212 inline void DocBookOutput::printShortArg(Arg* a)
213 {
214 std::string lt = "&lt;";
215 std::string gt = "&gt;";
216
217 std::string id = a->shortID();
218 substituteSpecialChars(id,'<',lt);
219 substituteSpecialChars(id,'>',gt);
220 removeChar(id,'[');
221 removeChar(id,']');
222
223 std::string choice = "opt";
224 if ( a->isRequired() )
225 choice = "plain";
226
227 std::cout << "<arg choice='" << choice << '\'';
228 if ( a->acceptsMultipleValues() )
229 std::cout << " rep='repeat'";
230
231
232 std::cout << '>';
233 if ( !a->getFlag().empty() )
234 std::cout << a->flagStartChar() << a->getFlag();
235 else
236 std::cout << a->nameStartString() << a->getName();
237 if ( a->isValueRequired() )
238 {
239 std::string arg = a->shortID();
240 removeChar(arg,'[');
241 removeChar(arg,']');
242 removeChar(arg,'<');
243 removeChar(arg,'>');
244 arg.erase(0, arg.find_last_of(theDelimiter) + 1);
245 std::cout << theDelimiter;
246 std::cout << "<replaceable>" << arg << "</replaceable>";
247 }
248 std::cout << "</arg>" << std::endl;
249
250 }
251
252 inline void DocBookOutput::printLongArg(Arg* a)
253 {
254 std::string lt = "&lt;";
255 std::string gt = "&gt;";
256
257 std::string desc = a->getDescription();
258 substituteSpecialChars(desc,'<',lt);
259 substituteSpecialChars(desc,'>',gt);
260
261 std::cout << "<varlistentry>" << std::endl;
262
263 if ( !a->getFlag().empty() )
264 {
265 std::cout << "<term>" << std::endl;
266 std::cout << "<option>";
267 std::cout << a->flagStartChar() << a->getFlag();
268 std::cout << "</option>" << std::endl;
269 std::cout << "</term>" << std::endl;
270 }
271
272 std::cout << "<term>" << std::endl;
273 std::cout << "<option>";
274 std::cout << a->nameStartString() << a->getName();
275 if ( a->isValueRequired() )
276 {
277 std::string arg = a->shortID();
278 removeChar(arg,'[');
279 removeChar(arg,']');
280 removeChar(arg,'<');
281 removeChar(arg,'>');
282 arg.erase(0, arg.find_last_of(theDelimiter) + 1);
283 std::cout << theDelimiter;
284 std::cout << "<replaceable>" << arg << "</replaceable>";
285 }
286 std::cout << "</option>" << std::endl;
287 std::cout << "</term>" << std::endl;
288
289 std::cout << "<listitem>" << std::endl;
290 std::cout << "<para>" << std::endl;
291 std::cout << desc << std::endl;
292 std::cout << "</para>" << std::endl;
293 std::cout << "</listitem>" << std::endl;
294
295 std::cout << "</varlistentry>" << std::endl;
296 }
297
298 } //namespace TCLAP
299 #endif
TCLAP::XorHandler::getXorList
std::vector< std::vector< Arg * > > & getXorList()
Definition: XorHandler.h:153
TCLAP::Arg
A virtual base class that defines the essential data for all arguments.
Definition: Arg.h:64
TCLAP::ArgException::what
const char * what() const
Returns the arg id and error text.
Definition: ArgException.h:80
TCLAP::ArgException
A simple class that defines and argument exception.
Definition: ArgException.h:36
TCLAP::DocBookOutput::usage
virtual void usage(CmdLineInterface &c)
Prints the usage to stdout.
Definition: DocBookOutput.h:95
TCLAP::CmdLineInterface::getProgramName
virtual std::string & getProgramName()=0
Returns the program name string.
TCLAP::DocBookOutput::theDelimiter
char theDelimiter
Definition: DocBookOutput.h:86
CmdLineOutput.h
TCLAP::DocBookOutput::printShortArg
void printShortArg(Arg *it)
Definition: DocBookOutput.h:212
TCLAP::ExitException
Definition: ArgException.h:187
TCLAP::CmdLineInterface::getArgList
virtual std::list< Arg * > & getArgList()=0
Returns the argList.
XorHandler.h
TCLAP::CmdLineInterface::getDelimiter
virtual char getDelimiter()=0
Returns the delimiter string.
CmdLineInterface.h
TCLAP::CmdLineInterface::getVersion
virtual std::string & getVersion()=0
Returns the version string.
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:182
TCLAP::CmdLineInterface::getXorHandler
virtual XorHandler & getXorHandler()=0
Returns the XorHandler.
TCLAP::Arg::shortID
virtual std::string shortID(const std::string &valueId="val") const
Returns a short ID for the usage.
Definition: Arg.h:505
TCLAP::ArgListIterator
std::list< Arg * >::iterator ArgListIterator
Typedef of an Arg list iterator.
Definition: Arg.h:396
TCLAP::CmdLineInterface
The base class that manages the command line definition and passes along the parsing to the appropria...
Definition: CmdLineInterface.h:43
TCLAP::Arg::acceptsMultipleValues
virtual bool acceptsMultipleValues()
Use by output classes to determine whether an Arg accepts multiple values.
Definition: Arg.h:674
TCLAP::XorHandler::contains
bool contains(const Arg *a)
Simply checks whether the Arg is contained in one of the arg lists.
Definition: XorHandler.h:141
TCLAP::ArgVectorIterator
std::vector< Arg * >::iterator ArgVectorIterator
Typedef of an Arg vector iterator.
Definition: Arg.h:401
Arg.h
TCLAP::DocBookOutput::printLongArg
void printLongArg(Arg *it)
Definition: DocBookOutput.h:252
TCLAP::CmdLineInterface::getMessage
virtual std::string & getMessage()=0
Returns the message string.
TCLAP::DocBookOutput::version
virtual void version(CmdLineInterface &c)
Prints the version to stdout.
Definition: DocBookOutput.h:90
TCLAP::DocBookOutput::basename
void basename(std::string &s)
Definition: DocBookOutput.h:203
TCLAP
Definition: Arg.h:57
TCLAP::DocBookOutput::removeChar
void removeChar(std::string &s, char r)
Definition: DocBookOutput.h:194
TCLAP::DocBookOutput
A class that generates DocBook output for usage() method for the given CmdLine and its Args...
Definition: DocBookOutput.h:43
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:174
TCLAP::XorHandler
This class handles lists of Arg's that are to be XOR'd on the command line.
Definition: XorHandler.h:38
TCLAP::CmdLineOutput
The interface that any output object must implement.
Definition: CmdLineOutput.h:41
TCLAP::Arg::isRequired
virtual bool isRequired() const
Indicates whether the argument is required.
Definition: Arg.h:571