2012-12-07 17:27:10 +04:00
|
|
|
#ifndef parameters_parser_hh_INCLUDED
|
|
|
|
#define parameters_parser_hh_INCLUDED
|
|
|
|
|
|
|
|
#include "exception.hh"
|
2017-03-07 04:12:37 +03:00
|
|
|
#include "hash_map.hh"
|
2015-01-06 16:40:56 +03:00
|
|
|
#include "array_view.hh"
|
2014-10-23 21:55:45 +04:00
|
|
|
#include "flags.hh"
|
2015-03-14 22:16:46 +03:00
|
|
|
#include "optional.hh"
|
2013-04-09 22:05:40 +04:00
|
|
|
#include "string.hh"
|
2012-12-07 17:27:10 +04:00
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2015-03-09 16:48:41 +03:00
|
|
|
using ParameterList = ConstArrayView<String>;
|
2012-12-07 17:27:10 +04:00
|
|
|
|
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
|
|
|
{
|
2014-10-20 22:18:38 +04:00
|
|
|
unknown_option(StringView name)
|
2015-06-01 23:15:59 +03:00
|
|
|
: parameter_error(format("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
|
|
|
{
|
2014-10-20 22:18:38 +04:00
|
|
|
missing_option_value(StringView name)
|
2015-06-01 23:15:59 +03:00
|
|
|
: parameter_error(format("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
|
|
|
};
|
|
|
|
|
2014-02-12 02:23:44 +04:00
|
|
|
struct SwitchDesc
|
2014-02-12 02:16:17 +04:00
|
|
|
{
|
|
|
|
bool takes_arg;
|
|
|
|
String description;
|
|
|
|
};
|
|
|
|
|
2017-03-07 04:12:37 +03:00
|
|
|
using SwitchMap = HashMap<String, SwitchDesc, MemoryDomain::Commands>;
|
2014-02-12 02:16:17 +04:00
|
|
|
|
2014-02-12 02:23:44 +04:00
|
|
|
String generate_switches_doc(const SwitchMap& opts);
|
2013-03-27 17:27:12 +04:00
|
|
|
|
2014-02-08 05:02:58 +04:00
|
|
|
struct ParameterDesc
|
2012-12-07 17:27:10 +04:00
|
|
|
{
|
2013-04-02 16:22:55 +04:00
|
|
|
enum class Flags
|
|
|
|
{
|
|
|
|
None = 0,
|
2014-02-12 02:23:44 +04:00
|
|
|
SwitchesOnlyAtStart = 1,
|
2014-03-20 23:50:42 +04:00
|
|
|
SwitchesAsPositional = 2,
|
2013-04-02 16:22:55 +04:00
|
|
|
};
|
|
|
|
|
2014-02-08 05:02:58 +04:00
|
|
|
ParameterDesc() = default;
|
2014-02-12 02:23:44 +04:00
|
|
|
ParameterDesc(SwitchMap switches, Flags flags = Flags::None,
|
2014-02-08 05:02:58 +04:00
|
|
|
size_t min_positionals = 0, size_t max_positionals = -1)
|
2014-02-12 02:23:44 +04:00
|
|
|
: switches(std::move(switches)), flags(flags),
|
2014-02-08 05:02:58 +04:00
|
|
|
min_positionals(min_positionals), max_positionals(max_positionals) {}
|
|
|
|
|
2014-02-12 02:23:44 +04:00
|
|
|
SwitchMap switches;
|
2014-02-08 05:02:58 +04:00
|
|
|
Flags flags = Flags::None;
|
|
|
|
size_t min_positionals = 0;
|
|
|
|
size_t max_positionals = -1;
|
|
|
|
};
|
|
|
|
|
2014-10-23 21:55:45 +04:00
|
|
|
template<> struct WithBitOps<ParameterDesc::Flags> : std::true_type {};
|
|
|
|
|
2014-02-08 05:02:58 +04:00
|
|
|
// ParametersParser 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
|
|
|
|
{
|
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.
|
2014-02-08 05:02:58 +04:00
|
|
|
ParametersParser(ParameterList params, const ParameterDesc& desc);
|
2012-12-07 17:27:10 +04:00
|
|
|
|
2015-03-14 22:16:46 +03:00
|
|
|
// Return a valid optional if the switch was given, with
|
|
|
|
// a non empty StringView value if the switch took an argument.
|
|
|
|
Optional<StringView> get_switch(StringView name) const;
|
2012-12-07 17:27:10 +04:00
|
|
|
|
2015-03-12 16:58:31 +03:00
|
|
|
struct iterator : std::iterator<std::forward_iterator_tag, String>
|
2012-12-07 17:27:10 +04:00
|
|
|
{
|
|
|
|
iterator(const ParametersParser& parser, size_t index)
|
|
|
|
: m_parser(parser), m_index(index) {}
|
|
|
|
|
2015-03-12 16:58:31 +03:00
|
|
|
const String& operator*() const { return m_parser[m_index]; }
|
|
|
|
const String* operator->() const { return &m_parser[m_index]; }
|
2012-12-07 17:27:10 +04:00
|
|
|
|
2013-02-27 23:51:44 +04:00
|
|
|
iterator& operator++() { ++m_index; return *this; }
|
2014-06-12 23:03:17 +04:00
|
|
|
iterator operator++(int) { auto copy = *this; ++m_index; return copy; }
|
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
|
|
|
|
{
|
2015-03-12 16:58:31 +03:00
|
|
|
return not (*this == other);
|
2012-12-07 17:27:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const ParametersParser& m_parser;
|
|
|
|
size_t m_index;
|
|
|
|
};
|
|
|
|
|
2015-03-12 16:58:31 +03:00
|
|
|
// positional parameters count
|
|
|
|
size_t positional_count() const { return m_positional_indices.size(); }
|
|
|
|
|
2012-12-07 17:27:10 +04:00
|
|
|
// access positional parameter by index
|
2015-03-12 16:58:31 +03:00
|
|
|
const String& operator[] (size_t index) const
|
|
|
|
{
|
|
|
|
kak_assert(index < positional_count());
|
|
|
|
return m_params[m_positional_indices[index]];
|
|
|
|
}
|
|
|
|
|
|
|
|
iterator begin() const { return iterator(*this, 0); }
|
|
|
|
iterator end() const { return iterator(*this, m_positional_indices.size()); }
|
2012-12-07 17:27:10 +04:00
|
|
|
|
|
|
|
private:
|
2015-01-12 16:45:44 +03:00
|
|
|
ParameterList m_params;
|
|
|
|
Vector<size_t, MemoryDomain::Commands> m_positional_indices;
|
2014-02-08 05:02:58 +04:00
|
|
|
const ParameterDesc& m_desc;
|
2012-12-07 17:27:10 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // parameters_parser_hh_INCLUDED
|