CommandOverload implementation with automatic argument parsing.
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:
Adding argument parsing callbacks to the tree
To add overloads to the tree, use the senf::console::factory::Command factory:
std::string taskStatus(int id);
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:
.
doc(
"Query the current task status")
description =
"numeric id of task to check, -1 for the current task." You may also add an additional \c 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);
.
add(
"taskStatus", fty::Command(
static_cast<std::string (*)(
int)
>(
&taskStatus))
.
doc(
"Query the current task status")
.overloadDoc("Query status by id")
description =
"numeric id of task to check, -1 for the current task." .
add(
"taskStatus", fty::Command(
static_cast<std::string (*)(std::string
const &)
>(
&taskStatus))
.overloadDoc("Query status by name")
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.
Custom parameter parsers
By default, parameters are parsed using \c boost::lexical_cast and therefore using \c
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.
Custom return-value formatters
By default, return values are streamed to an ostream. This automatically allows any
streamable type to be used as return value. To add new types or customize the formating, the
senf::console::ReturnValueTraits template needs to be specialized for that type. See
that class for more information.
Definition at line 126 of file ParsedCommand.hh.