The Console library implements a runtime interactive (network) console which allows to configure, control and manipulate a running application in any way. Additionally this library provides support for configuration files and command line parsing which can be used with or without the network console.
The Config/Console library is built around several components
#include <senf/Console.hh> // Define callback function. void mycommand(std::ostream & os, int foo, int bar) { // ... os << "!! Important message ...\n"; } namespace kw = senf::console::kw; namespace fty = senf::console::factory; int main(int argc, char** argv) { // Provide global documentation senf::console::root() .doc("This is someServer server"); // Add a command senf::console::root().add("mycommand", fty::Command(&mycommand) .doc("If <bar> is given, flurgle the <foo>, otherwise burgle it") .arg("foo") .arg(kw::name = "bar", kw::default_value = 0) ); // Parse command line parameters senf::console::parseOptions(argc,argv); // Start the interactive console server senf::console::Server::start(senf::INet4SocketAddress(senf::INet4Address::None, 23232u)) .name("someServer"); // Run the scheduler senf::scheduler::process(); }
after this registration, we can call the command from the command-line using
$ someServer --mycommand="1 2"
the console can be accessed easily via telnet:
$ telnet localhost 23232 Trying 127.0.0.1... Connected to localhost. Escape character is '^]' xxxx-xx-xx xx:xx:xx.xxxxxx-0000 [NOTICE][senf::console::Server] Registered new client 0xxxxxxx someServer:/# ls mycommand someServer:/# mycommand !! Important message ... someServer:/# exit xxxx-xx-xx xx:xx:xx.xxxxxx-0000 [NOTICE][senf::console::Server] Disposing client 0xxxxxxx Connection closed by foreign host. $
There are several ways to access the node tree:
The basic idea is, that the console/config library manages a directory structure of parameters and auxiliary commands. Parameters are just commands which set a parameter value so everything is either a directory entry (senf::console::DirectoryNode) or a command (senf::console::CommandNode).
The console/config language does not define, how arguments are passed to the commands, it just tokenizes the input and passes the tokens to the commands which then handle the conversion.
Since parsing the tokens into something usable is quite tedious and error prone, the library implements automatic argument parsing where the argument tokens are automatically parsed depending on argument types. This enables you to register a command taking an integer argument which will be called with an already parsed integer value (or throw a senf::console::SyntaxErrorException if the conversion fails). This will be the most often used command.