cmdlineparser.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_cmdlineparser_hh
22 #define mia_core_cmdlineparser_hh
23 
24 //#include <miaconfig.h>
25 #include <vector>
26 #include <map>
27 #include <memory>
28 #include <sstream>
29 #include <iostream>
30 #include <string>
31 #include <iterator>
33 #include <mia/core/dictmap.hh>
34 #include <mia/core/flagstring.hh>
35 #include <mia/core/handlerbase.hh>
37 
39 
41 extern EXPORT_CORE const char *g_help_optiongroup;
42 
44 typedef std::map<std::string, std::string> CHistoryRecord;
45 
47 typedef std::pair<std::string, CHistoryRecord> CHistoryEntry;
48 
57  pdi_group = 0, /*<! The group the program belongs to, in the help the program will be described in this section */
58  pdi_short = 1, /*<! A sgort description of the program, this will be the head line in the unix man page. */
59  pdi_description = 2, /*<! The full description of the program */
60  pdi_example_descr = 3, /*<! description of the example that follows */
61  pdi_example_code = 4, /*<! The example command line without the program name */
62  pdi_author = 5 /*<! Author name (if other than main MIA contributer(s) */
63 };
64 
65 extern const std::map<EProgramDescriptionEntry, const char *> g_DescriptionEntryNames;
66 
81 typedef std::map<EProgramDescriptionEntry, const char *> SProgramDescription;
82 
83 
84 
85 
91 class CCmdFlagOption: public CCmdOption{
92 
93 public:
104  CCmdFlagOption(int& val, const CFlagString& map, char short_opt, const char *long_opt,
105  const char *long_help, const char *short_help,
107 private:
108  virtual bool do_set_value(const char *str_value);
109  virtual size_t do_get_needed_args() const;
110  virtual void do_write_value(std::ostream& os) const;
111  virtual void do_get_long_help(std::ostream& os) const;
112  virtual const std::string do_get_value_as_string() const;
113  int& m_value;
114  const CFlagString m_map;
115 };
116 
117 
127 public:
128 
133  hr_no = 0,
139  hr_selftest
140  };
141 
149 
151  ~CCmdOptionList();
152 
156  void add(PCmdOption opt);
157 
162  void add(const std::string& group, PCmdOption opt);
163 
173  void add_selftest(int& test_result, CSelftestCallback *callback);
174 
185  EHelpRequested parse(size_t argc, char *args[], const std::string& additional_type,
186  const CPluginHandlerBase *additional_help = NULL)
187  __attribute__((warn_unused_result));
188 
200  EHelpRequested parse(size_t argc, const char *args[], const std::string& additional_type,
201  const CPluginHandlerBase *additional_help = NULL)
202  __attribute__((warn_unused_result));
203 
209  EHelpRequested parse(size_t argc, char *args[]) __attribute__((warn_unused_result));
210 
216  EHelpRequested parse(size_t argc, const char *args[]) __attribute__((warn_unused_result));
217 
219  const std::vector<std::string>& get_remaining() const;
220 
224  CHistoryRecord get_values() const;
225 
230  void set_group(const std::string& group);
231 
236  void set_logstream(std::ostream& os);
237 
238 
241  void set_stdout_is_result();
242 private:
243  EHelpRequested do_parse(size_t argc, const char *args[], bool has_additional,
244  const CPluginHandlerBase *additional_help)
245  __attribute__((warn_unused_result));
246  int handle_shortargs(const char *arg, size_t argc, const char *args[]);
247  struct CCmdOptionListData *m_impl;
248 };
249 
250 
251 // implementation of template classes and functions
252 
253 
270 template <typename T>
271 PCmdOption make_opt(T& value, const char *long_opt, char short_opt,
272  const char *help, CCmdOptionFlags flags = CCmdOptionFlags::none)
273 {
275  return PCmdOption(new CParamOption( short_opt, long_opt, new CTParameter<T>(value, required, help)));
276 }
277 
293 template <typename T>
294 PCmdOption make_opt(T& value, EParameterBounds bflags, const std::vector<T>& bounds,
295  const char *long_opt, char short_opt,
296  const char *help, CCmdOptionFlags flags = CCmdOptionFlags::none)
297 {
299  return PCmdOption(new CParamOption( short_opt, long_opt,
300  new TBoundedParameter<T>(value, bflags, bounds, required, help)));
301 }
302 
317 PCmdOption make_opt(bool& value, const char *long_opt, char short_opt, const char *help,
319 
335 template <typename T>
336 PCmdOption make_opt(T& value, const TDictMap<T>& map,
337  const char *long_opt, char short_opt, const char *help)
338 {
339  return PCmdOption(new CParamOption( short_opt, long_opt,
340  new CDictParameter<T>(value, map, help)));
341 }
342 
343 
360 PCmdOption make_opt(int& value, const CFlagString& map, const char *long_opt,
361  char short_opt, const char *long_help,
362  const char *short_help,
364 
365 
381 PCmdOption make_opt(std::string& value, const char *long_opt, char short_opt, const char *long_help,
382  CCmdOptionFlags flags = CCmdOptionFlags::none, const CPluginHandlerBase *plugin_hint = NULL);
383 
384 
385 
401 template <typename T>
402 PCmdOption make_opt(T& value, const std::set<T>& valid_set,
403  const char *long_opt, char short_opt,
404  const char *help,
406 {
408  return PCmdOption(new CParamOption( short_opt, long_opt,
409  new CSetParameter<T>(value, valid_set, help, required)));
410 }
411 
412 
432 template <typename T>
433 PCmdOption make_opt(typename std::shared_ptr<T>& value, const char *default_value, const char *long_opt,
434  char short_opt, const char *help,
436 {
438  typedef typename FactoryTrait<T>::type F;
439  return PCmdOption(new CParamOption( short_opt, long_opt,
440  new TFactoryParameter<F>(value, default_value, required, help)));
441 }
442 
461 template <typename T>
462 PCmdOption make_opt(typename std::unique_ptr<T>& value, const char *default_value, const char *long_opt,
463  char short_opt, const char *help, CCmdOptionFlags flags = CCmdOptionFlags::none)
464 {
466  typedef typename FactoryTrait<T>::type F;
467  return PCmdOption(new CParamOption( short_opt, long_opt,
468  new TFactoryParameter<F>(value, default_value, required, help)));
469 }
470 
471 
473 
474 #endif
std::pair< std::string, CHistoryRecord > CHistoryEntry
EXPORT_CORE const char * g_help_optiongroup
the string defining the name of the help options
std::map< std::string, std::string > CHistoryRecord
A parameter that get&#39;s initialized by a factory to a shared or unique pointer.
Definition: parameter.hh:401
A parameter that can only assume values out of a limited set.
Definition: parameter.hh:466
The base class for all command line options.
Definition: cmdoption.hh:50
#define NS_MIA_BEGIN
conveniance define to start the mia namespace
Definition: defines.hh:33
Command line option that translates a string to a set of flags.
std::map< EProgramDescriptionEntry, const char * > SProgramDescription
the map that holds a basic program description
The base class for all plugin handlers.
Definition: handlerbase.hh:56
Generic type of a complex paramter.
Definition: parameter.hh:169
EParameterBounds
Scalar parameter with an expected value range.
Definition: parameter.hh:214
CCmdFlagOption(int &val, const CFlagString &map, char short_opt, const char *long_opt, const char *long_help, const char *short_help, CCmdOptionFlags flags=CCmdOptionFlags::none)
const char * long_help() const
CCmdOptionFlags
command line option that handles a parameter
Definition: paramoption.hh:36
const mia::SProgramDescription description
Definition: autotest.hh:44
std::shared_ptr< CCmdOption > PCmdOption
a shared pointer definition of the Option
Definition: cmdoption.hh:180
EProgramDescriptionEntry
program description entry identifiers
#define EXPORT_CORE
Macro to manage Visual C++ style dllimport/dllexport.
Definition: defines.hh:101
bool has_flag(CCmdOptionFlags test_flags) const
Dictionary parameter.
Definition: parameter.hh:364
PCmdOption make_opt(T &value, const char *long_opt, char short_opt, const char *help, CCmdOptionFlags flags=CCmdOptionFlags::none)
Create a standard option that sets a value of the give type.
The class to hold the list of options.
const std::map< EProgramDescriptionEntry, const char * > g_DescriptionEntryNames
A mapper from emums to string values. - usefull for names flags.
Definition: dictmap.hh:45
#define NS_MIA_END
conveniance define to end the mia namespace
Definition: defines.hh:36