ChainCLI
A modern C++20 command-line interface library
Loading...
Searching...
No Matches
cli::commands::CommandTree Class Reference

Tree structure to manage commands and their subcommands. More...

#include <command_tree.h>

Public Member Functions

 CommandTree (std::string_view rootName)
 Construct a new CommandTree.
 
template<typename... Ids>
void insert (std::unique_ptr< Command > cmd, std::string_view parentId, Ids &&...rest)
 Insert a command into the tree.
 
void insert (std::unique_ptr< Command > cmd)
 Insert a command into the tree.
 
template<typename... Ids>
Commandfind (Ids &&...ids) const
 Find a command in the tree by a path of identifiers leading to it.
 
void forEachCommand (const std::function< void(Command *)> &func) const
 Apply a function to each command in the tree.
 
void forEachCommand (const std::function< void(Command &)> &func) const
 Apply a function to each command in the tree.
 
CommandgetRootCommand ()
 Get the root command of the tree.
 
const CommandgetRootCommand () const
 Get the root command of the tree.
 
std::string_view getPathForCommand (Command *cmd) const
 Get the path for a command in the tree.
 
void buildCommandPathMap (const std::string &separator=" ")
 Build a map of command paths for quick lookup.
 
std::vector< Command * > getAllCommands () const
 Get a vector of all commands in the tree.
 
std::vector< const Command * > getAllCommandsConst () const
 Get a vector of all commands in the tree (const version).
 

Detailed Description

Tree structure to manage commands and their subcommands.

Each node in the tree is a Command, and commands can have multiple subcommands. The tree allows for insertion, lookup, and traversal of commands.

Constructor & Destructor Documentation

◆ CommandTree()

inline_t cli::commands::CommandTree::CommandTree ( std::string_view  rootName)
explicit

Construct a new CommandTree.

Parameters
rootNameThe name of the root command.

Member Function Documentation

◆ buildCommandPathMap()

inline_t void cli::commands::CommandTree::buildCommandPathMap ( const std::string &  separator = " ")

Build a map of command paths for quick lookup.

Parameters
separatorThe separator to use between command names in the path (default is a space).

◆ find()

template<typename... Ids>
Command * cli::commands::CommandTree::find ( Ids &&...  ids) const
inline

Find a command in the tree by a path of identifiers leading to it.

Template Parameters
...IdsThe types of the command IDs.
Parameters
...idsThe command IDs.
Returns
A pointer to the found command, or nullptr if not found.

◆ forEachCommand() [1/2]

void cli::commands::CommandTree::forEachCommand ( const std::function< void(Command &)> &  func) const
inline

Apply a function to each command in the tree.

The function is applied in a depth-first search (DFS) manner.

Parameters
funcThe function to apply.

◆ forEachCommand() [2/2]

void cli::commands::CommandTree::forEachCommand ( const std::function< void(Command *)> &  func) const
inline

Apply a function to each command in the tree.

The function is applied in a depth-first search (DFS) manner.

Parameters
funcThe function to apply.

◆ getAllCommands()

inline_t std::vector< Command * > cli::commands::CommandTree::getAllCommands ( ) const

Get a vector of all commands in the tree.

Commands are collected in a depth-first search (DFS) manner.

Returns
A vector containing pointers to all commands in the tree.

◆ getAllCommandsConst()

inline_t std::vector< const Command * > cli::commands::CommandTree::getAllCommandsConst ( ) const

Get a vector of all commands in the tree (const version).

Commands are collected in a depth-first search (DFS) manner.

Returns
A vector containing pointers to all commands in the tree.

◆ getPathForCommand()

inline_t std::string_view cli::commands::CommandTree::getPathForCommand ( Command cmd) const

Get the path for a command in the tree.

Note
Uses a pre-built map for O(1) lookup internally that maps needs to be constructed first using the buildCommandPathMap function.
Parameters
cmdThe command to find the path for.
Returns
The path to the command, or an empty string if not found.

◆ getRootCommand() [1/2]

Command * cli::commands::CommandTree::getRootCommand ( )
inline

Get the root command of the tree.

Returns
A pointer to the root command.

◆ getRootCommand() [2/2]

const Command * cli::commands::CommandTree::getRootCommand ( ) const
inline

Get the root command of the tree.

Returns
A pointer to the root command.

◆ insert() [1/2]

void cli::commands::CommandTree::insert ( std::unique_ptr< Command cmd)
inline

Insert a command into the tree.

Parameters
cmdThe command to insert.

◆ insert() [2/2]

template<typename... Ids>
void cli::commands::CommandTree::insert ( std::unique_ptr< Command cmd,
std::string_view  parentId,
Ids &&...  rest 
)
inline

Insert a command into the tree.

Template Parameters
...IdsThe types of the parent command IDs.
Parameters
cmdThe command to insert.
parentIdThe ID of the direct parent command (used to split first value of ...rest).
...restThe IDs of any additional parent commands.

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