fix8  version 1.4.0
Open Source C++ FIX Framework
usage.hpp
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------------------
2 /*
3 
4 Fix8 is released under the GNU LESSER GENERAL PUBLIC LICENSE Version 3.
5 
6 Fix8 Open Source FIX Engine.
7 Copyright (C) 2010-16 David L. Dight <fix@fix8.org>
8 
9 Fix8 is free software: you can redistribute it and / or modify it under the terms of the
10 GNU Lesser General Public License as published by the Free Software Foundation, either
11 version 3 of the License, or (at your option) any later version.
12 
13 Fix8 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
14 even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 
16 You should have received a copy of the GNU Lesser General Public License along with Fix8.
17 If not, see <http://www.gnu.org/licenses/>.
18 
19 BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO
20 THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE
21 COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY
22 KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO
24 THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE,
25 YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
26 
27 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT
28 HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED
29 ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
30 CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT
31 NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR
32 THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH
33 HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
34 
35 */
36 //-----------------------------------------------------------------------------------------
37 #ifndef _USAGE_HPP_
38 # define _USAGE_HPP_
39 
40 //-----------------------------------------------------------------------------------------
42 class UsageMan
43 {
44  const std::string prognm_, params_;
45  std::string argstr_, description_;
46  using OPTEL = std::map<const char, const std::pair<const std::string, const std::string>>;
48  std::list<std::string> xtrlines_;
49  const int splen_, argoptlen_;
50 
51 public:
58  UsageMan(const std::string& prognm, const std::string& argstr, const std::string& params,
59  int splen=3, int argoptlen=23)
60  : prognm_(prognm), params_(params), argstr_(argstr), splen_(splen), argoptlen_(argoptlen)
61  {
62  std::sort(argstr_.begin(), argstr_.end());
63  }
64 
66  virtual ~UsageMan() {}
67 
73  bool add(const char sw, const std::string& lsw, const std::string& help)
74  {
75  return optels_.insert({sw, std::pair<const std::string, const std::string>(lsw, help)}).second;
76  }
77 
80  void add(const std::string& xtr)
81  {
82  std::string topush(xtr);
83  std::string::size_type spos;
84  while ((spos = topush.find_first_of('@')) != std::string::npos)
85  topush.replace(spos, 1, std::string(splen_, ' '));
86  xtrlines_.push_back(topush);
87  }
88 
91  void setdesc(const std::string& desc) { description_ = desc; }
92 
95  void print(std::ostream& os) const
96  {
97  if (!description_.empty())
98  os << description_ << std::endl << std::endl;
99  os << "Usage: " << prognm_ << " [-";
100  for (std::string::const_iterator itr(argstr_.begin()); itr != argstr_.end(); ++itr)
101  if (*itr != ':')
102  os << *itr;
103  os << "] " << params_ << std::endl;
104  const std::string spacer(splen_, ' ');
105  for (OPTEL::const_iterator itr(optels_.begin()); itr != optels_.end(); ++itr)
106  {
107  std::ostringstream ostr;
108  ostr << '-' << itr->first << ",--" << itr->second.first;
109  os << spacer << std::left << std::setw(argoptlen_) << ostr.str()
110  << ' ' << itr->second.second << std::endl;
111  }
112  for (std::list<std::string>::const_iterator itr(xtrlines_.begin()); itr != xtrlines_.end(); ++itr)
113  os << *itr << std::endl;
114  }
115 };
116 
117 #endif // _USAGE_HPP_
118 
UsageMan(const std::string &prognm, const std::string &argstr, const std::string &params, int splen=3, int argoptlen=23)
Definition: usage.hpp:58
virtual ~UsageMan()
Dtor.
Definition: usage.hpp:66
std::string argstr_
Definition: usage.hpp:45
bool add(const char sw, const std::string &lsw, const std::string &help)
Definition: usage.hpp:73
void add(const std::string &xtr)
Definition: usage.hpp:80
std::list< std::string > xtrlines_
Definition: usage.hpp:48
const int splen_
Definition: usage.hpp:49
OPTEL optels_
Definition: usage.hpp:47
const std::string prognm_
Definition: usage.hpp:44
string spacer
Definition: f8c.cpp:96
void setdesc(const std::string &desc)
Definition: usage.hpp:91
void print(std::ostream &os) const
Definition: usage.hpp:95
const int argoptlen_
Definition: usage.hpp:49
std::string description_
Definition: usage.hpp:45
const std::string params_
Definition: usage.hpp:44
Convenient program help/usage wrapper. Generates a standardised usage message.
Definition: usage.hpp:42
std::map< const char, const std::pair< const std::string, const std::string >> OPTEL
Definition: usage.hpp:46