cmdparamoption.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_cmdparamlineparser_hh
#define mia_core_cmdparamlineparser_hh
#include <mia/core/cmdoption.hh>
#include <mia/core/typedescr.hh>
#include <mia/core/paramoption.hh>
#include <mia/core/handlerbase.hh>
NS_MIA_BEGIN
template <typename T>
class TCmdOption: public CCmdOption
{
public:
TCmdOption(T& val, char short_opt, const char *long_opt, const char *long_help,
const char *short_help, CCmdOptionFlags flags = CCmdOptionFlags::none);
private:
virtual void do_get_long_help_xml(std::ostream& os, CXMLElement& parent, HandlerHelpMap& handler_map) const;
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 const std::string do_get_value_as_string() const;
T& m_value;
};
template <typename T>
class TRepeatableCmdOption: public CCmdOption
{
public:
TRepeatableCmdOption(std::vector<T>& val, char short_opt, const char *long_opt, const char *long_help,
const char *short_help, CCmdOptionFlags flags = CCmdOptionFlags::none);
private:
virtual void do_get_long_help_xml(std::ostream& os, CXMLElement& parent, HandlerHelpMap& handler_map) const;
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 const std::string do_get_value_as_string() const;
std::vector<T>& m_value;
};
template <typename T>
struct __dispatch_opt {
static void init(T& /*value*/)
{
}
static bool apply(const char *svalue, T& value)
{
std::istringstream sval(svalue);
sval >> value;
while (isspace(sval.peek())) {
char c;
sval >> c;
}
return sval.eof();
}
static size_t size(const T /*value*/)
{
return 1;
}
static void apply(std::ostream& os, const T& value, bool /*required*/)
{
os << "=" << value << " ";
}
static const std::string get_as_string(const T& value)
{
std::ostringstream os;
os << value;
return os.str();
}
};
template <typename T>
struct __dispatch_opt< std::vector<T>> {
static void init(std::vector<T>& /*value*/)
{
}
static bool apply(const char *svalue, std::vector<T>& value)
{
std::string h(svalue);
unsigned int n = 1;
for (std::string::iterator hb = h.begin(); hb != h.end(); ++hb)
if (*hb == ',') {
*hb = ' ';
++n;
}
if (!value.empty()) {
if (n > value.size()) {
throw create_exception<std::invalid_argument>("Expect only ", value.size(),
" coma separated values, but '",
svalue, "' provides ", n);
}
} else {
value.resize(n);
}
std::istringstream sval(h);
auto i = value.begin();
while (!sval.eof()) {
sval >> *i;
++i;
}
return sval.eof();
}
static size_t size(const std::vector<T>& /*value*/)
{
return 1;
}
static void apply(std::ostream& os, const std::vector<T>& value, bool required)
{
os << "=";
if (value.empty() && required)
os << "[required] ";
else {
for (auto i = value.begin(); i != value.end(); ++i) {
if (i != value.begin())
os << ",";
os << *i;
}
os << " ";
}
}
static const std::string get_as_string(const std::vector<T>& value)
{
std::ostringstream os;
for (auto i = value.begin(); i != value.end(); ++i) {
if (i != value.begin())
os << ",";
os << *i;
}
return os.str();
}
};
template <>
struct __dispatch_opt<bool> {
static void init(bool& value)
{
value = false;
}
static bool apply(const char */*svalue*/, bool& value)
{
value = true;
return true;
}
static size_t size(bool /*value*/)
{
return 0;
}
static void apply(std::ostream& /*os*/, bool /*value*/, bool /*required*/)
{
}
static const std::string get_as_string(const bool& value)
{
return value ? "true" : "false";
}
};
template <>
struct __dispatch_opt<std::string> {
static void init(std::string& /*value*/)
{
}
static bool apply(const char *svalue, std::string& value)
{
value = std::string(svalue);
return true;
}
static size_t size(std::string /*value*/)
{
return 1;
}
static void apply(std::ostream& os, const std::string& value, bool required)
{
if (value.empty())
if (required)
os << "[required] ";
else
os << "=NULL ";
else
os << "=" << value;
}
static const std::string get_as_string(const std::string& value)
{
return value;
}
};
//
// Implementation of the standard option that holds a value
//
template <typename T>
TCmdOption<T>::TCmdOption(T& val, char short_opt, const char *long_opt,
const char *long_help, const char *short_help,
CCmdOptionFlags flags):
CCmdOption(short_opt, long_opt, long_help, short_help, flags),
m_value(val)
{
__dispatch_opt<T>::init(m_value);
}
template <typename T>
bool TCmdOption<T>::do_set_value(const char *svalue)
{
return __dispatch_opt<T>::apply(svalue, m_value);
}
template <typename T>
size_t TCmdOption<T>::do_get_needed_args() const
{
return __dispatch_opt<T>::size(m_value);
}
template <typename T>
void TCmdOption<T>::do_write_value(std::ostream& os) const
{
__dispatch_opt<T>::apply( os, m_value, is_required());
}
template <typename T>
void TCmdOption<T>::do_get_long_help_xml(std::ostream& os, CXMLElement& parent,
HandlerHelpMap& /*handler_map*/) const
{
do_get_long_help(os);
xmlhelp_set_attribute(parent, "type", __type_descr<T>::value);
}
template <typename T>
const std::string TCmdOption<T>::do_get_value_as_string() const
{
return __dispatch_opt<T>::get_as_string(m_value);
}
template <typename T>
TRepeatableCmdOption<T>::TRepeatableCmdOption(std::vector<T>& val, char short_opt, const char *long_opt,
const char *long_help,
const char *short_help,
CCmdOptionFlags flags):
CCmdOption(short_opt, long_opt, long_help, short_help, flags),
m_value(val)
{
__dispatch_opt<std::vector<T>>::init(m_value);
}
template <typename T>
void TRepeatableCmdOption<T>::do_get_long_help_xml(std::ostream& os, CXMLElement& parent,
HandlerHelpMap& MIA_PARAM_UNUSED(handler_map)) const
{
do_get_long_help(os);
xmlhelp_set_attribute(parent, "type", __type_descr<T>::value);
xmlhelp_set_attribute(parent, "repeatable", "1");
}
template <typename T>
bool TRepeatableCmdOption<T>::do_set_value(const char *str_value)
{
T value;
bool good = __dispatch_opt<T>::apply(str_value, value);
if (good) {
m_value.push_back(value);
}
return good;
}
template <typename T>
size_t TRepeatableCmdOption<T>::do_get_needed_args() const
{
return 1;
}
template <typename T>
void TRepeatableCmdOption<T>::do_write_value(std::ostream& os) const
{
__dispatch_opt<std::vector<T>>::apply( os, m_value, is_required());
}
template <typename T>
const std::string TRepeatableCmdOption<T>::do_get_value_as_string() const
{
return __dispatch_opt<std::vector<T>>::get_as_string(m_value);
}
template <typename T>
PCmdOption make_opt(std::vector<T>& value, const char *long_opt, char short_opt,
const char *help, CCmdOptionFlags flags = CCmdOptionFlags::none)
{
return PCmdOption(new TCmdOption<std::vector<T>>(value, short_opt, long_opt, help,
long_opt, flags ));
}
template <typename T>
PCmdOption make_repeatable_opt(std::vector<T>& value, const char *long_opt, char short_opt,
const char *help, CCmdOptionFlags flags = CCmdOptionFlags::none)
{
return PCmdOption(new TRepeatableCmdOption<T>(value, short_opt, long_opt, help,
long_opt, flags ));
}
NS_MIA_END
#endif
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
CXMLElement
This class implements a facade for the xml Element.
Definition xmlinterface.hh:50
TCmdOption
Templated version based on CCmdOptionValue for values that can be converted to and from strings by st...
Definition cmdparamoption.hh:50
TCmdOption::TCmdOption
TCmdOption(T &val, char short_opt, const char *long_opt, const char *long_help, const char *short_help, CCmdOptionFlags flags=CCmdOptionFlags::none)
Definition cmdparamoption.hh:346
TRepeatableCmdOption
Templated version based on CCmdOptionValue for values that can be converted to and from strings by st...
Definition cmdparamoption.hh:95
TRepeatableCmdOption::TRepeatableCmdOption
TRepeatableCmdOption(std::vector< T > &val, char short_opt, const char *long_opt, const char *long_help, const char *short_help, CCmdOptionFlags flags=CCmdOptionFlags::none)
Definition cmdparamoption.hh:389
cmdoption.hh
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
NS_MIA_BEGIN
#define NS_MIA_BEGIN
conveniance define to start the mia namespace
Definition defines.hh:33
NS_MIA_END
#define NS_MIA_END
conveniance define to end the mia namespace
Definition defines.hh:36
make_repeatable_opt
PCmdOption make_repeatable_opt(std::vector< T > &value, const char *long_opt, char short_opt, const char *help, CCmdOptionFlags flags=CCmdOptionFlags::none)
Create a repeatable option to set a vector of values.
Definition cmdparamoption.hh:483
make_opt
PCmdOption make_opt(std::vector< T > &value, const char *long_opt, char short_opt, const char *help, CCmdOptionFlags flags=CCmdOptionFlags::none)
Create an option to set a vector of values,.
Definition cmdparamoption.hh:458
create_exception
E create_exception(T ...t)
helper template to create exceptions with complex messages
Definition errormacro.hh:80
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:45
handlerbase.hh
std
Definition gsl_iterator.hh:324
paramoption.hh
typedescr.hh