CommandOverload implementation with automatic argument parsing. More...

#include <senf/Utils/Console/ParsedCommand.hh>

Inheritance diagram for senf::console::ParsedCommandOverloadBase:

Public Types

typedef boost::intrusive_ptr< ParsedCommandOverloadBaseptr
 
- Public Types inherited from senf::console::CommandOverload
typedef boost::intrusive_ptr< CommandOverloadptr
 
typedef boost::intrusive_ptr< CommandOverload const > cptr
 
- Public Types inherited from senf::intrusive_refcount_base
typedef unsigned refcount_t
 

Public Member Functions

detail::ArgumentInfoBase & arg (unsigned n) const
 
void doc (std::string const &d)
 
- Public Member Functions inherited from senf::console::CommandOverload
virtual ~CommandOverload ()
 
void execute (boost::any &rv, std::ostream &os, ParseCommandInfo const &command)
 Call the overload. More...
 
void operator() (boost::any &rv, std::ostream &os, ParseCommandInfo const &command)
 Call the overload. More...
 
unsigned numArguments () const
 Number of arguments this overload takes. More...
 
void argumentDoc (unsigned index, ArgumentDoc &doc) const
 Get information on argument index. More...
 
std::string doc () const
 Get overload documentation. More...
 
OverloadedCommandNodenode () const
 Access owning node. More...
 
unsigned overloadIndex () const
 Get index of overload in it's OverloadedCommandNode. More...
 
- Public Member Functions inherited from senf::intrusive_refcount_base
virtual ~intrusive_refcount_base ()
 
refcount_t refcount () const
 
bool is_shared () const
 

Protected Member Functions

 ParsedCommandOverloadBase ()
 
template<class Type >
void addParameter ()
 
- Protected Member Functions inherited from senf::console::CommandOverload
 CommandOverload ()
 
virtual void v_execute (boost::any &rv, std::ostream &os, ParseCommandInfo const &command) const =0
 Execute the overload. More...
 
- Protected Member Functions inherited from senf::intrusive_refcount
 intrusive_refcount ()
 
- Protected Member Functions inherited from intrusive_refcount_t< intrusive_refcount >
 intrusive_refcount_t ()
 
- Protected Member Functions inherited from senf::intrusive_refcount_base
 intrusive_refcount_base ()
 
void add_ref ()
 
bool release ()
 

Detailed Description

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:
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 \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")
.arg( name = "id",
description = "numeric id of task to check, -1 for the current task."
default_value = -1 ) );
.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.

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.

Member Typedef Documentation

◆ ptr

Definition at line 130 of file ParsedCommand.hh.

Constructor & Destructor Documentation

◆ ParsedCommandOverloadBase()

senf::console::ParsedCommandOverloadBase::ParsedCommandOverloadBase ( )
protected

Member Function Documentation

◆ addParameter()

template<class Type >
void senf::console::ParsedCommandOverloadBase::addParameter ( )
protected

◆ arg()

detail::ArgumentInfoBase& senf::console::ParsedCommandOverloadBase::arg ( unsigned  n) const

◆ doc()

void senf::console::ParsedCommandOverloadBase::doc ( std::string const &  d)

The documentation for this class was generated from the following files: