2012-12-07 17:27:10 +04:00
|
|
|
#ifndef parameters_parser_hh_INCLUDED
|
|
|
|
#define parameters_parser_hh_INCLUDED
|
|
|
|
|
|
|
|
#include "exception.hh"
|
2013-04-09 22:05:40 +04:00
|
|
|
#include "memoryview.hh"
|
|
|
|
#include "string.hh"
|
2012-12-07 17:27:10 +04:00
|
|
|
|
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
|
|
|
using ParameterList = memoryview<String>;
|
|
|
|
|
2014-01-23 23:36:07 +04:00
|
|
|
struct parameter_error : public runtime_error
|
|
|
|
{
|
|
|
|
using runtime_error::runtime_error;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct unknown_option : public parameter_error
|
2012-12-07 17:27:10 +04:00
|
|
|
{
|
|
|
|
unknown_option(const String& name)
|
2014-01-23 23:36:07 +04:00
|
|
|
: parameter_error("unknown option '" + name + "'") {}
|
2012-12-07 17:27:10 +04:00
|
|
|
};
|
|
|
|
|
2014-01-23 23:36:07 +04:00
|
|
|
struct missing_option_value: public parameter_error
|
2012-12-07 17:27:10 +04:00
|
|
|
{
|
|
|
|
missing_option_value(const String& name)
|
2014-01-23 23:36:07 +04:00
|
|
|
: parameter_error("missing value for option '" + name + "'") {}
|
2012-12-07 17:27:10 +04:00
|
|
|
};
|
|
|
|
|
2014-01-23 23:36:07 +04:00
|
|
|
struct wrong_argument_count : public parameter_error
|
2013-03-27 17:27:12 +04:00
|
|
|
{
|
2014-01-23 23:36:07 +04:00
|
|
|
wrong_argument_count() : parameter_error("wrong argument count") {}
|
2013-03-27 17:27:12 +04:00
|
|
|
};
|
|
|
|
|
2013-04-03 20:51:40 +04:00
|
|
|
using OptionMap = std::unordered_map<String, bool>;
|
2013-03-27 17:27:12 +04:00
|
|
|
|
2012-12-07 17:27:10 +04:00
|
|
|
// ParameterParser provides tools to parse command parameters.
|
|
|
|
// There are 3 types of parameters:
|
|
|
|
// * unnamed options, which are accessed by position (ignoring named ones)
|
|
|
|
// * named boolean options, which are enabled using '-name' syntax
|
|
|
|
// * named string options, which are defined using '-name value' syntax
|
|
|
|
struct ParametersParser
|
|
|
|
{
|
2013-04-02 16:22:55 +04:00
|
|
|
enum class Flags
|
|
|
|
{
|
|
|
|
None = 0,
|
|
|
|
OptionsOnlyAtStart = 1,
|
|
|
|
};
|
|
|
|
friend constexpr Flags operator|(Flags lhs, Flags rhs)
|
|
|
|
{
|
|
|
|
return (Flags)((int) lhs | (int) rhs);
|
|
|
|
}
|
|
|
|
friend constexpr bool operator&(Flags lhs, Flags rhs)
|
|
|
|
{
|
|
|
|
return ((int) lhs & (int) rhs) != 0;
|
|
|
|
}
|
|
|
|
|
2012-12-07 17:27:10 +04:00
|
|
|
// the options defines named options, if they map to true, then
|
|
|
|
// they are understood as string options, else they are understood as
|
|
|
|
// boolean option.
|
|
|
|
ParametersParser(const ParameterList& params,
|
2013-04-03 20:51:40 +04:00
|
|
|
OptionMap options,
|
2013-04-02 16:22:55 +04:00
|
|
|
Flags flags = Flags::None,
|
2013-03-27 17:27:12 +04:00
|
|
|
size_t min_positionals = 0,
|
|
|
|
size_t max_positionals = -1);
|
2012-12-07 17:27:10 +04:00
|
|
|
|
|
|
|
// check if a named option (either string or boolean) is specified
|
|
|
|
bool has_option(const String& name) const;
|
|
|
|
|
|
|
|
// get a string option value, returns an empty string if the option
|
|
|
|
// is not defined
|
|
|
|
const String& option_value(const String& name) const;
|
|
|
|
|
|
|
|
// positional parameters count
|
|
|
|
size_t positional_count() const;
|
|
|
|
|
|
|
|
struct iterator
|
|
|
|
{
|
|
|
|
public:
|
2014-01-09 23:50:01 +04:00
|
|
|
using value_type = String;
|
|
|
|
using pointer = const value_type*;
|
|
|
|
using reference = const value_type&;
|
|
|
|
using difference_type = size_t;
|
|
|
|
using iterator_category = std::forward_iterator_tag;
|
2012-12-07 17:27:10 +04:00
|
|
|
|
|
|
|
iterator(const ParametersParser& parser, size_t index)
|
|
|
|
: m_parser(parser), m_index(index) {}
|
|
|
|
|
|
|
|
const String& operator*() const
|
|
|
|
{
|
2013-02-27 23:51:44 +04:00
|
|
|
return m_parser.m_params[m_parser.m_positional_indices[m_index]];
|
2012-12-07 17:27:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
const String* operator->() const
|
|
|
|
{
|
2013-02-27 23:51:44 +04:00
|
|
|
return &m_parser.m_params[m_parser.m_positional_indices[m_index]];
|
2012-12-07 17:27:10 +04:00
|
|
|
}
|
|
|
|
|
2013-02-27 23:51:44 +04:00
|
|
|
iterator& operator++() { ++m_index; return *this; }
|
2012-12-07 17:27:10 +04:00
|
|
|
|
|
|
|
bool operator==(const iterator& other) const
|
|
|
|
{
|
2013-04-09 22:04:11 +04:00
|
|
|
kak_assert(&m_parser == &other.m_parser);
|
2013-02-27 23:51:44 +04:00
|
|
|
return m_index == other.m_index;
|
2012-12-07 17:27:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const iterator& other) const
|
|
|
|
{
|
2013-04-09 22:04:11 +04:00
|
|
|
kak_assert(&m_parser == &other.m_parser);
|
2013-02-27 23:51:44 +04:00
|
|
|
return m_index != other.m_index;
|
2012-12-07 17:27:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator<(const iterator& other) const
|
|
|
|
{
|
2013-04-09 22:04:11 +04:00
|
|
|
kak_assert(&m_parser == &other.m_parser);
|
2012-12-07 17:27:10 +04:00
|
|
|
return m_index < other.m_index;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const ParametersParser& m_parser;
|
|
|
|
size_t m_index;
|
|
|
|
};
|
|
|
|
|
|
|
|
// access positional parameter by index
|
|
|
|
const String& operator[] (size_t index) const;
|
|
|
|
// positional parameter begin
|
|
|
|
iterator begin() const;
|
|
|
|
// positional parameter end
|
|
|
|
iterator end() const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
ParameterList m_params;
|
2013-02-27 23:51:44 +04:00
|
|
|
std::vector<size_t> m_positional_indices;
|
2012-12-07 17:27:10 +04:00
|
|
|
std::unordered_map<String, bool> m_options;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // parameters_parser_hh_INCLUDED
|
|
|
|
|