OGS
TCLAPCustomOutput.h
Go to the documentation of this file.
1 
10 #pragma once
11 
12 #include <tclap/Arg.h>
13 #include <tclap/CmdLineInterface.h>
14 #include <tclap/CmdLineOutput.h>
15 #include <tclap/StdOutput.h>
16 #include <tclap/XorHandler.h>
17 
18 #include <algorithm>
19 #include <iosfwd>
20 #include <list>
21 #include <string>
22 #include <vector>
23 
24 namespace BaseLib
25 {
30 class TCLAPCustomOutput : public TCLAP::StdOutput
31 {
32 public:
38  virtual void usage(TCLAP::CmdLineInterface& cmd_);
39 
46  virtual void failure(TCLAP::CmdLineInterface& cmd_, TCLAP::ArgException& e);
47 
48 protected:
54  void shortUsage_(TCLAP::CmdLineInterface& cmd_, std::ostream& os) const;
55 
62  void longUsage_(TCLAP::CmdLineInterface& cmd_, std::ostream& os) const;
63 };
64 
65 inline void TCLAPCustomOutput::usage(TCLAP::CmdLineInterface& cmd_)
66 {
67  std::cout << std::endl << "USAGE: " << std::endl << std::endl;
68 
69  shortUsage_(cmd_, std::cout);
70 
71  std::cout << std::endl << std::endl << "Where: " << std::endl << std::endl;
72 
73  longUsage_(cmd_, std::cout);
74 
75  std::cout << std::endl;
76 }
77 
78 inline void TCLAPCustomOutput::failure(TCLAP::CmdLineInterface& cmd_,
79  TCLAP::ArgException& e)
80 {
81  std::string progName = cmd_.getProgramName();
82 
83  std::cerr << "PARSE ERROR: " << e.argId() << std::endl
84  << " " << e.error() << std::endl
85  << std::endl;
86 
87  if (cmd_.hasHelpAndVersion())
88  {
89  std::cerr << "Brief USAGE: " << std::endl;
90 
91  shortUsage_(cmd_, std::cerr);
92 
93  std::cerr << std::endl
94  << "For complete USAGE and HELP type: " << std::endl
95  << " " << progName << " --help" << std::endl
96  << std::endl;
97  }
98  else
99  {
100  usage(cmd_);
101  }
102 
103  throw TCLAP::ExitException(1);
104 }
105 
106 inline void TCLAPCustomOutput::shortUsage_(TCLAP::CmdLineInterface& cmd_,
107  std::ostream& os) const
108 {
109  std::list<TCLAP::Arg*> argList = cmd_.getArgList();
110  std::string progName = cmd_.getProgramName();
111  TCLAP::XorHandler xorHandler = cmd_.getXorHandler();
112  std::vector<std::vector<TCLAP::Arg*>> xorList = xorHandler.getXorList();
113 
114  std::string s = progName + " ";
115 
116  // first the xor
117  for (int i = 0; static_cast<unsigned int>(i) < xorList.size(); i++)
118  {
119  s += " {";
120  for (TCLAP::ArgVectorIterator it = xorList[i].begin();
121  it != xorList[i].end();
122  it++)
123  {
124  s += (*it)->shortID() + "|";
125  }
126 
127  s[s.length() - 1] = '}';
128  }
129 
130  // then the rest
131  for (auto it = argList.rbegin(); it != argList.rend(); ++it)
132  { // here modified
133  if (!xorHandler.contains((*it)))
134  {
135  s += " " + (*it)->shortID();
136  }
137  }
138 
139  // if the program name is too long, then adjust the second line offset
140  int secondLineOffset = static_cast<int>(progName.length()) + 2;
141  if (secondLineOffset > 75 / 2)
142  {
143  secondLineOffset = static_cast<int>(75 / 2);
144  }
145 
146  spacePrint(os, s, 75, 3, secondLineOffset);
147 }
148 
149 inline void TCLAPCustomOutput::longUsage_(TCLAP::CmdLineInterface& cmd_,
150  std::ostream& os) const
151 {
152  std::list<TCLAP::Arg*> argList = cmd_.getArgList();
153  std::string message = cmd_.getMessage();
154  TCLAP::XorHandler xorHandler = cmd_.getXorHandler();
155  std::vector<std::vector<TCLAP::Arg*>> xorList = xorHandler.getXorList();
156 
157  // first the xor
158  for (int i = 0; static_cast<unsigned int>(i) < xorList.size(); i++)
159  {
160  for (TCLAP::ArgVectorIterator it = xorList[i].begin();
161  it != xorList[i].end();
162  it++)
163  {
164  this->spacePrint(os, (*it)->longID(), 75, 3, 3);
165  spacePrint(os, (*it)->getDescription(), 75, 5, 0);
166 
167  if (it + 1 != xorList[i].end())
168  {
169  spacePrint(os, "-- OR --", 75, 9, 0);
170  }
171  }
172  os << std::endl << std::endl;
173  }
174 
175  // then the rest
176  for (auto it = argList.rbegin(); it != argList.rend(); it++)
177  { // here modified
178  if (!xorHandler.contains((*it)))
179  {
180  spacePrint(os, (*it)->longID(), 75, 3, 3);
181  spacePrint(os, (*it)->getDescription(), 75, 5, 0);
182  os << std::endl;
183  }
184  }
185 
186  os << std::endl;
187 
188  spacePrint(os, message, 75, 3, 0);
189 }
190 
191 } // namespace BaseLib
virtual void usage(TCLAP::CmdLineInterface &cmd_)
void longUsage_(TCLAP::CmdLineInterface &cmd_, std::ostream &os) const
virtual void failure(TCLAP::CmdLineInterface &cmd_, TCLAP::ArgException &e)
void shortUsage_(TCLAP::CmdLineInterface &cmd_, std::ostream &os) const