std_output.hpp
Go to the documentation of this file.
1 
11 /*****************************************************************************
12 ** Ifdefs
13 *****************************************************************************/
14 
15 #ifndef TCLAP_STDCMDLINEOUTPUT_H
16 #define TCLAP_STDCMDLINEOUTPUT_H
17 
18 #include <string>
19 #include <vector>
20 #include <list>
21 #include <iostream>
22 #include <algorithm>
23 #include <cmath>
24 
25 #include "cmd_line_interface.hpp"
26 #include "cmd_line_output.hpp"
27 #include "xor_handler.hpp"
28 #include "arg.hpp"
29 
30 namespace ecl {
31 
38 class StdOutput : public CmdLineOutput
39 {
40 
41  public:
42 
48  virtual void usage(CmdLineInterface& c);
49 
55  virtual void version(CmdLineInterface& c);
56 
63  virtual void failure(CmdLineInterface& c,
64  ArgException& e );
65 
66  protected:
67 
73  void _shortUsage( CmdLineInterface& c, std::ostream& os ) const;
74 
81  void _longUsage( CmdLineInterface& c, std::ostream& os ) const;
82 
94  void spacePrint( std::ostream& os,
95  const std::string& s,
96  int maxWidth,
97  int indentSpaces,
98  int secondLineOffset ) const;
99 
100 };
101 
102 
104 {
105  std::string progName = _cmd.getProgramName();
106  std::string interface_version = _cmd.getVersion();
107 
108  std::cout << std::endl << progName << " version: "
109  << interface_version << std::endl << std::endl;
110 }
111 
113 {
114  std::cout << std::endl << "USAGE: " << std::endl << std::endl;
115 
116  _shortUsage( _cmd, std::cout );
117 
118  std::cout << std::endl << std::endl << "Where: " << std::endl << std::endl;
119 
120  _longUsage( _cmd, std::cout );
121 
122  std::cout << std::endl;
123 
124 }
125 
127  ArgException& e )
128 {
129  std::string progName = _cmd.getProgramName();
130 
131  std::cerr << "PARSE ERROR: " << e.argId() << std::endl
132  << " " << e.error() << std::endl << std::endl;
133 
134  if ( _cmd.hasHelpAndVersion() )
135  {
136  std::cerr << "Brief USAGE: " << std::endl;
137 
138  _shortUsage( _cmd, std::cerr );
139 
140  std::cerr << std::endl << "For complete USAGE and HELP type: "
141  << std::endl << " " << progName << " --help"
142  << std::endl << std::endl;
143  }
144  else
145  usage(_cmd);
146 
147 }
148 
150  std::ostream& os ) const
151 {
152  std::list<Arg*> argList = _cmd.getArgList();
153  std::string progName = _cmd.getProgramName();
154  XorHandler xorHandler = _cmd.getXorHandler();
155  std::vector< std::vector<Arg*> > xorList = xorHandler.getXorList();
156 
157  std::string s = progName + " ";
158 
159  // first the xor
160  for ( int i = 0; static_cast<unsigned int>(i) < xorList.size(); i++ )
161  {
162  s += " {";
163  for ( ArgVectorIterator it = xorList[i].begin();
164  it != xorList[i].end(); it++ )
165  s += (*it)->shortID() + "|";
166 
167  s[s.length()-1] = '}';
168  }
169 
170  // then the rest
171  for (ArgListIterator it = argList.begin(); it != argList.end(); it++)
172  if ( !xorHandler.contains( (*it) ) )
173  s += " " + (*it)->shortID();
174 
175  // if the program name is too long, then adjust the second line offset
176  int secondLineOffset = static_cast<int>(progName.length()) + 2;
177  if ( secondLineOffset > 75/2 )
178  secondLineOffset = static_cast<int>(75/2);
179 
180  spacePrint( os, s, 75, 3, secondLineOffset );
181  //spacePrint( std::cout, s, 75, 3, secondLineOffset );
182 }
183 
185  std::ostream& os ) const
186 {
187  std::list<Arg*> argList = _cmd.getArgList();
188  std::string message = _cmd.getMessage();
189  XorHandler xorHandler = _cmd.getXorHandler();
190  std::vector< std::vector<Arg*> > xorList = xorHandler.getXorList();
191 
192  // first the xor
193  for ( int i = 0; static_cast<unsigned int>(i) < xorList.size(); i++ )
194  {
195  for ( ArgVectorIterator it = xorList[i].begin();
196  it != xorList[i].end();
197  it++ )
198  {
199  spacePrint( os, (*it)->longID(), 75, 3, 3 );
200  spacePrint( os, (*it)->getDescription(), 75, 5, 0 );
201 
202  if ( it+1 != xorList[i].end() )
203  spacePrint(os, "-- OR --", 75, 9, 0);
204  }
205  os << std::endl << std::endl;
206  }
207 
208  // then the rest
209  for (ArgListIterator it = argList.begin(); it != argList.end(); it++)
210  if ( !xorHandler.contains( (*it) ) )
211  {
212  spacePrint( os, (*it)->longID(), 75, 3, 3 );
213  spacePrint( os, (*it)->getDescription(), 75, 5, 0 );
214  os << std::endl;
215  }
216 
217  os << std::endl;
218 
219  spacePrint( os, message, 75, 3, 0 );
220 }
221 
222 inline void StdOutput::spacePrint( std::ostream& os,
223  const std::string& s,
224  int maxWidth,
225  int indentSpaces,
226  int secondLineOffset ) const
227 {
228  int len = static_cast<int>(s.length());
229 
230  if ( (len + indentSpaces > maxWidth) && maxWidth > 0 )
231  {
232  int allowedLen = maxWidth - indentSpaces;
233  int start = 0;
234  while ( start < len )
235  {
236  // find the substring length
237  int stringLen = std::min<int>( len - start, allowedLen );
238 
239  // trim the length so it doesn't end in middle of a word
240  if ( stringLen == allowedLen )
241  while ( s[stringLen+start] != ' ' &&
242  s[stringLen+start] != ',' &&
243  s[stringLen+start] != '|' &&
244  stringLen >= 0 )
245  stringLen--;
246 
247  // ok, the word is longer than the line, so just split
248  // wherever the line ends
249  if ( stringLen <= 0 )
250  stringLen = allowedLen;
251 
252  // check for newlines
253  for ( int i = 0; i < stringLen; i++ )
254  if ( s[start+i] == '\n' )
255  stringLen = i+1;
256 
257  // print the indent
258  for ( int i = 0; i < indentSpaces; i++ )
259  os << " ";
260 
261  if ( start == 0 )
262  {
263  // handle second line offsets
264  indentSpaces += secondLineOffset;
265 
266  // adjust allowed len
267  allowedLen -= secondLineOffset;
268  }
269 
270  os << s.substr(start,stringLen) << std::endl;
271 
272  // so we don't start a line with a space
273  while ( s[stringLen+start] == ' ' && start < len )
274  start++;
275 
276  start += stringLen;
277  }
278  }
279  else
280  {
281  for ( int i = 0; i < indentSpaces; i++ )
282  os << " ";
283  os << s << std::endl;
284  }
285 }
286 }; // namespace ecl
287 
288 #endif
TCLAP command line argument parser classes.
void spacePrint(std::ostream &os, const std::string &s, int maxWidth, int indentSpaces, int secondLineOffset) const
Definition: std_output.hpp:222
virtual void usage(CmdLineInterface &c)
Definition: std_output.hpp:112
void _longUsage(CmdLineInterface &c, std::ostream &os) const
Definition: std_output.hpp:184
virtual std::string & getVersion()=0
Tclap class indirectly used by children for standardising outputs.
Managing interface for The base class that manages the command line definition and passes along the p...
std::list< Arg * >::iterator ArgListIterator
Definition: arg.hpp:336
virtual std::list< Arg * > & getArgList()=0
void _shortUsage(CmdLineInterface &c, std::ostream &os) const
Definition: std_output.hpp:149
std::vector< std::vector< Arg * > > & getXorList()
Defines the exception that is thrown whenever a command line is created and parsed.
virtual XorHandler & getXorHandler()=0
std::vector< Arg * >::iterator ArgVectorIterator
Definition: arg.hpp:341
virtual std::string & getMessage()=0
TCLAP command line argument parser classes.
virtual bool hasHelpAndVersion()=0
TCLAP command line argument parser classes.
TClap class indirectly used by CmdLine for handling xor&#39;d arguments.
Definition: xor_handler.hpp:32
bool contains(const Arg *a)
TCLAP command line argument parser classes.
std::string argId() const
virtual void version(CmdLineInterface &c)
Definition: std_output.hpp:103
std::string error() const
virtual void failure(CmdLineInterface &c, ArgException &e)
Definition: std_output.hpp:126
virtual std::string & getProgramName()=0
TClap class indirectly used to handle CmdLine arguments.
Definition: std_output.hpp:38


ecl_command_line
Author(s): Daniel Stonier
autogenerated on Mon Jun 10 2019 13:08:08