Classes | Public Types | Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
uuid::console::Commands Class Reference

Container of commands for use by a Shell. More...

#include <uuid/console.h>

Collaboration diagram for uuid::console::Commands:
Collaboration graph
[legend]

Classes

class  Command
 Command for execution on a Shell. More...
 
struct  Completion
 Result of a command completion operation. More...
 
struct  Execution
 Result of a command execution operation. More...
 
struct  Match
 Result of a command find operation. More...
 

Public Types

using command_function = std::function< void(Shell &shell, std::vector< std::string > &arguments)>
 Function to handle a command. More...
 
using argument_completion_function = std::function< const std::vector< std::string >(Shell &shell, const std::vector< std::string > &arguments)>
 Function to obtain completions for a command line. More...
 
using apply_function = std::function< void(std::vector< std::string > &name, std::vector< std::string > &arguments)>
 Function to apply an operation to a command. More...
 

Public Member Functions

 Commands ()=default
 Construct a new container of commands for use by a Shell. More...
 
void add_command (const flash_string_vector &name, command_function function)
 Add a command with no arguments to the list of commands in this container. More...
 
void add_command (const flash_string_vector &name, const flash_string_vector &arguments, command_function function)
 Add a command with arguments to the list of commands in this container. More...
 
void add_command (const flash_string_vector &name, const flash_string_vector &arguments, command_function function, argument_completion_function arg_function)
 Add a command with arguments and automatic argument completion to the list of commands in this container. More...
 
void add_command (unsigned int context, unsigned int flags, const flash_string_vector &name, command_function function)
 Add a command with no arguments to the list of commands in this container. More...
 
void add_command (unsigned int context, unsigned int flags, const flash_string_vector &name, const flash_string_vector &arguments, command_function function)
 Add a command with arguments to the list of commands in this container. More...
 
void add_command (unsigned int context, unsigned int flags, const flash_string_vector &name, const flash_string_vector &arguments, command_function function, argument_completion_function arg_function)
 Add a command with arguments and automatic argument completion to the list of commands in this container. More...
 
Execution execute_command (Shell &shell, CommandLine &&command_line)
 Execute a command for a Shell if it exists in the current context and with the current flags. More...
 
Completion complete_command (Shell &shell, const CommandLine &command_line)
 Complete a partial command for a Shell if it exists in the current context and with the current flags. More...
 
void for_each_available_command (Shell &shell, apply_function f) const
 Applies the given function object f to all commands in the current context and with the current flags. More...
 

Private Member Functions

Match find_command (Shell &shell, const CommandLine &command_line)
 Find commands by matching them against the command line. More...
 

Static Private Member Functions

static bool find_longest_common_prefix (const std::multimap< size_t, const Command *> &commands, std::vector< std::string > &longest_name)
 Find the longest common prefix from a shortest match of commands. More...
 
static std::string find_longest_common_prefix (const std::vector< std::string > &arguments)
 Find the longest common prefix from a list of potential arguments. More...
 

Private Attributes

std::multimap< unsigned int, Commandcommands_
 

Detailed Description

Container of commands for use by a Shell.

These should normally be stored in a std::shared_ptr and reused.

Since
0.1.0

Definition at line 1084 of file console.h.

Member Typedef Documentation

◆ apply_function

using uuid::console::Commands::apply_function = std::function<void(std::vector<std::string> &name, std::vector<std::string> &arguments)>

Function to apply an operation to a command.

Parameters
[in]nameName of the command as a std::vector of strings.
[in]argumentsHelp text for arguments that the command accepts as a std::vector of strings.
Since
0.4.0

Definition at line 1140 of file console.h.

◆ argument_completion_function

using uuid::console::Commands::argument_completion_function = std::function<const std::vector<std::string>(Shell &shell, const std::vector<std::string> &arguments)>

Function to obtain completions for a command line.

This is a vector instead of set so that a custom sort order can be used. It should normally be sorted lexicographically so that the list of options is not confusing.

Parameters
[in]shellShell instance that has a command line matching this command.
[in]argumentsCommand line arguments prior to (but excluding) the argument being completed.
Returns
Possible values for the next argument on the command line.
Since
0.1.0

Definition at line 1130 of file console.h.

◆ command_function

using uuid::console::Commands::command_function = std::function<void(Shell &shell, std::vector<std::string> &arguments)>

Function to handle a command.

Parameters
[in]shellShell instance that is executing the command.
[in]argumentsCommand line arguments.
Since
0.1.0

Definition at line 1114 of file console.h.

Constructor & Destructor Documentation

◆ Commands()

uuid::console::Commands::Commands ( )
default

Construct a new container of commands for use by a Shell.

This should normally be stored in a std::shared_ptr and reused.

Since
0.1.0

Member Function Documentation

◆ add_command() [1/6]

void uuid::console::Commands::add_command ( const flash_string_vector name,
command_function  function 
)

Add a command with no arguments to the list of commands in this container.

The shell context for the command will default to 0 and not require any flags for it to be available.

Parameters
[in]nameName of the command as a std::vector of flash strings.
[in]functionFunction to be used when the command is executed.
Since
0.2.0

Definition at line 45 of file commands.cpp.

◆ add_command() [2/6]

void uuid::console::Commands::add_command ( const flash_string_vector name,
const flash_string_vector arguments,
command_function  function 
)

Add a command with arguments to the list of commands in this container.

The shell context for the command will default to 0 and not require any flags for it to be available.

Parameters
[in]nameName of the command as a std::vector of flash strings.
[in]argumentsHelp text for arguments that the command accepts as a std::vector of flash strings (use "<" to indicate a required argument).
[in]functionFunction to be used when the command is executed.
Since
0.2.0

Definition at line 49 of file commands.cpp.

◆ add_command() [3/6]

void uuid::console::Commands::add_command ( const flash_string_vector name,
const flash_string_vector arguments,
command_function  function,
argument_completion_function  arg_function 
)

Add a command with arguments and automatic argument completion to the list of commands in this container.

The shell context for the command will default to 0 and not require any flags for it to be available.

Parameters
[in]nameName of the command as a std::vector of flash strings.
[in]argumentsHelp text for arguments that the command accepts as a std::vector of flash strings (use "<" to indicate a required argument).
[in]functionFunction to be used when the command is executed.
[in]arg_functionFunction to be used to perform argument completions for this command.
Since
0.2.0

Definition at line 54 of file commands.cpp.

◆ add_command() [4/6]

void uuid::console::Commands::add_command ( unsigned int  context,
unsigned int  flags,
const flash_string_vector name,
command_function  function 
)

Add a command with no arguments to the list of commands in this container.

Parameters
[in]contextShell context in which this command is available.
[in]flagsShell flags that must be set for this command to be available.
[in]nameName of the command as a std::vector of flash strings.
[in]functionFunction to be used when the command is executed.
Since
0.1.0

Definition at line 59 of file commands.cpp.

◆ add_command() [5/6]

void uuid::console::Commands::add_command ( unsigned int  context,
unsigned int  flags,
const flash_string_vector name,
const flash_string_vector arguments,
command_function  function 
)

Add a command with arguments to the list of commands in this container.

Parameters
[in]contextShell context in which this command is available.
[in]flagsShell flags that must be set for this command to be available.
[in]nameName of the command as a std::vector of flash strings.
[in]argumentsHelp text for arguments that the command accepts as a std::vector of flash strings (use "<" to indicate a required argument).
[in]functionFunction to be used when the command is executed.
Since
0.1.0

Definition at line 64 of file commands.cpp.

◆ add_command() [6/6]

void uuid::console::Commands::add_command ( unsigned int  context,
unsigned int  flags,
const flash_string_vector name,
const flash_string_vector arguments,
command_function  function,
argument_completion_function  arg_function 
)

Add a command with arguments and automatic argument completion to the list of commands in this container.

Parameters
[in]contextShell context in which this command is available.
[in]flagsShell flags that must be set for this command to be available.
[in]nameName of the command as a std::vector of flash strings.
[in]argumentsHelp text for arguments that the command accepts as a std::vector of flash strings (use "<" to indicate a required argument).
[in]functionFunction to be used when the command is executed.
[in]arg_functionFunction to be used to perform argument completions for this command.
Since
0.1.0

Definition at line 70 of file commands.cpp.

◆ complete_command()

Commands::Completion uuid::console::Commands::complete_command ( Shell shell,
const CommandLine command_line 
)

Complete a partial command for a Shell if it exists in the current context and with the current flags.

Parameters
[in]shellShell that is completing the command.
[in]command_lineCommand line parameters.
Returns
An object describing the result of the command completion operation.
Since
0.1.0

Definition at line 196 of file commands.cpp.

◆ execute_command()

Commands::Execution uuid::console::Commands::execute_command ( Shell shell,
CommandLine &&  command_line 
)

Execute a command for a Shell if it exists in the current context and with the current flags.

Parameters
[in]shellShell that is executing the command.
[in]command_lineCommand line parameters.
Returns
An object describing the result of the command execution operation.
Since
0.1.0

Definition at line 77 of file commands.cpp.

◆ find_command()

Commands::Match uuid::console::Commands::find_command ( Shell shell,
const CommandLine command_line 
)
private

Find commands by matching them against the command line.

Parameters
[in]shellShell that is accessing commands.
[in]command_lineCommand line parmeters.
Returns
An object describing the result of the command find operation.
Since
0.1.0

Definition at line 420 of file commands.cpp.

◆ find_longest_common_prefix() [1/2]

bool uuid::console::Commands::find_longest_common_prefix ( const std::multimap< size_t, const Command *> &  commands,
std::vector< std::string > &  longest_name 
)
staticprivate

Find the longest common prefix from a shortest match of commands.

Parameters
[in]commandsAll commands that matched (at least 2).
[out]longest_nameThe longest common prefix as a list of strings.
Returns
True if the longest common prefix is made up of whole components, false if the last part is constructed from a partial component.
Since
0.1.0

Definition at line 111 of file commands.cpp.

◆ find_longest_common_prefix() [2/2]

std::string uuid::console::Commands::find_longest_common_prefix ( const std::vector< std::string > &  arguments)
staticprivate

Find the longest common prefix from a list of potential arguments.

Parameters
[in]argumentsAll potential arguments (at least 2).
Returns
The longest common prefix, which could be empty.
Since
0.1.0

Definition at line 173 of file commands.cpp.

◆ for_each_available_command()

void uuid::console::Commands::for_each_available_command ( Shell shell,
apply_function  f 
) const

Applies the given function object f to all commands in the current context and with the current flags.

Parameters
[in]shellShell that is accessing commands.
[in]fFunction to apply to each command.
Since
0.4.0

Definition at line 477 of file commands.cpp.

Member Data Documentation

◆ commands_

std::multimap<unsigned int,Command> uuid::console::Commands::commands_
private

Commands stored in this container, separated by context.

Since
0.1.0

Definition at line 1398 of file console.h.


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