Move parameter count validation to the ParameterParser

This commit is contained in:
Maxime Coste 2013-03-27 14:27:12 +01:00
parent 3b059bafe5
commit 29f79346c3
3 changed files with 27 additions and 42 deletions

View File

@ -31,11 +31,6 @@ namespace Kakoune
namespace namespace
{ {
struct wrong_argument_count : runtime_error
{
wrong_argument_count() : runtime_error("wrong argument count") {}
};
Buffer* open_or_create(const String& filename, Context& context) Buffer* open_or_create(const String& filename, Context& context)
{ {
Buffer* buffer = create_buffer_from_file(filename); Buffer* buffer = create_buffer_from_file(filename);
@ -92,11 +87,7 @@ template<bool force_reload>
void edit(const CommandParameters& params, Context& context) void edit(const CommandParameters& params, Context& context)
{ {
ParametersParser parser(params, { { "scratch", false }, ParametersParser parser(params, { { "scratch", false },
{ "fifo", true } }); { "fifo", true } }, 1, 3);
const size_t param_count = parser.positional_count();
if (param_count == 0 or param_count > 3)
throw wrong_argument_count();
const String name = parser[0]; const String name = parser[0];
@ -115,6 +106,7 @@ void edit(const CommandParameters& params, Context& context)
BufferManager::instance().set_last_used_buffer(*buffer); BufferManager::instance().set_last_used_buffer(*buffer);
const size_t param_count = parser.positional_count();
if (buffer != &context.buffer() or param_count > 1) if (buffer != &context.buffer() or param_count > 1)
context.push_jump(); context.push_jump();
@ -254,10 +246,7 @@ Group& get_group(Group& root, const String& group_path)
void add_highlighter(const CommandParameters& params, Context& context) void add_highlighter(const CommandParameters& params, Context& context)
{ {
ParametersParser parser(params, { { "group", true } }); ParametersParser parser(params, { { "group", true } }, 1);
if (parser.positional_count() < 1)
throw wrong_argument_count();
HighlighterRegistry& registry = HighlighterRegistry::instance(); HighlighterRegistry& registry = HighlighterRegistry::instance();
auto begin = parser.begin(); auto begin = parser.begin();
@ -277,9 +266,7 @@ void add_highlighter(const CommandParameters& params, Context& context)
void rm_highlighter(const CommandParameters& params, Context& context) void rm_highlighter(const CommandParameters& params, Context& context)
{ {
ParametersParser parser(params, { { "group", true } }); ParametersParser parser(params, { { "group", true } }, 1, 1);
if (parser.positional_count() != 1)
throw wrong_argument_count();
Window& window = context.window(); Window& window = context.window();
HighlighterGroup& group = parser.has_option("group") ? HighlighterGroup& group = parser.has_option("group") ?
@ -291,9 +278,7 @@ void rm_highlighter(const CommandParameters& params, Context& context)
void add_filter(const CommandParameters& params, Context& context) void add_filter(const CommandParameters& params, Context& context)
{ {
ParametersParser parser(params, { { "group", true } }); ParametersParser parser(params, { { "group", true } }, 1);
if (parser.positional_count() < 1)
throw wrong_argument_count();
FilterRegistry& registry = FilterRegistry::instance(); FilterRegistry& registry = FilterRegistry::instance();
@ -314,9 +299,7 @@ void add_filter(const CommandParameters& params, Context& context)
void rm_filter(const CommandParameters& params, Context& context) void rm_filter(const CommandParameters& params, Context& context)
{ {
ParametersParser parser(params, { { "group", true } }); ParametersParser parser(params, { { "group", true } }, 1, 1);
if (parser.positional_count() != 1)
throw wrong_argument_count();
Editor& editor = context.editor(); Editor& editor = context.editor();
FilterGroup& group = parser.has_option("group") ? FilterGroup& group = parser.has_option("group") ?
@ -371,10 +354,8 @@ void define_command(const CommandParameters& params, Context& context)
{ "shell-params", false }, { "shell-params", false },
{ "allow-override", false }, { "allow-override", false },
{ "file-completion", false }, { "file-completion", false },
{ "shell-completion", true } }); { "shell-completion", true } },
2, 2);
if (parser.positional_count() != 2)
throw wrong_argument_count();
auto begin = parser.begin(); auto begin = parser.begin();
const String& cmd_name = *begin; const String& cmd_name = *begin;
@ -510,9 +491,7 @@ void declare_option(const CommandParameters& params, Context& context)
template<typename Func> template<typename Func>
void context_wrap(const CommandParameters& params, Context& context, Func func) void context_wrap(const CommandParameters& params, Context& context, Func func)
{ {
ParametersParser parser(params, { { "client", true }, { "draft", false }}); ParametersParser parser(params, { { "client", true }, { "draft", false }}, 1);
if (parser.positional_count() == 0)
throw wrong_argument_count();
Context& real_context = parser.has_option("client") ? Context& real_context = parser.has_option("client") ?
ClientManager::instance().get_client_context(parser.option_value("client")) ClientManager::instance().get_client_context(parser.option_value("client"))
@ -663,10 +642,7 @@ static String assist(String message, CharCount maxWidth)
void info(const CommandParameters& params, Context& context) void info(const CommandParameters& params, Context& context)
{ {
ParametersParser parser(params, { { "anchor", true }, { "assist", false } }); ParametersParser parser(params, { { "anchor", true }, { "assist", false } }, 0, 1);
if (parser.positional_count() > 1)
throw wrong_argument_count();
context.ui().info_hide(); context.ui().info_hide();
if (parser.positional_count() > 0) if (parser.positional_count() > 0)
@ -713,18 +689,14 @@ void try_catch(const CommandParameters& params, Context& context)
void define_color_alias(const CommandParameters& params, Context& context) void define_color_alias(const CommandParameters& params, Context& context)
{ {
ParametersParser parser(params, {}); ParametersParser parser(params, {}, 2, 2);
if (parser.positional_count() != 2)
throw wrong_argument_count();
ColorRegistry::instance().register_alias( ColorRegistry::instance().register_alias(
parser[0], parser[1], true); parser[0], parser[1], true);
} }
void set_client_name(const CommandParameters& params, Context& context) void set_client_name(const CommandParameters& params, Context& context)
{ {
ParametersParser parser(params, {}); ParametersParser parser(params, {}, 1, 1);
if (parser.positional_count() != 1)
throw wrong_argument_count();
ClientManager::instance().set_client_name(context, params[0]); ClientManager::instance().set_client_name(context, params[0]);
} }

View File

@ -4,7 +4,9 @@ namespace Kakoune
{ {
ParametersParser::ParametersParser(const ParameterList& params, ParametersParser::ParametersParser(const ParameterList& params,
std::unordered_map<String, bool> options) std::unordered_map<String, bool> options,
size_t min_positionals,
size_t max_positionals)
: m_params(params), : m_params(params),
m_options(std::move(options)) m_options(std::move(options))
{ {
@ -29,6 +31,9 @@ ParametersParser::ParametersParser(const ParameterList& params,
else else
m_positional_indices.push_back(i); m_positional_indices.push_back(i);
} }
size_t count = m_positional_indices.size();
if (count > max_positionals or count < min_positionals)
throw wrong_argument_count();
} }
bool ParametersParser::has_option(const String& name) const bool ParametersParser::has_option(const String& name) const

View File

@ -24,6 +24,12 @@ struct missing_option_value: public runtime_error
: runtime_error("missing value for option '" + name + "'") {} : runtime_error("missing value for option '" + name + "'") {}
}; };
struct wrong_argument_count : runtime_error
{
wrong_argument_count() : runtime_error("wrong argument count") {}
};
// ParameterParser provides tools to parse command parameters. // ParameterParser provides tools to parse command parameters.
// There are 3 types of parameters: // There are 3 types of parameters:
// * unnamed options, which are accessed by position (ignoring named ones) // * unnamed options, which are accessed by position (ignoring named ones)
@ -35,7 +41,9 @@ struct ParametersParser
// they are understood as string options, else they are understood as // they are understood as string options, else they are understood as
// boolean option. // boolean option.
ParametersParser(const ParameterList& params, ParametersParser(const ParameterList& params,
std::unordered_map<String, bool> options); std::unordered_map<String, bool> options,
size_t min_positionals = 0,
size_t max_positionals = -1);
// check if a named option (either string or boolean) is specified // check if a named option (either string or boolean) is specified
bool has_option(const String& name) const; bool has_option(const String& name) const;