multovl  1.3
Multiple overlaps of genomic regions
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
polite.hh
Go to the documentation of this file.
1 /* <LICENSE>
2 License for the MULTOVL multiple genomic overlap tools
3 
4 Copyright (c) 2007-2012, Dr Andras Aszodi,
5 Campus Science Support Facilities GmbH (CSF),
6 Dr-Bohr-Gasse 3, A-1030 Vienna, Austria, Europe.
7 All rights reserved.
8 
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are
11 met:
12 
13  * Redistributions of source code must retain the above copyright notice,
14  this list of conditions and the following disclaimer.
15  * Redistributions in binary form must reproduce the above copyright notice,
16  this list of conditions and the following disclaimer in the documentation
17  and/or other materials provided with the distribution.
18  * Neither the name of the Campus Science Support Facilities GmbH
19  nor the names of its contributors may be used to endorse
20  or promote products derived from this software without specific prior
21  written permission.
22 
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
24 AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
26 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
27 THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
30 USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
31 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 </LICENSE> */
35 #ifndef MULTOVL_POLITE_HEADER
36 #define MULTOVL_POLITE_HEADER
37 
38 // == HEADER polite.hh ==
39 
47 // -- Standard headers --
48 
49 #include <iostream>
50 #include <sstream>
51 #include <string>
52 #include <vector>
53 
54 // -- Boost headers --
55 
56 #include "boost/program_options.hpp"
57 namespace bpo = boost::program_options;
58 
59 // -- Classes --
60 
61 namespace multovl {
62 
71 class Polite
72 {
73  public:
74 
86  void process_commandline(int argc, char* argv[]);
87 
93  bool parse_check(int argc, char* argv[]);
94 
97  std::vector<std::string> pos_opts() const;
98 
101  bool error_status() const { return _err; }
102 
106  std::string error_messages() const { return _errors.str(); }
107 
110  bool version_needed() const { return _vm.count("version") > 0; }
111 
115  virtual
116  std::ostream& print_version(std::ostream& out) const { return version_info(out); }
117 
121  bool help_needed() const { return _vm.count("help") > 0; }
122 
126  virtual
127  std::ostream& print_help(std::ostream& out) const;
128 
129  virtual ~Polite() {}
130 
131  protected:
132 
139  explicit Polite(const std::string& descr = "Options");
140 
149  template <typename Opttype>
150  void add_option(const std::string& name, Opttype* optvarp, const Opttype& defval,
151  const std::string& descr, const char nm = '\0')
152  {
153  std::string oname = (nm == '\0')? name: name + ',' + nm;
154  _desc.add_options()
155  (oname.c_str(),
156  bpo::value<Opttype>(optvarp),
157  /* NOTE: this used to be
158  * bpo::value<Opttype>(optvarp)->default_value(defval),
159  * but that caused _vm.count("name") > 0
160  * even if the parameter was not present on the commandline
161  */
162  descr.c_str());
163  *optvarp = defval; // init default val here then
164  }
165 
171  void add_bool_switch(const std::string& name, bool* varp,
172  const std::string& descr, const char nm = '\0');
173 
180  template <typename Opttype>
181  void add_mandatory_option(const std::string& name, const std::string& descr, const char nm = '\0')
182  {
183  std::string oname = (nm == '\0')? name: name + ',' + nm;
184  _desc.add_options()
185  (oname.c_str(),
186  bpo::value<Opttype>(),
187  descr.c_str());
188  }
189 
196  bool perform_parsing(int argc, char* argv[]);
197 
226  virtual
227  bool check_variables() = 0;
228 
232  bool option_seen(const std::string& optnm) const { return (_vm.count(optnm) > 0); }
233 
237  template <typename Opttype>
238  Opttype fetch_value(const std::string& optnm) const { return (_vm[optnm].as<Opttype>()); }
239 
244  void add_error(const std::string& msg);
245 
247  void error_reset();
248 
256  virtual
257  std::ostream& version_info(std::ostream& out) const;
258 
259  private:
260 
261  bpo::options_description _desc, _hidden;
262  bpo::positional_options_description _posdesc;
263  bpo::variables_map _vm;
264  bool _err;
265  std::ostringstream _errors;
266  bool _parsed; // apparently you may parse only once
267 };
268 
269 } // namespace multovl
270 
271 #endif // MULTOVL_POLITE_HEADER
virtual std::ostream & version_info(std::ostream &out) const
void add_bool_switch(const std::string &name, bool *varp, const std::string &descr, const char nm= '\0')
Opttype fetch_value(const std::string &optnm) const
Definition: polite.hh:238
void add_option(const std::string &name, Opttype *optvarp, const Opttype &defval, const std::string &descr, const char nm= '\0')
Definition: polite.hh:150
void error_reset()
Reset the error status to "no error".
bool version_needed() const
Definition: polite.hh:110
virtual std::ostream & print_version(std::ostream &out) const
Definition: polite.hh:116
virtual std::ostream & print_help(std::ostream &out) const
virtual bool check_variables()=0
bool option_seen(const std::string &optnm) const
Definition: polite.hh:232
bool help_needed() const
Definition: polite.hh:121
std::vector< std::string > pos_opts() const
Polite(const std::string &descr="Options")
void add_error(const std::string &msg)
bool parse_check(int argc, char *argv[])
bool perform_parsing(int argc, char *argv[])
void add_mandatory_option(const std::string &name, const std::string &descr, const char nm= '\0')
Definition: polite.hh:181
void process_commandline(int argc, char *argv[])
std::string error_messages() const
Definition: polite.hh:106
bool error_status() const
Definition: polite.hh:101
Program Options LITE base class. The Polite class provides a wrapper over boost::program_options to m...
Definition: polite.hh:71