Pass tokensplitter& as argument to command handler

This commit is contained in:
Ilari Liusvaara 2011-09-17 13:15:20 +03:00
parent 9564054f27
commit 6a823be7d1
5 changed files with 33 additions and 39 deletions

View file

@ -36,10 +36,9 @@ namespace
messages << "alias " << i->first << " " << *j << std::endl;
});
function_ptr_command<const std::string&> unalias_command("unalias-command", "unalias a command",
function_ptr_command<tokensplitter&> unalias_command("unalias-command", "unalias a command",
"Syntax: unalias-command <aliasname>\nClear expansion of alias <aliasname>\n",
[](const std::string& args) throw(std::bad_alloc, std::runtime_error) {
tokensplitter t(args);
[](tokensplitter& t) throw(std::bad_alloc, std::runtime_error) {
std::string aliasname = t;
if(t)
throw std::runtime_error("This command only takes one argument");
@ -49,11 +48,10 @@ namespace
messages << "Command '" << aliasname << "' unaliased" << std::endl;
});
function_ptr_command<const std::string&> alias_command("alias-command", "alias a command",
function_ptr_command<tokensplitter&> alias_command("alias-command", "alias a command",
"Syntax: alias-command <aliasname> <command>\nAppend <command> to expansion of alias <aliasname>\n"
"Valid alias names can't be empty nor start with '*' or '?'\n",
[](const std::string& args) throw(std::bad_alloc, std::runtime_error) {
tokensplitter t(args);
[](tokensplitter& t) throw(std::bad_alloc, std::runtime_error) {
std::string aliasname = t;
std::string command = t.tail();
if(command == "")
@ -241,3 +239,10 @@ void invoke_command_fn(void (*fn)(struct arg_filename a), const std::string& arg
b.v = args;
fn(b);
}
template<>
void invoke_command_fn(void (*fn)(tokensplitter& a), const std::string& args)
{
tokensplitter t(args);
fn(t);
}

View file

@ -11,12 +11,11 @@
namespace
{
function_ptr_command<const std::string&> bind_key("bind-key", "Bind a (pseudo-)key",
function_ptr_command<tokensplitter&> bind_key("bind-key", "Bind a (pseudo-)key",
"Syntax: bind-key [<mod>/<modmask>] <key> <command>\nBind command to specified key (with specified "
" modifiers)\n",
[](const std::string& args) throw(std::bad_alloc, std::runtime_error) {
[](tokensplitter& t) throw(std::bad_alloc, std::runtime_error) {
std::string mod, modmask, keyname, command;
tokensplitter t(args);
std::string mod_or_key = t;
if(mod_or_key.find_first_of("/") < mod_or_key.length()) {
//Mod field.
@ -34,11 +33,10 @@ namespace
window::bind(mod, modmask, keyname, command);
});
function_ptr_command<const std::string&> unbind_key("unbind-key", "Unbind a (pseudo-)key",
function_ptr_command<tokensplitter&> unbind_key("unbind-key", "Unbind a (pseudo-)key",
"Syntax: unbind-key [<mod>/<modmask>] <key>\nUnbind specified key (with specified modifiers)\n",
[](const std::string& args) throw(std::bad_alloc, std::runtime_error) {
[](tokensplitter& t) throw(std::bad_alloc, std::runtime_error) {
std::string mod, modmask, keyname, command;
tokensplitter t(args);
std::string mod_or_key = t;
if(mod_or_key.find_first_of("/") < mod_or_key.length()) {
//Mod field.

View file

@ -614,10 +614,9 @@ namespace
redraw_framebuffer();
});
function_ptr_command<const std::string&> add_watch("add-watch", "Add a memory watch",
function_ptr_command<tokensplitter&> add_watch("add-watch", "Add a memory watch",
"Syntax: add-watch <name> <expression>\nAdds a new memory watch\n",
[](const std::string& args) throw(std::bad_alloc, std::runtime_error) {
tokensplitter t(args);
[](tokensplitter& t) throw(std::bad_alloc, std::runtime_error) {
std::string name = t;
if(name == "" || t.tail() == "")
throw std::runtime_error("syntax: add-watch <name> <expr>");
@ -626,10 +625,9 @@ namespace
update_movie_state();
});
function_ptr_command<const std::string&> remove_watch("remove-watch", "Remove a memory watch",
function_ptr_command<tokensplitter&> remove_watch("remove-watch", "Remove a memory watch",
"Syntax: remove-watch <name>\nRemoves a memory watch\n",
[](const std::string& args) throw(std::bad_alloc, std::runtime_error) {
tokensplitter t(args);
[](tokensplitter& t) throw(std::bad_alloc, std::runtime_error) {
std::string name = t;
if(name == "" || t.tail() != "") {
messages << "syntax: remove-watch <name>" << std::endl;
@ -659,10 +657,9 @@ namespace
while(1);
});
function_ptr_command<const std::string&> mouse_button_handler("mouse_button", "no description available",
function_ptr_command<tokensplitter&> mouse_button_handler("mouse_button", "no description available",
"No help available\n",
[](const std::string& args) throw(std::bad_alloc, std::runtime_error) {
tokensplitter t(args);
[](tokensplitter& t) throw(std::bad_alloc, std::runtime_error) {
std::string x = t;
std::string y = t;
std::string b = t;

View file

@ -56,31 +56,28 @@ namespace
messages << "End of authors list" << std::endl;
});
function_ptr_command<const std::string&> add_author("add-author", "Add an author",
function_ptr_command<tokensplitter&> add_author("add-author", "Add an author",
"Syntax: add-author <fullname>\nSyntax: add-author |<nickname>\n"
"Syntax: add-author <fullname>|<nickname>\nAdds a new author\n",
[](const std::string& args) throw(std::bad_alloc, std::runtime_error) {
tokensplitter t(args);
[](tokensplitter& t) throw(std::bad_alloc, std::runtime_error) {
auto g = split_author(t.tail());
our_movie.authors.push_back(g);
messages << (our_movie.authors.size() - 1) << ": " << g.first << "|" << g.second << std::endl;
});
function_ptr_command<const std::string&> remove_author("remove-author", "Remove an author",
function_ptr_command<tokensplitter&> remove_author("remove-author", "Remove an author",
"Syntax: remove-author <id>\nRemoves author with ID <id>\n",
[](const std::string& args) throw(std::bad_alloc, std::runtime_error) {
tokensplitter t(args);
[](tokensplitter& t) throw(std::bad_alloc, std::runtime_error) {
uint64_t index = parse_value<uint64_t>(t.tail());
if(index >= our_movie.authors.size())
throw std::runtime_error("No such author");
our_movie.authors.erase(our_movie.authors.begin() + index);
});
function_ptr_command<const std::string&> edit_author("edit-author", "Edit an author",
function_ptr_command<tokensplitter&> edit_author("edit-author", "Edit an author",
"Syntax: edit-author <authorid> <fullname>\nSyntax: edit-author <authorid> |<nickname>\n"
"Syntax: edit-author <authorid> <fullname>|<nickname>\nEdits author name\n",
[](const std::string& args) throw(std::bad_alloc, std::runtime_error) {
tokensplitter t(args);
[](tokensplitter& t) throw(std::bad_alloc, std::runtime_error) {
uint64_t index = parse_value<uint64_t>(t);
if(index >= our_movie.authors.size())
throw std::runtime_error("No such author");

View file

@ -10,11 +10,10 @@ namespace
{
std::map<std::string, setting*>* settings;
function_ptr_command<const std::string&> set_setting("set-setting", "set a setting",
function_ptr_command<tokensplitter&> set_setting("set-setting", "set a setting",
"Syntax: set-setting <setting> [<value>]\nSet setting to a new value. Omit <value> to set to ''\n",
[](const std::string& args) throw(std::bad_alloc, std::runtime_error) {
[](tokensplitter& t) throw(std::bad_alloc, std::runtime_error) {
std::string syntax = "Syntax: set-setting <setting> [<value>]";
tokensplitter t(args);
std::string settingname = t;
std::string settingvalue = t.tail();
if(settingname == "")
@ -24,11 +23,10 @@ namespace
<< std::endl;
});
function_ptr_command<const std::string&> unset_setting("unset-setting", "unset a setting",
function_ptr_command<tokensplitter&> unset_setting("unset-setting", "unset a setting",
"Syntax: unset-setting <setting>\nTry to unset a setting. Note that not all settings can be unset\n",
[](const std::string& args) throw(std::bad_alloc, std::runtime_error) {
[](tokensplitter& t) throw(std::bad_alloc, std::runtime_error) {
std::string syntax = "Syntax: unset-setting <setting>";
tokensplitter t(args);
std::string settingname = t;
if(settingname == "" || t)
throw std::runtime_error("Expected setting name and nothing else");
@ -36,10 +34,9 @@ namespace
messages << "Setting '" << settingname << "' unset" << std::endl;
});
function_ptr_command<const std::string&> get_command("get-setting", "get value of a setting",
function_ptr_command<tokensplitter&> get_command("get-setting", "get value of a setting",
"Syntax: get-setting <setting>\nShow value of setting\n",
[](const std::string& args) throw(std::bad_alloc, std::runtime_error) {
tokensplitter t(args);
[](tokensplitter& t) throw(std::bad_alloc, std::runtime_error) {
std::string settingname = t;
if(settingname == "" || t.tail() != "")
throw std::runtime_error("Expected setting name and nothing else");