idfxx 1.0.0
Modern C++23 components for ESP-IDF
Loading...
Searching...
No Matches
Console Component

Interactive console with REPL and command registration. More...

Namespaces

namespace  idfxx
 
namespace  idfxx::console
 

Classes

struct  idfxx::console::command
 Command descriptor for registration. More...
 
struct  idfxx::console::config
 Configuration for low-level console initialization. More...
 
class  idfxx::console::repl
 Interactive REPL (Read-Eval-Print Loop) console. More...
 

Typedefs

using idfxx::console::handler = std::move_only_function< int(int argc, char **argv)>
 Command handler callback type.
 

Functions

void idfxx::console::register_command (const command &cmd, handler callback)
 Registers a command with a callback handler.
 
void idfxx::console::deregister_command (std::string_view name)
 Deregisters a previously registered command.
 
void idfxx::console::register_help_command ()
 Registers the built-in 'help' command.
 
void idfxx::console::deregister_help_command ()
 Deregisters the built-in 'help' command.
 
result< voididfxx::console::try_register_command (const command &cmd, handler callback)
 Registers a command with a callback handler.
 
result< voididfxx::console::try_deregister_command (std::string_view name)
 Deregisters a previously registered command.
 
result< voididfxx::console::try_register_help_command ()
 Registers the built-in 'help' command.
 
result< voididfxx::console::try_deregister_help_command ()
 Deregisters the built-in 'help' command.
 
void idfxx::console::init (const config &cfg={})
 Initializes the console module.
 
void idfxx::console::deinit ()
 Deinitializes the console module.
 
int idfxx::console::run (std::string_view cmdline)
 Runs a command line string.
 
result< voididfxx::console::try_init (const config &cfg={})
 Initializes the console module.
 
result< voididfxx::console::try_deinit ()
 Deinitializes the console module.
 
result< intidfxx::console::try_run (std::string_view cmdline)
 Runs a command line string.
 

Detailed Description

Interactive console with REPL and command registration.

Provides an interactive command-line console for ESP32 devices, supporting UART, USB CDC, and USB Serial JTAG backends. Commands can be registered with C++ callbacks (including lambdas with captures) or raw function pointers.

The console uses a global command registry – commands registered via the free functions in this namespace are available across all backends.

Depends on Core Utilities for error handling.

Typedef Documentation

◆ handler

using idfxx::console::handler = typedef std::move_only_function<int(int argc, char** argv)>

Command handler callback type.

Called when a registered command is executed. The callback receives the argument count and argument vector, and returns an integer status code (0 for success).

Definition at line 51 of file console.hpp.

Function Documentation

◆ deinit()

void idfxx::console::deinit ( )

Deinitializes the console module.

Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled.
Exceptions
std::system_erroron failure.

◆ deregister_command()

void idfxx::console::deregister_command ( std::string_view  name)

Deregisters a previously registered command.

Parameters
nameThe command name to deregister.
Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled.
Exceptions
std::system_erroron failure.

◆ deregister_help_command()

void idfxx::console::deregister_help_command ( )

Deregisters the built-in 'help' command.

Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled.
Exceptions
std::system_erroron failure.

◆ init()

void idfxx::console::init ( const config cfg = {})

Initializes the console module.

Call this before using command registration or run() in non-REPL mode. Not needed when using the REPL, which initializes the console internally.

Parameters
cfgConsole configuration.
Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled.
Exceptions
std::system_erroron failure.

◆ register_command()

void idfxx::console::register_command ( const command cmd,
handler  callback 
)

Registers a command with a callback handler.

The callback can be any callable, including lambdas with captures and raw function pointers. The callback and command strings are stored internally and remain valid until the command is deregistered or the console is deinitialized.

int counter = 0;
{.name = "count", .help = "Increment counter"},
[&counter](int argc, char** argv) {
std::printf("Count: %d\n", counter);
return 0;
});
void register_command(const command &cmd, handler callback)
Registers a command with a callback handler.
std::expected< T, std::error_code > result
result type wrapping a value or error code.
Definition error.hpp:120
Parameters
cmdCommand descriptor.
callbackFunction to call when the command is executed.
Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled.
Exceptions
std::system_erroron failure.

◆ register_help_command()

void idfxx::console::register_help_command ( )

Registers the built-in 'help' command.

The help command lists all registered commands with their help text.

Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled.
Exceptions
std::system_erroron failure.

◆ run()

int idfxx::console::run ( std::string_view  cmdline)

Runs a command line string.

Parses and executes the given command line. The console must be initialized first via init() or by creating a REPL.

{.name = "hello", .help = "Say hello"},
[](int, char**) { std::printf("Hello!\n"); return 0; });
int ret = idfxx::console::run("hello");
int run(std::string_view cmdline)
Runs a command line string.
void init(const config &cfg={})
Initializes the console module.
Parameters
cmdlineThe command line string to execute.
Returns
The command's return code.
Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled.
Exceptions
std::system_errorif the command is not found or the console is not initialized.

◆ try_deinit()

result< void > idfxx::console::try_deinit ( )

Deinitializes the console module.

Returns
Success, or an error.
Return values
invalid_stateIf not initialized.

◆ try_deregister_command()

result< void > idfxx::console::try_deregister_command ( std::string_view  name)

Deregisters a previously registered command.

Parameters
nameThe command name to deregister.
Returns
Success, or an error.
Return values
invalid_argIf the command is not registered.

◆ try_deregister_help_command()

result< void > idfxx::console::try_deregister_help_command ( )

Deregisters the built-in 'help' command.

Returns
Success, or an error.

◆ try_init()

result< void > idfxx::console::try_init ( const config cfg = {})

Initializes the console module.

Call this before using command registration or try_run() in non-REPL mode. Not needed when using the REPL, which initializes the console internally.

Parameters
cfgConsole configuration.
Returns
Success, or an error.
Return values
invalid_stateIf already initialized.
invalid_argIf the configuration is invalid.

◆ try_register_command()

result< void > idfxx::console::try_register_command ( const command cmd,
handler  callback 
)

Registers a command with a callback handler.

The callback can be any callable, including lambdas with captures and raw function pointers. The callback and command strings are stored internally and remain valid until the command is deregistered or the console is deinitialized.

Parameters
cmdCommand descriptor.
callbackFunction to call when the command is executed.
Returns
Success, or an error.
Return values
invalid_argIf the command name is empty or contains spaces.

◆ try_register_help_command()

result< void > idfxx::console::try_register_help_command ( )

Registers the built-in 'help' command.

The help command lists all registered commands with their help text.

Returns
Success, or an error.
Return values
invalid_stateIf the console is not initialized.

◆ try_run()

result< int > idfxx::console::try_run ( std::string_view  cmdline)

Runs a command line string.

Parses and executes the given command line. The console must be initialized first via try_init() or by creating a REPL.

Parameters
cmdlineThe command line string to execute.
Returns
The command's return code, or an error.
Return values
invalid_argIf the command line is empty or only whitespace.
not_foundIf the command is not registered.
invalid_stateIf the console is not initialized.