Consistently name -foo things 'switches'

This commit is contained in:
Maxime Coste 2014-02-11 22:23:44 +00:00
parent 9451782648
commit 486d1269e0
5 changed files with 55 additions and 55 deletions

View File

@ -355,11 +355,11 @@ std::pair<String, String> CommandManager::command_info(const String& command_lin
return res; return res;
res.first = cmd->first; res.first = cmd->first;
auto& opts = cmd->second.param_desc.options; auto& switches = cmd->second.param_desc.switches;
if (not opts.empty()) if (not switches.empty())
{ {
res.second += "Flags:\n"; res.second += "Switches:\n";
res.second += generate_flags_doc(opts); res.second += generate_switches_doc(switches);
} }
return res; return res;

View File

@ -85,7 +85,7 @@ Buffer* open_fifo(const String& name , const String& filename, Context& context)
} }
static const ParameterDesc edit_params{ static const ParameterDesc edit_params{
OptionMap{ { "scratch", { false, "create a scratch buffer, not linked to a file" } }, SwitchMap{ { "scratch", { false, "create a scratch buffer, not linked to a file" } },
{ "fifo", { true, "create a buffer reading its content from a named fifo" } } }, { "fifo", { true, "create a buffer reading its content from a named fifo" } } },
ParameterDesc::Flags::None, 1, 3 ParameterDesc::Flags::None, 1, 3
}; };
@ -133,7 +133,7 @@ void edit(const ParametersParser& parser, Context& context)
} }
static const ParameterDesc write_params{ static const ParameterDesc write_params{
OptionMap{}, SwitchMap{},
ParameterDesc::Flags::None, 0, 1 ParameterDesc::Flags::None, 0, 1
}; };
@ -151,7 +151,7 @@ void write_buffer(const ParametersParser& parser, Context& context)
} }
static const ParameterDesc no_params{ static const ParameterDesc no_params{
OptionMap{}, SwitchMap{},
ParameterDesc::Flags::None, 0, 0 ParameterDesc::Flags::None, 0, 0
}; };
@ -200,7 +200,7 @@ void write_and_quit(const ParametersParser& parser, Context& context)
quit<force>(ParametersParser{memoryview<String>{}, no_params}, context); quit<force>(ParametersParser{memoryview<String>{}, no_params}, context);
} }
static const ParameterDesc single_name_params{ OptionMap{}, ParameterDesc::Flags::None, 1, 1 }; static const ParameterDesc single_name_params{ SwitchMap{}, ParameterDesc::Flags::None, 1, 1 };
void show_buffer(const ParametersParser& parser, Context& context) void show_buffer(const ParametersParser& parser, Context& context)
{ {
@ -214,7 +214,7 @@ void show_buffer(const ParametersParser& parser, Context& context)
} }
} }
static const ParameterDesc single_opt_name_params{ OptionMap{}, ParameterDesc::Flags::None, 0, 1 }; static const ParameterDesc single_opt_name_params{ SwitchMap{}, ParameterDesc::Flags::None, 0, 1 };
template<bool force> template<bool force>
void delete_buffer(const ParametersParser& parser, Context& context) void delete_buffer(const ParametersParser& parser, Context& context)
@ -243,7 +243,7 @@ void define_highlighter(const ParametersParser& parser, Context& context)
} }
static const ParameterDesc add_highlighter_params{ static const ParameterDesc add_highlighter_params{
OptionMap{ { "group", { true, "add highlighter to named group" } }, SwitchMap{ { "group", { true, "add highlighter to named group" } },
{ "def-group", { true, "add highlighter to reusable defined group" } } }, { "def-group", { true, "add highlighter to reusable defined group" } } },
ParameterDesc::Flags::None, 1 ParameterDesc::Flags::None, 1
}; };
@ -277,7 +277,7 @@ void add_highlighter(const ParametersParser& parser, Context& context)
} }
static const ParameterDesc rm_highlighter_params{ static const ParameterDesc rm_highlighter_params{
OptionMap{ { "group", { true, "remove highlighter from given group" } } }, SwitchMap{ { "group", { true, "remove highlighter from given group" } } },
ParameterDesc::Flags::None, 1, 1 ParameterDesc::Flags::None, 1, 1
}; };
@ -303,7 +303,7 @@ static HookManager& get_hook_manager(const String& scope, Context& context)
} }
static const ParameterDesc add_hook_params{ static const ParameterDesc add_hook_params{
OptionMap{ { "id", { true, "set hook id" } } }, ParameterDesc::Flags::None, 4, 4 SwitchMap{ { "id", { true, "set hook id" } } }, ParameterDesc::Flags::None, 4, 4
}; };
void add_hook(const ParametersParser& parser, Context& context) void add_hook(const ParametersParser& parser, Context& context)
@ -321,7 +321,7 @@ void add_hook(const ParametersParser& parser, Context& context)
} }
static const ParameterDesc rm_hooks_params{ static const ParameterDesc rm_hooks_params{
OptionMap{}, ParameterDesc::Flags::None, 2, 2 SwitchMap{}, ParameterDesc::Flags::None, 2, 2
}; };
void rm_hooks(const ParametersParser& parser, Context& context) void rm_hooks(const ParametersParser& parser, Context& context)
@ -350,7 +350,7 @@ std::vector<String> params_to_shell(const ParametersParser& parser)
} }
static const ParameterDesc define_command_params{ static const ParameterDesc define_command_params{
OptionMap{ { "env-params", { false, "pass parameters as env variables param0..paramN" } }, SwitchMap{ { "env-params", { false, "pass parameters as env variables param0..paramN" } },
{ "shell-params", { false, "pass parameters to each shell escape as $0..$N" } }, { "shell-params", { false, "pass parameters to each shell escape as $0..$N" } },
{ "allow-override", { false, "allow overriding existing command" } }, { "allow-override", { false, "allow overriding existing command" } },
{ "file-completion", { false, "complete parameters using filename completion" } }, { "file-completion", { false, "complete parameters using filename completion" } },
@ -378,7 +378,7 @@ void define_command(const ParametersParser& parser, Context& context)
ParameterDesc desc; ParameterDesc desc;
if (parser.has_option("env-params")) if (parser.has_option("env-params"))
{ {
desc = ParameterDesc{ OptionMap{}, ParameterDesc::Flags::None }; desc = ParameterDesc{ SwitchMap{}, ParameterDesc::Flags::None };
cmd = [=](const ParametersParser& parser, Context& context) { cmd = [=](const ParametersParser& parser, Context& context) {
CommandManager::instance().execute(commands, context, {}, CommandManager::instance().execute(commands, context, {},
params_to_env_var_map(parser)); params_to_env_var_map(parser));
@ -386,14 +386,14 @@ void define_command(const ParametersParser& parser, Context& context)
} }
if (parser.has_option("shell-params")) if (parser.has_option("shell-params"))
{ {
desc = ParameterDesc{ OptionMap{}, ParameterDesc::Flags::None }; desc = ParameterDesc{ SwitchMap{}, ParameterDesc::Flags::None };
cmd = [=](const ParametersParser& parser, Context& context) { cmd = [=](const ParametersParser& parser, Context& context) {
CommandManager::instance().execute(commands, context, params_to_shell(parser)); CommandManager::instance().execute(commands, context, params_to_shell(parser));
}; };
} }
else else
{ {
desc = ParameterDesc{ OptionMap{}, ParameterDesc::Flags::None, 0, 0 }; desc = ParameterDesc{ SwitchMap{}, ParameterDesc::Flags::None, 0, 0 };
cmd = [=](const ParametersParser& parser, Context& context) { cmd = [=](const ParametersParser& parser, Context& context) {
CommandManager::instance().execute(commands, context); CommandManager::instance().execute(commands, context);
}; };
@ -434,8 +434,8 @@ void define_command(const ParametersParser& parser, Context& context)
} }
static const ParameterDesc echo_message_params{ static const ParameterDesc echo_message_params{
OptionMap{ { "color", { true, "set message color" } } }, SwitchMap{ { "color", { true, "set message color" } } },
ParameterDesc::Flags::OptionsOnlyAtStart ParameterDesc::Flags::SwitchesOnlyAtStart
}; };
void echo_message(const ParametersParser& parser, Context& context) void echo_message(const ParametersParser& parser, Context& context)
@ -449,8 +449,8 @@ void echo_message(const ParametersParser& parser, Context& context)
} }
static const ParameterDesc write_debug_message_params{ static const ParameterDesc write_debug_message_params{
OptionMap{}, SwitchMap{},
ParameterDesc::Flags::OptionsOnlyAtStart ParameterDesc::Flags::SwitchesOnlyAtStart
}; };
void write_debug_message(const ParametersParser& parser, Context&) void write_debug_message(const ParametersParser& parser, Context&)
@ -462,7 +462,7 @@ void write_debug_message(const ParametersParser& parser, Context&)
} }
static const ParameterDesc exec_commands_in_file_params{ static const ParameterDesc exec_commands_in_file_params{
OptionMap{}, SwitchMap{},
ParameterDesc::Flags::None, ParameterDesc::Flags::None,
1, 1 1, 1
}; };
@ -498,8 +498,8 @@ static OptionManager& get_options(const String& scope, const Context& context)
static const ParameterDesc set_option_params{ static const ParameterDesc set_option_params{
OptionMap{ { "add", { false, "add to option rather than replacing it" } } }, SwitchMap{ { "add", { false, "add to option rather than replacing it" } } },
ParameterDesc::Flags::OptionsOnlyAtStart, ParameterDesc::Flags::SwitchesOnlyAtStart,
3, 3 3, 3
}; };
@ -513,8 +513,8 @@ void set_option(const ParametersParser& parser, Context& context)
} }
static const ParameterDesc declare_option_params{ static const ParameterDesc declare_option_params{
OptionMap{ { "hidden", { false, "do not display option name when completing" } } }, SwitchMap{ { "hidden", { false, "do not display option name when completing" } } },
ParameterDesc::Flags::OptionsOnlyAtStart, ParameterDesc::Flags::SwitchesOnlyAtStart,
2, 3 2, 3
}; };
@ -571,7 +571,7 @@ KeymapMode parse_keymap_mode(const String& str)
} }
static const ParameterDesc map_key_params{ static const ParameterDesc map_key_params{
OptionMap{}, ParameterDesc::Flags::None, 4, 4 SwitchMap{}, ParameterDesc::Flags::None, 4, 4
}; };
void map_key(const ParametersParser& parser, Context& context) void map_key(const ParametersParser& parser, Context& context)
@ -588,11 +588,11 @@ void map_key(const ParametersParser& parser, Context& context)
} }
const ParameterDesc context_wrap_params = { const ParameterDesc context_wrap_params = {
OptionMap{ { "client", { true, "run in given client context" } }, SwitchMap{ { "client", { true, "run in given client context" } },
{ "try-client", { true, "run in given client context if it exists, or else in the current one" } }, { "try-client", { true, "run in given client context if it exists, or else in the current one" } },
{ "draft", { false, "run in a disposable context" } }, { "draft", { false, "run in a disposable context" } },
{ "itersel", { false, "run once for each selection with that selection as the only one" } } }, { "itersel", { false, "run once for each selection with that selection as the only one" } } },
ParameterDesc::Flags::OptionsOnlyAtStart, 1 ParameterDesc::Flags::SwitchesOnlyAtStart, 1
}; };
template<typename Func> template<typename Func>
@ -667,7 +667,7 @@ void eval_string(const ParametersParser& parser, Context& context)
} }
static const ParameterDesc menu_params{ static const ParameterDesc menu_params{
OptionMap{ { "auto-single", { false, "instantly validate if only one item is available" } }, SwitchMap{ { "auto-single", { false, "instantly validate if only one item is available" } },
{ "select-cmds", { false, "each item specify an additional command to run when selected" } } } { "select-cmds", { false, "each item specify an additional command to run when selected" } } }
}; };
@ -707,7 +707,7 @@ void menu(const ParametersParser& parser, Context& context)
} }
static const ParameterDesc info_params{ static const ParameterDesc info_params{
OptionMap{ { "anchor", { true, "set info anchoring (left, right, or cursor)" } }, SwitchMap{ { "anchor", { true, "set info anchoring (left, right, or cursor)" } },
{ "title", { true, "set info title" } } }, { "title", { true, "set info title" } } },
ParameterDesc::Flags::None, 0, 1 ParameterDesc::Flags::None, 0, 1
}; };
@ -740,7 +740,7 @@ void info(const ParametersParser& parser, Context& context)
} }
static const ParameterDesc try_catch_params{ static const ParameterDesc try_catch_params{
OptionMap{}, ParameterDesc::Flags::None, 1, 3 SwitchMap{}, ParameterDesc::Flags::None, 1, 3
}; };
void try_catch(const ParametersParser& parser, Context& context) void try_catch(const ParametersParser& parser, Context& context)
@ -765,7 +765,7 @@ void try_catch(const ParametersParser& parser, Context& context)
} }
static const ParameterDesc define_color_alias_params{ static const ParameterDesc define_color_alias_params{
OptionMap{}, ParameterDesc::Flags::None, 2, 2 SwitchMap{}, ParameterDesc::Flags::None, 2, 2
}; };
void define_color_alias(const ParametersParser& parser, Context& context) void define_color_alias(const ParametersParser& parser, Context& context)
@ -774,7 +774,7 @@ void define_color_alias(const ParametersParser& parser, Context& context)
} }
static const ParameterDesc set_client_name_params{ static const ParameterDesc set_client_name_params{
OptionMap{}, ParameterDesc::Flags::None, 1, 1 SwitchMap{}, ParameterDesc::Flags::None, 1, 1
}; };
void set_client_name(const ParametersParser& parser, Context& context) void set_client_name(const ParametersParser& parser, Context& context)
@ -786,7 +786,7 @@ void set_client_name(const ParametersParser& parser, Context& context)
} }
static const ParameterDesc set_register_params{ static const ParameterDesc set_register_params{
OptionMap{}, ParameterDesc::Flags::None, 2, 2 SwitchMap{}, ParameterDesc::Flags::None, 2, 2
}; };
void set_register(const ParametersParser& parser, Context& context) void set_register(const ParametersParser& parser, Context& context)
@ -797,7 +797,7 @@ void set_register(const ParametersParser& parser, Context& context)
} }
static const ParameterDesc change_working_directory_params{ static const ParameterDesc change_working_directory_params{
OptionMap{}, ParameterDesc::Flags::None, 1, 1 SwitchMap{}, ParameterDesc::Flags::None, 1, 1
}; };
void change_working_directory(const ParametersParser& parser, Context&) void change_working_directory(const ParametersParser& parser, Context&)

View File

@ -360,7 +360,7 @@ int main(int argc, char* argv[])
params.push_back(argv[i]); params.push_back(argv[i]);
const ParameterDesc param_desc{ const ParameterDesc param_desc{
OptionMap{ { "c", { true, "connect to given session" } }, SwitchMap{ { "c", { true, "connect to given session" } },
{ "e", { true, "execute argument on initialisation" } }, { "e", { true, "execute argument on initialisation" } },
{ "n", { false, "do not source kakrc files on startup" } }, { "n", { false, "do not source kakrc files on startup" } },
{ "s", { true, "set session name" } }, { "s", { true, "set session name" } },
@ -373,9 +373,9 @@ int main(int argc, char* argv[])
catch (Kakoune::parameter_error& error) catch (Kakoune::parameter_error& error)
{ {
printf("Error: %s\n" printf("Error: %s\n"
"Valid options:\n" "Valid switches:\n"
"%s", "%s",
error.what(), generate_flags_doc(param_desc.options).c_str()); error.what(), generate_switches_doc(param_desc.switches).c_str());
return -1; return -1;
} }
catch (Kakoune::exception& error) catch (Kakoune::exception& error)

View File

@ -3,11 +3,11 @@
namespace Kakoune namespace Kakoune
{ {
String generate_flags_doc(const OptionMap& opts) String generate_switches_doc(const SwitchMap& switches)
{ {
String res; String res;
for (auto& opt : opts) for (auto& sw : switches)
res += " -" + opt.first + (opt.second.takes_arg ? " <arg>: " : ": ") + opt.second.description + "\n"; res += " -" + sw.first + (sw.second.takes_arg ? " <arg>: " : ": ") + sw.second.description + "\n";
return res; return res;
} }
@ -23,8 +23,8 @@ ParametersParser::ParametersParser(ParameterList params,
only_pos = true; only_pos = true;
else if (not only_pos and params[i][0] == '-') else if (not only_pos and params[i][0] == '-')
{ {
auto it = m_desc.options.find(params[i].substr(1_byte)); auto it = m_desc.switches.find(params[i].substr(1_byte));
if (it == m_desc.options.end()) if (it == m_desc.switches.end())
throw unknown_option(params[i]); throw unknown_option(params[i]);
if (it->second.takes_arg) if (it->second.takes_arg)
@ -36,7 +36,7 @@ ParametersParser::ParametersParser(ParameterList params,
} }
else else
{ {
if (desc.flags & ParameterDesc::Flags::OptionsOnlyAtStart) if (desc.flags & ParameterDesc::Flags::SwitchesOnlyAtStart)
only_pos = true; only_pos = true;
m_positional_indices.push_back(i); m_positional_indices.push_back(i);
} }
@ -48,7 +48,7 @@ ParametersParser::ParametersParser(ParameterList params,
bool ParametersParser::has_option(const String& name) const bool ParametersParser::has_option(const String& name) const
{ {
kak_assert(m_desc.options.find(name) != m_desc.options.end()); kak_assert(m_desc.switches.find(name) != m_desc.switches.end());
for (auto& param : m_params) for (auto& param : m_params)
{ {
if (param[0] == '-' and param.substr(1_byte) == name) if (param[0] == '-' and param.substr(1_byte) == name)
@ -63,8 +63,8 @@ bool ParametersParser::has_option(const String& name) const
const String& ParametersParser::option_value(const String& name) const const String& ParametersParser::option_value(const String& name) const
{ {
#ifdef KAK_DEBUG #ifdef KAK_DEBUG
auto it = m_desc.options.find(name); auto it = m_desc.switches.find(name);
kak_assert(it != m_desc.options.end()); kak_assert(it != m_desc.switches.end());
kak_assert(it->second.takes_arg); kak_assert(it->second.takes_arg);
#endif #endif

View File

@ -34,22 +34,22 @@ struct wrong_argument_count : public parameter_error
wrong_argument_count() : parameter_error("wrong argument count") {} wrong_argument_count() : parameter_error("wrong argument count") {}
}; };
struct OptionDesc struct SwitchDesc
{ {
bool takes_arg; bool takes_arg;
String description; String description;
}; };
using OptionMap = std::unordered_map<String, OptionDesc>; using SwitchMap = std::unordered_map<String, SwitchDesc>;
String generate_flags_doc(const OptionMap& opts); String generate_switches_doc(const SwitchMap& opts);
struct ParameterDesc struct ParameterDesc
{ {
enum class Flags enum class Flags
{ {
None = 0, None = 0,
OptionsOnlyAtStart = 1, SwitchesOnlyAtStart = 1,
}; };
friend constexpr Flags operator|(Flags lhs, Flags rhs) friend constexpr Flags operator|(Flags lhs, Flags rhs)
{ {
@ -61,12 +61,12 @@ struct ParameterDesc
} }
ParameterDesc() = default; ParameterDesc() = default;
ParameterDesc(OptionMap options, Flags flags = Flags::None, ParameterDesc(SwitchMap switches, Flags flags = Flags::None,
size_t min_positionals = 0, size_t max_positionals = -1) size_t min_positionals = 0, size_t max_positionals = -1)
: options(std::move(options)), flags(flags), : switches(std::move(switches)), flags(flags),
min_positionals(min_positionals), max_positionals(max_positionals) {} min_positionals(min_positionals), max_positionals(max_positionals) {}
OptionMap options; SwitchMap switches;
Flags flags = Flags::None; Flags flags = Flags::None;
size_t min_positionals = 0; size_t min_positionals = 0;
size_t max_positionals = -1; size_t max_positionals = -1;