Bayeux  3.4.1
Core Foundation library for SuperNEMO
base_command.h
Go to the documentation of this file.
1 //
4 // Copyright (c) 2015 by François Mauger <mauger@lpccaen.in2p3.fr>
5 //
6 // This file is part of datatools.
7 //
8 // datatools is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation, either version 3 of the License, or
11 // (at your option) any later version.
12 //
13 // datatools is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with datatools. If not, see <http://www.gnu.org/licenses/>.
20 
21 #ifndef DATATOOLS_UI_BASE_COMMAND_H
22 #define DATATOOLS_UI_BASE_COMMAND_H
23 
24 // Standard library:
25 #include <string>
26 #include <vector>
27 #include <memory>
28 
29 // Third party:
30 // - Boost:
31 #include <boost/optional.hpp>
32 #include <boost/program_options.hpp>
33 #include <boost/cstdint.hpp>
34 
35 // This project:
37 #include <datatools/properties.h>
39 #include <datatools/version_id.h>
40 
41 namespace datatools {
42 
43  namespace ui {
44 
45  class base_command_interface;
46 
50  {
51  public:
52 
53  // Aliases:
54  typedef boost::program_options::options_description opts_type;
55  typedef boost::program_options::positional_options_description args_type;
56  typedef boost::program_options::variables_map vmap_type;
57 
59  virtual bool is_name_valid(const std::string & candidate_name_) const;
60 
62  base_command();
63 
65  base_command(const std::string & name_,
66  const std::string & description_ = "",
67  const version_id & vid_ = version_id::invalid());
68 
70  virtual ~base_command();
71 
73  bool has_generic_opts() const;
74 
76  const opts_type & get_generic_opts() const;
77 
79  bool use_generic_options() const;
80 
82  bool use_generic_help() const;
83 
85  bool has_opts() const;
86 
88  const opts_type & get_opts() const;
89 
91  bool has_args() const;
92 
94  bool has_short_ui_options() const;
95 
97  bool has_version() const;
98 
100  const datatools::version_id & get_version() const;
101 
103  void set_version(const datatools::version_id &);
104 
106  virtual bool is_valid() const;
107 
109  bool has_parent_interface() const;
110 
113 
116 
119 
121  bool is_initialized() const;
122 
124  void initialize_simple();
125 
127  void initialize(const properties & config_);
128 
130  void reset();
131 
133  bool is_disabled() const;
134 
136  int operator()(const std::vector<std::string> & argv_,
138  uint32_t flags_ = 0);
139 
142  operator()(const std::vector<std::string> & argv_,
143  uint32_t flags_ = 0);
144 
146  void call(const std::vector<std::string> & argv_,
148  uint32_t flags_ = 0);
149 
151  void print_usage(std::ostream & out_, uint32_t flags_ = 0) const;
152 
154  void print_version(std::ostream & out_, uint32_t flags_ = 0) const;
155 
157  virtual void tree_dump(std::ostream & out_ = std::clog,
158  const std::string & title_ = "",
159  const std::string & indent_ = "",
160  bool inherit_ = false) const;
161 
162  protected:
163 
164  void _set_version(const datatools::version_id &);
165 
166  virtual void _init(const properties & config_);
167 
168  virtual void _fini();
169 
171  uint32_t flags_ = 0) = 0;
172 
174  virtual bool _is_valid() const;
175 
177  virtual bool _is_disabled() const;
178 
180  uint32_t flags_ = 0);
181 
183 
184  void _alloc_generic_opts();
185 
186  void _free_generic_opts();
187 
188  opts_type & _grab_opts();
189 
190  void _alloc_opts(const std::string & opts_title_ = "Allowed options");
191 
192  void _free_opts();
193 
194  args_type & _grab_args();
195 
196  void _alloc_args();
197 
198  void _free_args();
199 
200  vmap_type & _grab_vmap();
201 
202  void _alloc_vmap();
203 
204  void _free_vmap();
205 
207 
208  protected:
209 
210  // Management:
211  bool _initialized_ = false;
212 
213  // Configuration:
214  bool _no_short_ui_options_ = false;
215  bool _no_generic_options_ = false;
216  bool _no_generic_help_ = false;
217  boost::optional<datatools::version_id> _version_;
218  boost::scoped_ptr<opts_type> _generic_opts_;
219  boost::scoped_ptr<opts_type> _opts_;
220  boost::scoped_ptr<args_type> _args_;
222 
223  // Working data:
224  std::unique_ptr<vmap_type> _vmap_;
225 
226  };
227 
228  } // namespace ui
229 
230 } // namespace datatools
231 
232 #endif // DATATOOLS_UI_BASIC_COMMAND_H
233 
234 // Local Variables: --
235 // mode: c++ --
236 // c-file-style: "gnu" --
237 // tab-width: 2 --
238 // End: --
bool has_opts() const
Check if some options are described.
base_command_interface & grab_parent_interface()
Return the parent interface.
void _alloc_opts(const std::string &opts_title_="Allowed options")
bool use_generic_options() const
Check if generic options are used.
bool _initialized_
Initialization flag.
Definition: base_command.h:211
bool _no_generic_options_
Definition: base_command.h:215
virtual bool _is_valid() const
Check the validity (this can be overloaded)
void _base_run(datatools::command::returned_info &, uint32_t flags_=0)
int operator()(const std::vector< std::string > &argv_, datatools::command::returned_info &cri_, uint32_t flags_=0)
Main execution operator.
virtual bool is_valid() const
Check validity.
base_command_interface * _parent_interface_
Reference to the parent interface (optional)
Definition: base_command.h:221
bool is_disabled() const
Check if the command is disabled.
bool _no_generic_help_
Definition: base_command.h:216
boost::scoped_ptr< opts_type > _opts_
Description of options.
Definition: base_command.h:219
void print_version(std::ostream &out_, uint32_t flags_=0) const
Print version.
std::unique_ptr< vmap_type > _vmap_
Description of variables map.
Definition: base_command.h:224
virtual void _init(const properties &config_)
static const version_id & invalid()
Return an invalid version identifier.
boost::optional< datatools::version_id > _version_
Optional version.
Definition: base_command.h:217
const base_command_interface & get_parent_interface() const
Return the parent interface.
Command returned information.
Definition: command_utils.h:78
bool has_parent_interface() const
Check is the parent interface is set.
virtual bool _is_disabled() const
Check the disabled status (default: false)
boost::scoped_ptr< args_type > _args_
Description of positional options.
Definition: base_command.h:220
bool has_version() const
Check if the version is available.
bool is_initialized() const
Check if the command is initialized.
bool has_args() const
Check if some positional options are set.
void call(const std::vector< std::string > &argv_, datatools::command::returned_info &cri_, uint32_t flags_=0)
Call.
const datatools::version_id & get_version() const
Return the version of the command.
boost::program_options::positional_options_description args_type
Definition: base_command.h:55
void initialize(const properties &config_)
Initialize.
virtual ~base_command()
Destructor.
void print_usage(std::ostream &out_, uint32_t flags_=0) const
Print usage.
Base command.
Definition: base_command.h:48
bool _no_short_ui_options_
Definition: base_command.h:214
Command interface for arbitrary objects.
Definition: base_command_interface.h:54
boost::scoped_ptr< opts_type > _generic_opts_
Description of generic options.
Definition: base_command.h:218
boost::program_options::variables_map vmap_type
Definition: base_command.h:56
The Bayeux/datatools library top-level namespace.
Definition: algo.h:13
virtual void _run(datatools::command::returned_info &, uint32_t flags_=0)=0
A base class with useful attributes usable in many contexts.
Definition: enriched_base.h:52
opts_type & _grab_generic_opts()
void set_parent_interface(base_command_interface &)
Set the parent interface.
virtual bool is_name_valid(const std::string &candidate_name_) const
Check if a name is valid.
const opts_type & get_generic_opts() const
Return the description of generic options.
boost::program_options::options_description opts_type
Definition: base_command.h:54
void initialize_simple()
Initialize.
base_command()
Default constructor.
virtual void tree_dump(std::ostream &out_=std::clog, const std::string &title_="", const std::string &indent_="", bool inherit_=false) const
Smart print.
A class representing a version ID :
Definition: version_id.h:67
bool has_generic_opts() const
Check if some generic options are described.
void _set_version(const datatools::version_id &)
bool use_generic_help() const
Check if generic help are used.
void set_version(const datatools::version_id &)
Set the version of the command.
const opts_type & get_opts() const
Return the description of options.
bool has_short_ui_options() const
Check if short UI options are allowed.
A dictionary of arbitrary properties.
Definition: properties.h:125