Skip to content
Permalink
Browse files

Add a LambdaCmd to make it even easier to define commands

  • Loading branch information...
Kangz committed Sep 2, 2015
1 parent 1493fc9 commit 4ec32f05994ad23d438b96908c9c0ed03fd8d58e
Showing with 48 additions and 1 deletion.
  1. +17 −0 daemon/src/common/Command.cpp
  2. +31 −1 daemon/src/common/Command.h
@@ -499,4 +499,21 @@ namespace Cmd {
AddCommand(std::move(name), *this, std::move(description));
}

CompletionResult NoopComplete(int, const Args&, Str::StringRef) {
return {};
}

LambdaCmd::LambdaCmd(std::string name, std::string description, RunFn run, CompleteFn complete)
:StaticCmd(std::move(name), std::move(description)), run(run), complete(complete) {
}
LambdaCmd::LambdaCmd(std::string name, int flags, std::string description, RunFn run, CompleteFn complete)
:StaticCmd(std::move(name), flags, std::move(description)), run(run), complete(complete) {
}

void LambdaCmd::Run(const Args& args) const {
run(args);
}
CompletionResult LambdaCmd::Complete(int argNum, const Args& args, Str::StringRef prefix) const {
return complete(argNum, args, prefix);
}
}
@@ -121,7 +121,7 @@ namespace Cmd {
// Called when the command is run with the command line args
virtual void Run(const Args& args) const = 0;
// Called when the user wants to autocomplete a call to this command.
virtual Cmd::CompletionResult Complete(int argNum, const Args& args, Str::StringRef prefix) const;
virtual CompletionResult Complete(int argNum, const Args& args, Str::StringRef prefix) const;

// Prints the usage of this command with a standard formatting
void PrintUsage(const Args& args, Str::StringRef syntax, Str::StringRef description = "") const;
@@ -161,12 +161,42 @@ namespace Cmd {
StaticCmd(std::string name, int flags, std::string description);
};

/**
* Cmd::LambdaCmd an automatically registered command whose callbacks can be
* defined by lambdas. A typical usage is
* static LambdaCMd MyCmd("my_command", NAMESPACE, "my_description",
* [](const Args& args) {
* // Do stuff
* }
* );
*/
CompletionResult NoopComplete(int argNum, const Args& args, Str::StringRef prefix);

class LambdaCmd : public StaticCmd {
public:
using RunFn = std::function<void(const Args&)>;
using CompleteFn = std::function<CompletionResult(int, const Args&, Str::StringRef)>;
LambdaCmd(std::string name, std::string description, RunFn run, CompleteFn complete = NoopComplete);
LambdaCmd(std::string name, int flags, std::string description, RunFn run, CompleteFn complete = NoopComplete);

void Run(const Args& args) const OVERRIDE;
CompletionResult Complete(int argNum, const Args& args, Str::StringRef prefix) const OVERRIDE;

private:
RunFn run;
CompleteFn complete;
};

/**
* Commands can be run from an environment. For now it is only
* used to redirect some calls (Print, ...).
*
* A DefaultEnvironment in CommandSystem can be inherited from
* when not all calls are redefined.
*
* Note that the environment is only used to support the print redirection
* for rcon. Changing that mechanism is TODO but will allow the removal of
* Environment.
*/
class Environment {
public:

0 comments on commit 4ec32f0

Please sign in to comment.
You can’t perform that action at this time.