#include <senf/Utils/Console/ParsedCommand.hh>
ParsedCommandOverloadBase implements a CommandOverload implementation supporting automatic parsing of arguments. This is not a node, it's a CommandOverload which is then added to an OverloadedCommandNode instance.
Automatic argument parsing and return value processing consists of several components:
namespace fty = senf::console::factory; std::string taskStatus(int id); senf::console::root().add("taskStatus", fty::Command(&taskStatus));
There are quite a number of additional parameters available to be set. These parameters are documented in ParsedArgumentAttributor. Parameters are set by adding them as additional calls after adding the node:
senf::console::root().add("taskStatus", fty::Command(&taskStatus) .doc("Query the current task status") .arg( name = "id", description = "numeric id of task to check, -1 for the current task." default_value = -1 ) );
You may also add an additional std::ostream
& Argument as first argument to the callback. If this argument is present, the stream connected to the console which issued the command will be passed there. This allows writing arbitrary messages to the console.
Additionally, overloading is supported by registering multiple commands under the same name. So, elaborating on above example:
std::string taskStatus(int id); std::string taskStatus(std::string const & name); senf::console::root() .add("taskStatus", fty::Command(static_cast<std::string (*)(int)>( &taskStatus)) .doc("Query the current task status") .overloadDoc("Query status by id") .arg( name = "id", description = "numeric id of task to check, -1 for the current task." default_value = -1 ) ); senf::console::root() .add("taskStatus", fty::Command(static_cast<std::string (*)(std::string const &)>( &taskStatus)) .overloadDoc("Query status by name") .arg( name = "name", description = "name of task to check" ) );
We can see here, that taking the address of an overloaded function requires a cast. If you can give unique names to each of the C++ overloads (not the overloads in the console), you should do so to make the unwieldy casts unnecessary.
boost::lexical_cast
and therefore using iostreams
. This means, that any type which can be read from a stream can automatically be used as argument type.
However, argument parsing can be configured by specializing senf::console::ArgumentTraits. See that class for more information.
Definition at line 136 of file ParsedCommand.hh.
Public Types |
|
typedef boost::intrusive_ptr < ParsedCommandOverloadBase > |
ptr |
Public Member Functions |
|
detail::ArgumentInfoBase & | arg (unsigned n) const |
void | doc (std::string const &d) |
Protected Member Functions |
|
ParsedCommandOverloadBase () | |
template<class Type > | |
void | addParameter () |
typedef boost::intrusive_ptr<ParsedCommandOverloadBase> senf::console::ParsedCommandOverloadBase:: | ||||
ptr | ||||
Reimplemented from senf::console::CommandOverload.
Reimplemented in senf::console::ParsedCommandOverload< FunctionTraits, ReturnType, arity >.
Definition at line 140 of file ParsedCommand.hh.
senf::console::ParsedCommandOverloadBase:: | ||||
ParsedCommandOverloadBase | () | |||
Definition at line 44 of file ParsedCommand.cci.
void senf::console::ParsedCommandOverloadBase:: | ||||
addParameter | () | |||
Definition at line 64 of file ParsedCommand.cti.
senf::console::detail::ArgumentInfoBase & senf::console::ParsedCommandOverloadBase:: | ||||
arg | ( | unsigned | n | ) |
Definition at line 48 of file ParsedCommand.cci.
void senf::console::ParsedCommandOverloadBase:: | ||||
doc | ( | std::string const & | d | ) |
Definition at line 55 of file ParsedCommand.cci.