cmdlineparser.hh
Go to the documentation of this file.
/* -*- mia-c++ -*-
*
* This file is part of MIA - a toolbox for medical image analysis
* Copyright (c) Leipzig, Madrid 1999-2017 Gert Wollny
*
* MIA is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MIA; if not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef mia_core_cmdlineparser_hh
#define mia_core_cmdlineparser_hh
//#include <miaconfig.h>
#include <vector>
#include <map>
#include <memory>
#include <sstream>
#include <iostream>
#include <string>
#include <iterator>
#include <mia/core/cmdparamoption.hh>
#include <mia/core/dictmap.hh>
#include <mia/core/flagstring.hh>
#include <mia/core/handlerbase.hh>
#include <mia/core/selftestcmdoption.hh>
NS_MIA_BEGIN
extern EXPORT_CORE const char *g_help_optiongroup;
typedef std::map<std::string, std::string> CHistoryRecord;
typedef std::pair<std::string, CHistoryRecord> CHistoryEntry;
enum EProgramDescriptionEntry {
pdi_group = 0, /*<! The group the program belongs to, in the help the program will be described in this section */
pdi_short = 1, /*<! A sgort description of the program, this will be the head line in the unix man page. */
pdi_description = 2, /*<! The full description of the program */
pdi_example_descr = 3, /*<! description of the example that follows */
pdi_example_code = 4, /*<! The example command line without the program name */
pdi_author = 5 /*<! Author name (if other than main MIA contributer(s) */
};
extern const std::map<EProgramDescriptionEntry, const char *> g_DescriptionEntryNames;
typedef std::map<EProgramDescriptionEntry, const char *> SProgramDescription;
class CCmdFlagOption: public CCmdOption
{
public:
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);
private:
virtual bool do_set_value(const char *str_value);
virtual size_t do_get_needed_args() const;
virtual void do_write_value(std::ostream& os) const;
virtual void do_get_long_help(std::ostream& os) const;
virtual const std::string do_get_value_as_string() const;
int& m_value;
const CFlagString m_map;
};
class EXPORT_CORE CCmdOptionList
{
public:
enum EHelpRequested {
hr_no = 0,
hr_help,
hr_help_xml,
hr_usage,
hr_version,
hr_copyright,
hr_selftest
};
CCmdOptionList(const SProgramDescription& description);
~CCmdOptionList();
void add(PCmdOption opt);
void add(const std::string& group, PCmdOption opt);
void add_selftest(int& test_result, CSelftestCallback *callback);
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));
EHelpRequested parse(size_t argc, char *args[]) __attribute__((warn_unused_result));
EHelpRequested parse(size_t argc, const char *args[]) __attribute__((warn_unused_result));
const std::vector<std::string>& get_remaining() const;
CHistoryRecord get_values() const;
void set_group(const std::string& group);
void set_logstream(std::ostream& os);
void set_stdout_is_result();
private:
EHelpRequested do_parse(size_t argc, const char *args[], bool has_additional,
const CPluginHandlerBase *additional_help)
__attribute__((warn_unused_result));
int handle_shortargs(const char *arg, size_t argc, const char *args[]);
struct CCmdOptionListData *m_impl;
};
// implementation of template classes and functions
template <typename T>
PCmdOption make_opt(T& value, const char *long_opt, char short_opt,
const char *help, CCmdOptionFlags flags = CCmdOptionFlags::none)
{
bool required = has_flag(flags, CCmdOptionFlags::required);
return PCmdOption(new CParamOption( short_opt, long_opt, new CTParameter<T>(value, required, help)));
}
template <typename T>
PCmdOption make_opt(T& value, EParameterBounds bflags, const std::vector<T>& bounds,
const char *long_opt, char short_opt,
const char *help, CCmdOptionFlags flags = CCmdOptionFlags::none)
{
bool required = has_flag(flags, CCmdOptionFlags::required);
return PCmdOption(new CParamOption( short_opt, long_opt,
new TBoundedParameter<T>(value, bflags, bounds, required, help)));
}
PCmdOption make_opt(bool& value, const char *long_opt, char short_opt, const char *help,
CCmdOptionFlags flags = CCmdOptionFlags::none);
template <typename T>
PCmdOption make_opt(T& value, const TDictMap<T>& map,
const char *long_opt, char short_opt, const char *help)
{
return PCmdOption(new CParamOption( short_opt, long_opt,
new CDictParameter<T>(value, map, help)));
}
PCmdOption make_opt(int& value, const CFlagString& map, const char *long_opt,
char short_opt, const char *long_help,
const char *short_help,
CCmdOptionFlags flags = CCmdOptionFlags::none);
PCmdOption make_opt(std::string& value, const char *long_opt, char short_opt, const char *long_help,
CCmdOptionFlags flags = CCmdOptionFlags::none, const CPluginHandlerBase *plugin_hint = NULL);
template <typename T>
PCmdOption make_opt(T& value, const std::set<T>& valid_set,
const char *long_opt, char short_opt,
const char *help,
CCmdOptionFlags flags = CCmdOptionFlags::none)
{
bool required = has_flag(flags, CCmdOptionFlags::required);
return PCmdOption(new CParamOption( short_opt, long_opt,
new CSetParameter<T>(value, valid_set, help, required)));
}
template <typename T>
PCmdOption make_opt(typename std::shared_ptr<T>& value, const char *default_value, const char *long_opt,
char short_opt, const char *help,
CCmdOptionFlags flags = CCmdOptionFlags::none)
{
bool required = has_flag(flags, CCmdOptionFlags::required);
typedef typename FactoryTrait<T>::type F;
return PCmdOption(new CParamOption( short_opt, long_opt,
new TFactoryParameter<F>(value, default_value, required, help)));
}
template <typename T>
PCmdOption make_opt(typename std::unique_ptr<T>& value, const char *default_value, const char *long_opt,
char short_opt, const char *help, CCmdOptionFlags flags = CCmdOptionFlags::none)
{
bool required = has_flag(flags, CCmdOptionFlags::required);
typedef typename FactoryTrait<T>::type F;
return PCmdOption(new CParamOption( short_opt, long_opt,
new TFactoryParameter<F>(value, default_value, required, help)));
}
NS_MIA_END
#endif
description
const mia::SProgramDescription description
Definition autotest.hh:46
CCmdFlagOption::CCmdFlagOption
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)
CCmdOptionList::add
void add(PCmdOption opt)
CCmdOptionList::parse
EHelpRequested parse(size_t argc, char *args[], const std::string &additional_type, const CPluginHandlerBase *additional_help=NULL) __attribute__((warn_unused_result))
CCmdOptionList::parse
EHelpRequested parse(size_t argc, const char *args[], const std::string &additional_type, const CPluginHandlerBase *additional_help=NULL) __attribute__((warn_unused_result))
CCmdOptionList::add_selftest
void add_selftest(int &test_result, CSelftestCallback *callback)
CCmdOptionList::~CCmdOptionList
~CCmdOptionList()
cleanup
CCmdOptionList::CCmdOptionList
CCmdOptionList(const SProgramDescription &description)
CCmdOptionList::parse
EHelpRequested parse(size_t argc, const char *args[]) __attribute__((warn_unused_result))
CCmdOptionList::get_values
CHistoryRecord get_values() const
CCmdOptionList::add
void add(const std::string &group, PCmdOption opt)
CCmdOptionList::get_remaining
const std::vector< std::string > & get_remaining() const
CCmdOptionList::parse
EHelpRequested parse(size_t argc, char *args[]) __attribute__((warn_unused_result))
CCmdOptionList::EHelpRequested
EHelpRequested
Definition cmdlineparser.hh:134
CCmdOptionList::hr_version
@ hr_version
Definition cmdlineparser.hh:139
CCmdOptionList::hr_selftest
@ hr_selftest
Definition cmdlineparser.hh:141
CCmdOptionList::hr_no
@ hr_no
Definition cmdlineparser.hh:135
CCmdOptionList::hr_usage
@ hr_usage
Definition cmdlineparser.hh:138
CCmdOptionList::hr_help
@ hr_help
Definition cmdlineparser.hh:136
CCmdOptionList::hr_help_xml
@ hr_help_xml
Definition cmdlineparser.hh:137
CCmdOptionList::hr_copyright
@ hr_copyright
Definition cmdlineparser.hh:140
CCmdOptionList::set_group
void set_group(const std::string &group)
CCmdOptionList::set_stdout_is_result
void set_stdout_is_result()
CCmdOptionList::set_logstream
void set_logstream(std::ostream &os)
CCmdOption::CCmdOption
CCmdOption(char short_opt, const char *long_opt, const char *long_help, const char *short_help, CCmdOptionFlags flags)
CCmdOption::long_help
const char * long_help() const
CDictParameter
Dictionary parameter.
Definition parameter.hh:368
CFlagString
Definition flagstring.hh:32
CParamOption
command line option that handles a parameter
Definition paramoption.hh:37
CPluginHandlerBase
The base class for all plugin handlers.
Definition handlerbase.hh:57
CSetParameter
A parameter that can only assume values out of a limited set.
Definition parameter.hh:472
CTParameter
Generic type of a complex paramter.
Definition parameter.hh:171
TBoundedParameter
Definition parameter.hh:237
TDictMap
A mapper from emums to string values. - usefull for names flags.
Definition dictmap.hh:46
TFactoryParameter
A parameter that get's initialized by a factory to a shared or unique pointer.
Definition parameter.hh:406
g_DescriptionEntryNames
const std::map< EProgramDescriptionEntry, const char * > g_DescriptionEntryNames
g_help_optiongroup
NS_MIA_BEGIN EXPORT_CORE const char * g_help_optiongroup
the string defining the name of the help options
CHistoryEntry
std::pair< std::string, CHistoryRecord > CHistoryEntry
Definition cmdlineparser.hh:47
CHistoryRecord
std::map< std::string, std::string > CHistoryRecord
Definition cmdlineparser.hh:44
PCmdOption
std::shared_ptr< CCmdOption > PCmdOption
a shared pointer definition of the Option
Definition cmdoption.hh:181
CCmdOptionFlags
CCmdOptionFlags
Definition cmdoptionflags.hh:28
CCmdOptionFlags::none
@ none
Definition cmdoptionflags.hh:29
CCmdOptionFlags::required
@ required
Definition cmdoptionflags.hh:30
cmdparamoption.hh
NS_MIA_BEGIN
#define NS_MIA_BEGIN
conveniance define to start the mia namespace
Definition defines.hh:33
EXPORT_CORE
#define EXPORT_CORE
Macro to manage Visual C++ style dllimport/dllexport.
Definition defines.hh:101
NS_MIA_END
#define NS_MIA_END
conveniance define to end the mia namespace
Definition defines.hh:36
dictmap.hh
flagstring.hh
EProgramDescriptionEntry
EProgramDescriptionEntry
program description entry identifiers
Definition cmdlineparser.hh:56
make_opt
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.
Definition cmdlineparser.hh:273
SProgramDescription
std::map< EProgramDescriptionEntry, const char * > SProgramDescription
the map that holds a basic program description
Definition cmdlineparser.hh:81
EParameterBounds
EParameterBounds
Scalar parameter with an expected value range.
Definition parameter.hh:216
pdi_example_descr
@ pdi_example_descr
Definition cmdlineparser.hh:60
pdi_example_code
@ pdi_example_code
Definition cmdlineparser.hh:61
pdi_short
@ pdi_short
Definition cmdlineparser.hh:58
pdi_description
@ pdi_description
Definition cmdlineparser.hh:59
pdi_author
@ pdi_author
Definition cmdlineparser.hh:62
pdi_group
@ pdi_group
Definition cmdlineparser.hh:57
handlerbase.hh
selftestcmdoption.hh
CSelftestCallback
Definition selftestcmdoption.hh:41