My Project
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 
92 {
93 
94 public:
105  CCmdFlagOption(int& val, const CFlagString& map, char short_opt, const char *long_opt,
106  const char *long_help, const char *short_help,
108 private:
109  virtual bool do_set_value(const char *str_value);
110  virtual size_t do_get_needed_args() const;
111  virtual void do_write_value(std::ostream& os) const;
112  virtual void do_get_long_help(std::ostream& os) const;
113  virtual const std::string do_get_value_as_string() const;
114  int& m_value;
115  const CFlagString m_map;
116 };
117 
118 
128 {
129 public:
130 
135  hr_no = 0,
141  hr_selftest
142  };
143 
151 
154 
158  void add(PCmdOption opt);
159 
164  void add(const std::string& group, PCmdOption opt);
165 
175  void add_selftest(int& test_result, CSelftestCallback *callback);
176 
187  EHelpRequested parse(size_t argc, char *args[], const std::string& additional_type,
188  const CPluginHandlerBase *additional_help = NULL)
189  __attribute__((warn_unused_result));
190 
202  EHelpRequested parse(size_t argc, const char *args[], const std::string& additional_type,
203  const CPluginHandlerBase *additional_help = NULL)
204  __attribute__((warn_unused_result));
205 
211  EHelpRequested parse(size_t argc, char *args[]) __attribute__((warn_unused_result));
212 
218  EHelpRequested parse(size_t argc, const char *args[]) __attribute__((warn_unused_result));
219 
221  const std::vector<std::string>& get_remaining() const;
222 
227 
232  void set_group(const std::string& group);
233 
238  void set_logstream(std::ostream& os);
239 
240 
244 private:
245  EHelpRequested do_parse(size_t argc, const char *args[], bool has_additional,
246  const CPluginHandlerBase *additional_help)
247  __attribute__((warn_unused_result));
248  int handle_shortargs(const char *arg, size_t argc, const char *args[]);
249  struct CCmdOptionListData *m_impl;
250 };
251 
252 
253 // implementation of template classes and functions
254 
255 
272 template <typename T>
273 PCmdOption make_opt(T& value, const char *long_opt, char short_opt,
274  const char *help, CCmdOptionFlags flags = CCmdOptionFlags::none)
275 {
276  bool required = has_flag(flags, CCmdOptionFlags::required);
277  return PCmdOption(new CParamOption( short_opt, long_opt, new CTParameter<T>(value, required, help)));
278 }
279 
295 template <typename T>
296 PCmdOption make_opt(T& value, EParameterBounds bflags, const std::vector<T>& bounds,
297  const char *long_opt, char short_opt,
298  const char *help, CCmdOptionFlags flags = CCmdOptionFlags::none)
299 {
300  bool required = has_flag(flags, CCmdOptionFlags::required);
301  return PCmdOption(new CParamOption( short_opt, long_opt,
302  new TBoundedParameter<T>(value, bflags, bounds, required, help)));
303 }
304 
319 PCmdOption make_opt(bool& value, const char *long_opt, char short_opt, const char *help,
321 
337 template <typename T>
338 PCmdOption make_opt(T& value, const TDictMap<T>& map,
339  const char *long_opt, char short_opt, const char *help)
340 {
341  return PCmdOption(new CParamOption( short_opt, long_opt,
342  new CDictParameter<T>(value, map, help)));
343 }
344 
345 
362 PCmdOption make_opt(int& value, const CFlagString& map, const char *long_opt,
363  char short_opt, const char *long_help,
364  const char *short_help,
366 
367 
383 PCmdOption make_opt(std::string& value, const char *long_opt, char short_opt, const char *long_help,
384  CCmdOptionFlags flags = CCmdOptionFlags::none, const CPluginHandlerBase *plugin_hint = NULL);
385 
386 
387 
403 template <typename T>
404 PCmdOption make_opt(T& value, const std::set<T>& valid_set,
405  const char *long_opt, char short_opt,
406  const char *help,
408 {
409  bool required = has_flag(flags, CCmdOptionFlags::required);
410  return PCmdOption(new CParamOption( short_opt, long_opt,
411  new CSetParameter<T>(value, valid_set, help, required)));
412 }
413 
414 
434 template <typename T>
435 PCmdOption make_opt(typename std::shared_ptr<T>& value, const char *default_value, const char *long_opt,
436  char short_opt, const char *help,
438 {
439  bool required = has_flag(flags, CCmdOptionFlags::required);
440  typedef typename FactoryTrait<T>::type F;
441  return PCmdOption(new CParamOption( short_opt, long_opt,
442  new TFactoryParameter<F>(value, default_value, required, help)));
443 }
444 
463 template <typename T>
464 PCmdOption make_opt(typename std::unique_ptr<T>& value, const char *default_value, const char *long_opt,
465  char short_opt, const char *help, CCmdOptionFlags flags = CCmdOptionFlags::none)
466 {
467  bool required = has_flag(flags, CCmdOptionFlags::required);
468  typedef typename FactoryTrait<T>::type F;
469  return PCmdOption(new CParamOption( short_opt, long_opt,
470  new TFactoryParameter<F>(value, default_value, required, help)));
471 }
472 
473 
475 
476 #endif
const mia::SProgramDescription description
Definition: autotest.hh:44
Command line option that translates a string to a set of flags.
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)
The class to hold the list of options.
void add(PCmdOption opt)
EHelpRequested parse(size_t argc, char *args[], const std::string &additional_type, const CPluginHandlerBase *additional_help=NULL) __attribute__((warn_unused_result))
EHelpRequested parse(size_t argc, const char *args[], const std::string &additional_type, const CPluginHandlerBase *additional_help=NULL) __attribute__((warn_unused_result))
void add_selftest(int &test_result, CSelftestCallback *callback)
~CCmdOptionList()
cleanup
CCmdOptionList(const SProgramDescription &description)
EHelpRequested parse(size_t argc, const char *args[]) __attribute__((warn_unused_result))
CHistoryRecord get_values() const
void add(const std::string &group, PCmdOption opt)
EHelpRequested parse(size_t argc, char *args[]) __attribute__((warn_unused_result))
void set_group(const std::string &group)
const std::vector< std::string > & get_remaining() const
void set_stdout_is_result()
void set_logstream(std::ostream &os)
The base class for all command line options.
Definition: cmdoption.hh:51
const char * long_help() const
Dictionary parameter.
Definition: parameter.hh:368
command line option that handles a parameter
Definition: paramoption.hh:37
The base class for all plugin handlers.
Definition: handlerbase.hh:57
A parameter that can only assume values out of a limited set.
Definition: parameter.hh:472
Generic type of a complex paramter.
Definition: parameter.hh:171
A mapper from emums to string values. - usefull for names flags.
Definition: dictmap.hh:46
A parameter that get's initialized by a factory to a shared or unique pointer.
Definition: parameter.hh:406
const std::map< EProgramDescriptionEntry, const char * > g_DescriptionEntryNames
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
std::shared_ptr< CCmdOption > PCmdOption
a shared pointer definition of the Option
Definition: cmdoption.hh:181
CCmdOptionFlags
#define NS_MIA_BEGIN
conveniance define to start the mia namespace
Definition: defines.hh:33
#define EXPORT_CORE
Macro to manage Visual C++ style dllimport/dllexport.
Definition: defines.hh:101
#define NS_MIA_END
conveniance define to end the mia namespace
Definition: defines.hh:36
EProgramDescriptionEntry
program description entry identifiers
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.
std::map< EProgramDescriptionEntry, const char * > SProgramDescription
the map that holds a basic program description
EParameterBounds
Scalar parameter with an expected value range.
Definition: parameter.hh:216
@ pdi_example_descr
@ pdi_example_code
@ pdi_short
@ pdi_description
@ pdi_author
@ pdi_group