Commit 7d2aa33c authored by Davis King's avatar Davis King

Added the get_option() routines which slightly simplify getting options

from command line parsers and config readers.
parent b9d92535
......@@ -9,6 +9,7 @@
#include "cmd_line_parser/cmd_line_parser_check_1.h"
#include "cmd_line_parser/cmd_line_parser_check_c.h"
#include <string>
#include "cmd_line_parser/get_option.h"
#include "map.h"
#include "sequence.h"
......
// Copyright (C) 2012 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_GET_OPTiON_H__
#define DLIB_GET_OPTiON_H__
#include "get_option_abstract.h"
#include "../string.h"
#include "../is_kind.h"
namespace dlib
{
// ----------------------------------------------------------------------------------------
class option_parse_error : public error
{
public:
option_parse_error(const std::string& option_string, const std::string& str):
error(EOPTION_PARSE,"Error parsing argument for option '" + option_string + "', offending string is '" + str + "'.") {}
};
// ----------------------------------------------------------------------------------------
template <typename config_reader_type, typename T>
T impl_config_reader_get_option (
const config_reader_type& cr,
const std::string& option_name,
const std::string& full_option_name,
T default_value
)
{
std::string::size_type pos = option_name.find_first_of(".");
if (pos == std::string::npos)
{
if (cr.is_key_defined(option_name))
{
try{ return string_cast<T>(cr[option_name]); }
catch (string_cast_error&) { throw option_parse_error(full_option_name, cr[option_name]); }
}
}
else
{
std::string block_name = option_name.substr(0,pos);
if (cr.is_block_defined(block_name))
{
return impl_config_reader_get_option(cr.block(block_name),
option_name.substr(pos+1),
full_option_name,
default_value);
}
}
return default_value;
}
// ----------------------------------------------------------------------------------------
template <typename cr_type, typename T>
typename enable_if<is_config_reader<cr_type>,T>::type get_option (
const cr_type& cr,
const std::string& option_name,
T default_value
)
{
return impl_config_reader_get_option(cr, option_name, option_name, default_value);
}
// ----------------------------------------------------------------------------------------
template <typename parser_type, typename T>
typename disable_if<is_config_reader<parser_type>,T>::type get_option (
const parser_type& parser,
const std::string& option_name,
T default_value
)
{
// make sure requires clause is not broken
DLIB_ASSERT( parser.option_is_defined(option_name) == true &&
parser.option(option_name).number_of_arguments() == 1,
"\t T get_option()"
<< "\n\t option_name: " << option_name
<< "\n\t parser.option_is_defined(option_name): " << parser.option_is_defined(option_name)
<< "\n\t parser.option(option_name).number_of_arguments(): " << parser.option(option_name).number_of_arguments()
);
if (parser.option(option_name))
{
try
{
default_value = string_cast<T>(parser.option(option_name).argument());
}
catch (string_cast_error&)
{
throw option_parse_error(option_name, parser.option(option_name).argument());
}
}
return default_value;
}
// ----------------------------------------------------------------------------------------
template <typename parser_type, typename cr_type, typename T>
typename disable_if<is_config_reader<parser_type>,T>::type get_option (
const parser_type& parser,
const cr_type& cr,
const std::string& option_name,
T default_value
)
{
// make sure requires clause is not broken
DLIB_ASSERT( parser.option_is_defined(option_name) == true &&
parser.option(option_name).number_of_arguments() == 1,
"\t T get_option()"
<< "\n\t option_name: " << option_name
<< "\n\t parser.option_is_defined(option_name): " << parser.option_is_defined(option_name)
<< "\n\t parser.option(option_name).number_of_arguments(): " << parser.option(option_name).number_of_arguments()
);
if (parser.option(option_name))
return get_option(parser, option_name, default_value);
else
return get_option(cr, option_name, default_value);
}
// ----------------------------------------------------------------------------------------
template <typename parser_type, typename cr_type, typename T>
typename disable_if<is_config_reader<parser_type>,T>::type get_option (
const cr_type& cr,
const parser_type& parser,
const std::string& option_name,
T default_value
)
{
// make sure requires clause is not broken
DLIB_ASSERT( parser.option_is_defined(option_name) == true &&
parser.option(option_name).number_of_arguments() == 1,
"\t T get_option()"
<< "\n\t option_name: " << option_name
<< "\n\t parser.option_is_defined(option_name): " << parser.option_is_defined(option_name)
<< "\n\t parser.option(option_name).number_of_arguments(): " << parser.option(option_name).number_of_arguments()
);
if (parser.option(option_name))
return get_option(parser, option_name, default_value);
else
return get_option(cr, option_name, default_value);
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
template <typename T>
inline std::string get_option (
const T& cr,
const std::string& option_name,
const char* default_value
)
{
return get_option(cr, option_name, std::string(default_value));
}
// ----------------------------------------------------------------------------------------
template <typename T, typename U>
inline std::string get_option (
const T& parser,
const U& cr,
const std::string& option_name,
const char* default_value
)
{
return get_option(parser, cr, std::string(default_value));
}
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_GET_OPTiON_H__
// Copyright (C) 2012 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#undef DLIB_GET_OPTiON_ABSTRACT_H__
#ifdef DLIB_GET_OPTiON_ABSTRACT_H__
#inclue <string>
namespace dlib
{
// ----------------------------------------------------------------------------------------
class option_parse_error : public error
{
/*!
WHAT THIS OBJECT REPRESENTS
This is the exception thrown by the get_option() functions. It is
thrown when the option string given by a command line parser or
config reader can't be converted into the type T.
!*/
};
// ----------------------------------------------------------------------------------------
template <
typename config_reader_type,
typename T
>
T get_option (
const config_reader_type& cr,
const std::string& option_name,
T default_value
);
/*!
requires
- T is a type which can be read from an input stream
- config_reader_type == an implementation of config_reader/config_reader_kernel_abstract.h
ensures
- option_name is used to index into the given config_reader.
- if (cr contains an entry corresponding to option_name) then
- converts the string value in cr corresponding to option_name into
an object of type T and returns it.
- else
- returns default_value
- The scheme for indexing into cr based on option_name is best
understood by looking at a few examples:
- an option name of "name" corresponds to cr["name"]
- an option name of "block1.name" corresponds to cr.block("block1")["name"]
- an option name of "block1.block2.name" corresponds to cr.block("block1").block("block2")["name"]
throws
- option_parse_error
This exception is thrown if we attempt but fail to convert the string value
in cr into an object of type T.
!*/
// ----------------------------------------------------------------------------------------
template <
typename command_line_parser_type,
typename T
>
T get_option (
const command_line_parser_type& parser,
const std::string& option_name,
T default_value
);
/*!
requires
- parser.option_is_defined(option_name) == true
- parser.option(option_name).number_of_arguments() == 1
- T is a type which can be read from an input stream
- command_line_parser_type == an implementation of cmd_line_parser/cmd_line_parser_kernel_abstract.h
ensures
- if (parser.option(option_name)) then
- converts parser.option(option_name).argument() into an object
of type T and returns it. That is, the string argument to this
command line option is converted into a T and returned.
- else
- returns default_value
throws
- option_parse_error
This exception is thrown if we attempt but fail to convert the string
argument into an object of type T.
!*/
// ----------------------------------------------------------------------------------------
template <
typename command_line_parser_type,
typename config_reader_type,
typename T
>
T get_option (
const command_line_parser_type& parser,
const config_reader_type& cr,
const std::string& option_name,
T default_value
);
/*!
requires
- parser.option_is_defined(option_name) == true
- parser.option(option_name).number_of_arguments() == 1
- T is a type which can be read from an input stream
- command_line_parser_type == an implementation of cmd_line_parser/cmd_line_parser_kernel_abstract.h
- config_reader_type == an implementation of config_reader/config_reader_kernel_abstract.h
ensures
- if (parser.option(option_name)) then
- returns get_option(parser, option_name, default_value)
- else
- returns get_option(cr, option_name, default_value)
!*/
// ----------------------------------------------------------------------------------------
template <
typename command_line_parser_type,
typename config_reader_type,
typename T
>
T get_option (
const config_reader_type& cr,
const command_line_parser_type& parser,
const std::string& option_name,
T default_value
);
/*!
requires
- parser.option_is_defined(option_name) == true
- parser.option(option_name).number_of_arguments() == 1
- T is a type which can be read from an input stream
- command_line_parser_type == an implementation of cmd_line_parser/cmd_line_parser_kernel_abstract.h
- config_reader_type == an implementation of config_reader/config_reader_kernel_abstract.h
ensures
- if (parser.option(option_name)) then
- returns get_option(parser, option_name, default_value)
- else
- returns get_option(cr, option_name, default_value)
!*/
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_GET_OPTiON_ABSTRACT_H__
......@@ -6,6 +6,7 @@
#include "config_reader/config_reader_kernel_1.h"
#include "map.h"
#include "tokenizer.h"
#include "cmd_line_parser/get_option.h"
#include "algs.h"
#include "is_kind.h"
......
......@@ -52,7 +52,8 @@ namespace dlib
EIMAGE_SAVE,
ECAST_TO_STRING,
ESTRING_CAST,
EUTF8_TO_UTF32
EUTF8_TO_UTF32,
EOPTION_PARSE
};
// ----------------------------------------------------------------------------------------
......@@ -165,6 +166,7 @@ namespace dlib
else if ( type == ECAST_TO_STRING) return "ECAST_TO_STRING";
else if ( type == ESTRING_CAST) return "ESTRING_CAST";
else if ( type == EUTF8_TO_UTF32) return "EUTF8_TO_UTF32";
else if ( type == EOPTION_PARSE) return "EOPTION_PARSE";
else return "undefined error type";
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment