My Project
cmdoption.hh
Go to the documentation of this file.
1 /* -*- mia-c++ -*-
2  *
3  * This file is part of MIA - a toolbox for medical image analysis
4  * Copyright (c) Leipzig, Madrid 1999-2017 Gert Wollny
5  *
6  * MIA is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with MIA; if not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 #ifndef mia_core_cmdoption_hh
22 #define mia_core_cmdoption_hh
23 
24 #include <string>
25 #include <memory>
26 #include <iostream>
28 #include <mia/core/handlerbase.hh>
29 
31 
32 class CCmdOption;
33 class CXMLElement;
34 
36 typedef std::map<char, CCmdOption *> CShortoptionMap;
37 
39 typedef std::map<std::string, CCmdOption *> CLongoptionMap;
40 
41 
51 {
52 public:
53 
54 
62  CCmdOption(char short_opt, const char *long_opt, const char *long_help,
63  const char *short_help, CCmdOptionFlags flags);
64 
65 
67  virtual ~CCmdOption();
68 
74 
78  size_t get_needed_args() const;
79 
83  void write_value(std::ostream& os) const;
84 
88  void set_value(const char *str_value);
89 
91  const char *get_long_option() const;
92 
94  char get_short_option() const;
95 
99  void print_short_help(std::ostream& os) const;
100 
104  void get_opt_help(std::ostream& os)const;
105 
109  void get_long_help(std::ostream& os)const;
110 
112  const std::string get_value_as_string() const;
113 
115  bool is_required() const;
116 
118  const char *long_help() const;
119 
128  std::string get_long_help_xml(CXMLElement& parent, HandlerHelpMap& handler_map) const;
129 
133  void post_set();
134 
141  void add_option_xml(CXMLElement& parent, HandlerHelpMap& handler_map) const;
142 protected:
143 
146 
151  virtual void do_get_long_help(std::ostream& os) const;
152 
153  bool has_flag(CCmdOptionFlags test_flags) const;
154 
155  void xmlhelp_set_attribute(CXMLElement& node, const char *name, const std::string& value) const;
156 private:
157  std::string get_flag_string() const;
158  const char *get_short_help() const;
159 
160  virtual void do_add_option(CShortoptionMap& sm, CLongoptionMap& lm);
161  virtual void do_print_short_help(std::ostream& os) const;
162  virtual void do_get_opt_help(std::ostream& os) const;
163 
164  virtual bool do_set_value(const char *str_value) = 0;
165  virtual size_t do_get_needed_args() const;
166  virtual void do_write_value(std::ostream& os) const = 0;
167 
168  virtual const std::string do_get_value_as_string() const;
169  virtual void do_post_set();
170 
171  virtual void do_get_long_help_xml(std::ostream& os, CXMLElement& parent, HandlerHelpMap& handler_map) const;
172 
173  char m_short_opt;
174  const char *m_long_opt;
175  const char *m_long_help;
176  const char *m_short_help;
177  CCmdOptionFlags m_flags;
178 };
179 
181 typedef std::shared_ptr<CCmdOption > PCmdOption;
182 
184 
185 #endif
CCmdOption::long_help
const char * long_help() const
CCmdOption::CCmdOption
CCmdOption(char short_opt, const char *long_opt, const char *long_help, const char *short_help, CCmdOptionFlags flags)
CCmdOption::add_option
void add_option(CShortoptionMap &sm, CLongoptionMap &lm)
CCmdOption::get_long_help_xml
std::string get_long_help_xml(CXMLElement &parent, HandlerHelpMap &handler_map) const
CCmdOption::xmlhelp_set_attribute
void xmlhelp_set_attribute(CXMLElement &node, const char *name, const std::string &value) const
NS_MIA_BEGIN
#define NS_MIA_BEGIN
conveniance define to start the mia namespace
Definition: defines.hh:33
CCmdOption::do_get_long_help
virtual void do_get_long_help(std::ostream &os) const
handlerbase.hh
CCmdOption::get_value_as_string
const std::string get_value_as_string() const
CCmdOption::has_flag
bool has_flag(CCmdOptionFlags test_flags) const
NS_MIA_END
#define NS_MIA_END
conveniance define to end the mia namespace
Definition: defines.hh:36
CCmdOption::is_required
bool is_required() const
HandlerHelpMap
std::map< std::string, const CPluginHandlerBase * > HandlerHelpMap
A map that is used to collect the plug-in handlers used in a program.
Definition: handlerbase.hh:35
CCmdOption::get_opt_help
void get_opt_help(std::ostream &os) const
CLongoptionMap
std::map< std::string, CCmdOption * > CLongoptionMap
Class to provide a maping from long option names to options.
Definition: cmdoption.hh:39
CCmdOption::clear_required
void clear_required()
clear the "required" flag
CXMLElement
This class implements a facade for the xml Element.
Definition: xmlinterface.hh:50
cmdoptionflags.hh
CCmdOption::get_short_option
char get_short_option() const
CCmdOption::get_long_help
void get_long_help(std::ostream &os) const
CCmdOption::get_needed_args
size_t get_needed_args() const
CCmdOption::set_value
void set_value(const char *str_value)
CShortoptionMap
std::map< char, CCmdOption * > CShortoptionMap
Class to provide a maping from short option names to options.
Definition: cmdoption.hh:33
CCmdOption::~CCmdOption
virtual ~CCmdOption()
ensure virtual destruction
EXPORT_CORE
#define EXPORT_CORE
Macro to manage Visual C++ style dllimport/dllexport.
Definition: defines.hh:101
CCmdOptionFlags
CCmdOptionFlags
Definition: cmdoptionflags.hh:28
CCmdOption::add_option_xml
void add_option_xml(CXMLElement &parent, HandlerHelpMap &handler_map) const
CCmdOption::print_short_help
void print_short_help(std::ostream &os) const
CCmdOption
The base class for all command line options.
Definition: cmdoption.hh:51
PCmdOption
std::shared_ptr< CCmdOption > PCmdOption
a shared pointer definition of the Option
Definition: cmdoption.hh:181
CCmdOption::post_set
void post_set()
CCmdOption::write_value
void write_value(std::ostream &os) const
CCmdOption::get_long_option
const char * get_long_option() const