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

Represents a command in the CLI application. More...

#include <command.h>

Public Member Functions

 Command (std::string_view id, std::string_view short_desc, std::string_view long_desc, std::unique_ptr< std::function< void(const CliContext &)> > actionPtr)
 Construct a new Command object.
 
 Command (std::string_view id, std::string_view short_desc, std::string_view long_desc, std::function< void(const CliContext &)> action)
 Construct a new Command object.
 
 Command (std::string_view id)
 Construct a new Command object.
 
 Command (Command &&) noexcept=default
 
Commandoperator= (Command &&) noexcept=default
 
 Command (const Command &)=delete
 
Commandoperator= (const Command &)=delete
 
constexpr std::string_view getIdentifier () const noexcept
 Get the unique identifier for the command.
 
constexpr std::string_view getShortDescription () const noexcept
 Get the short description of the command.
 
constexpr std::string_view getLongDescription () const noexcept
 Get the long description of the command.
 
bool hasExecutionFunction () const noexcept
 Check if the command has an execution function.
 
const std::vector< std::shared_ptr< PositionalArgumentBase > > & getPositionalArguments () const noexcept
 Get the positional arguments for the command.
 
const std::vector< std::shared_ptr< OptionArgumentBase > > & getOptionArguments () const noexcept
 Get the option arguments for the command.
 
const std::vector< std::shared_ptr< FlagArgument > > & getFlagArguments () const noexcept
 Get the flag arguments for the command.
 
const std::vector< std::unique_ptr< ArgumentGroup > > & getArgumentGroups () const noexcept
 Get the argument groups for the command.
 
std::string_view getDocStringShort () const
 Get the short documentation string for the command.
 
std::string_view getDocStringLong () const
 Get the long documentation string for the command.
 
CommandgetSubCommand (std::string_view id)
 Get a sub-command by its identifier.
 
const CommandgetSubCommand (std::string_view id) const
 Get a sub-command by its identifier.
 
auto & getSubCommands ()
 Get all sub-commands of the command.
 
auto const & getSubCommands () const
 Get all sub-commands of the command.
 
void execute (const CliContext &context) const
 Execute the command.
 
CommandwithShortDescription (std::string_view desc)
 Set the short description for the command.
 
CommandwithLongDescription (std::string_view desc)
 Set the long description for the command.
 
template<typename T >
CommandwithPositionalArgument (std::shared_ptr< PositionalArgument< T > > arg)
 Add a positional argument to the command.
 
template<typename T >
CommandwithPositionalArgument (PositionalArgument< T > &&arg)
 Add a positional argument to the command.
 
template<typename T >
CommandwithPositionalArgument (PositionalArgument< T > &arg)
 Add a positional argument to the command.
 
template<typename T >
CommandwithOptionArgument (std::shared_ptr< OptionArgument< T > > arg)
 Add an option argument to the command.
 
template<typename T >
CommandwithOptionArgument (OptionArgument< T > &&arg)
 Add an option argument to the command.
 
template<typename T >
CommandwithOptionArgument (OptionArgument< T > &arg)
 Add an option argument to the command.
 
CommandwithFlagArgument (std::shared_ptr< FlagArgument > arg)
 Add a flag argument to the command.
 
CommandwithFlagArgument (FlagArgument &&arg)
 Add a flag argument to the command.
 
CommandwithFlagArgument (FlagArgument &arg)
 Add a flag argument to the command.
 
CommandwithExecutionFunc (std::unique_ptr< std::function< void(const CliContext &)> > actionPtr)
 Set the execution function for the command.
 
CommandwithExecutionFunc (std::function< void(const CliContext &)> &&action)
 Set the execution function for the command.
 
CommandwithSubCommand (std::unique_ptr< Command > subCommandPtr)
 Add a sub-command to this command.
 
CommandwithSubCommand (Command &&subCommand)
 
template<typename... Args>
requires ((sizeof...(Args) > 0) && (std::derived_from<std::remove_cvref_t<Args>, ArgumentBase> && ...))
CommandwithExclusiveGroup (Args &&...args)
 Add a sub-command to this command.
 
template<typename... Args>
requires ((sizeof...(Args) > 0) && (std::derived_from<std::remove_cvref_t<Args>, ArgumentBase> && ...))
CommandwithInclusiveGroup (Args &&...args)
 Add an inclusive argument group to this command.
 

Friends

class cli::commands::docwriting::DocWriter
 
std::ostream & operator<< (std::ostream &out, const Command &cmd)
 

Detailed Description

Represents a command in the CLI application.

A command can have subcommands, arguments (positional, option, flag), and an execution function. Commands can be nested to create a hierarchy of commands and subcommands.

Constructor & Destructor Documentation

◆ Command() [1/3]

cli::commands::Command::Command ( std::string_view  id,
std::string_view  short_desc,
std::string_view  long_desc,
std::unique_ptr< std::function< void(const CliContext &)> >  actionPtr 
)
inline

Construct a new Command object.

Parameters
idThe unique identifier for the command.
short_descA short description of the command.
long_descA long description of the command.
actionPtrA pointer to the function to execute when the command is called.

◆ Command() [2/3]

cli::commands::Command::Command ( std::string_view  id,
std::string_view  short_desc,
std::string_view  long_desc,
std::function< void(const CliContext &)>  action 
)
inline

Construct a new Command object.

Parameters
idThe unique identifier for the command.
short_descA short description of the command.
long_descA long description of the command.
actionA function to execute when the command is called.

◆ Command() [3/3]

cli::commands::Command::Command ( std::string_view  id)
inlineexplicit

Construct a new Command object.

Parameters
idThe unique identifier for the command.

Member Function Documentation

◆ execute()

inline_t void cli::commands::Command::execute ( const CliContext context) const

Execute the command.

Parameters
contextThe CLI context to use for execution.

◆ getArgumentGroups()

const std::vector< std::unique_ptr< ArgumentGroup > > & cli::commands::Command::getArgumentGroups ( ) const
inlinenoexcept

Get the argument groups for the command.

Note
Argument groups appear on the command line help messages in the order they were added
Returns
The argument groups for the command.

◆ getDocStringLong()

inline_t std::string_view cli::commands::Command::getDocStringLong ( ) const

Get the long documentation string for the command.

This description contains the textual representation of the command and its arguments as well as the long description and the Options segment.

Note
the doc strings are cached internally and need to be built before being accessed
Returns
The long documentation string for the command.

◆ getDocStringShort()

inline_t std::string_view cli::commands::Command::getDocStringShort ( ) const

Get the short documentation string for the command.

This description only contains the textual representation of the command and its arguments as well as the short description.

Note
the doc strings are cached internally and need to be built before being accessed
Returns
The short documentation string for the command.

◆ getFlagArguments()

const std::vector< std::shared_ptr< FlagArgument > > & cli::commands::Command::getFlagArguments ( ) const
inlinenoexcept

Get the flag arguments for the command.

Note
Arguments appear on the command line help messages in the order they were added to the command.
Returns
The flag arguments for the command.

◆ getIdentifier()

constexpr std::string_view cli::commands::Command::getIdentifier ( ) const
inlineconstexprnoexcept

Get the unique identifier for the command.

Returns
The unique identifier for the command.

◆ getLongDescription()

constexpr std::string_view cli::commands::Command::getLongDescription ( ) const
inlineconstexprnoexcept

Get the long description of the command.

Returns
The long description of the command.

◆ getOptionArguments()

const std::vector< std::shared_ptr< OptionArgumentBase > > & cli::commands::Command::getOptionArguments ( ) const
inlinenoexcept

Get the option arguments for the command.

Note
Arguments appear on the command line help messages in the order they were added to the command.
Returns
The option arguments for the command.

◆ getPositionalArguments()

const std::vector< std::shared_ptr< PositionalArgumentBase > > & cli::commands::Command::getPositionalArguments ( ) const
inlinenoexcept

Get the positional arguments for the command.

Returns
The positional arguments for the command.

◆ getShortDescription()

constexpr std::string_view cli::commands::Command::getShortDescription ( ) const
inlineconstexprnoexcept

Get the short description of the command.

Returns
The short description of the command.

◆ getSubCommand() [1/2]

inline_t Command * cli::commands::Command::getSubCommand ( std::string_view  id)

Get a sub-command by its identifier.

Parameters
idThe identifier of the sub-command.
Returns
A pointer to the sub-command if found, nullptr otherwise.

◆ getSubCommand() [2/2]

inline_t const Command * cli::commands::Command::getSubCommand ( std::string_view  id) const

Get a sub-command by its identifier.

Parameters
idThe identifier of the sub-command.
Returns
A pointer to the sub-command if found, nullptr otherwise.

◆ getSubCommands() [1/2]

auto & cli::commands::Command::getSubCommands ( )
inline

Get all sub-commands of the command.

Returns
A reference to the map of identifiers to their sub-commands.

◆ getSubCommands() [2/2]

auto const & cli::commands::Command::getSubCommands ( ) const
inline

Get all sub-commands of the command.

Returns
A reference to the map of identifiers to their sub-commands.

◆ hasExecutionFunction()

bool cli::commands::Command::hasExecutionFunction ( ) const
inlinenoexcept

Check if the command has an execution function.

Returns
True if the command has an execution function, false otherwise.

◆ withExclusiveGroup()

template<typename... Args>
requires ((sizeof...(Args) > 0) && (std::derived_from<std::remove_cvref_t<Args>, ArgumentBase> && ...))
Command & cli::commands::Command::withExclusiveGroup ( Args &&...  args)
inline

Add a sub-command to this command.

Template Parameters
...ArgsThe types of the sub-commands.
Parameters
...argsThe sub-commands to add.
Returns
A reference to this command.

◆ withExecutionFunc() [1/2]

inline_t Command & cli::commands::Command::withExecutionFunc ( std::function< void(const CliContext &)> &&  action)

Set the execution function for the command.

Parameters
actionThe function to execute when the command is called.
Returns
A reference to this command.

◆ withExecutionFunc() [2/2]

inline_t Command & cli::commands::Command::withExecutionFunc ( std::unique_ptr< std::function< void(const CliContext &)> >  actionPtr)

Set the execution function for the command.

Parameters
actionThe function to execute when the command is called.
Returns
A reference to this command.

◆ withFlagArgument() [1/3]

inline_t Command & cli::commands::Command::withFlagArgument ( FlagArgument &&  arg)

Add a flag argument to the command.

Note
Arguments appear on the command line help messages in the order they were added to the command.
Parameters
argThe flag argument to set.
Returns
A reference to this command.

◆ withFlagArgument() [2/3]

inline_t Command & cli::commands::Command::withFlagArgument ( FlagArgument arg)

Add a flag argument to the command.

Note
Arguments appear on the command line help messages in the order they were added to the command.
Parameters
argThe flag argument to set.
Returns
A reference to this command.

◆ withFlagArgument() [3/3]

inline_t Command & cli::commands::Command::withFlagArgument ( std::shared_ptr< FlagArgument arg)

Add a flag argument to the command.

Note
Arguments appear on the command line help messages in the order they were added to the command.
Parameters
argThe flag argument to set.
Returns
A reference to this command.

◆ withInclusiveGroup()

template<typename... Args>
requires ((sizeof...(Args) > 0) && (std::derived_from<std::remove_cvref_t<Args>, ArgumentBase> && ...))
Command & cli::commands::Command::withInclusiveGroup ( Args &&...  args)
inline

Add an inclusive argument group to this command.

Template Parameters
...ArgsThe types of the sub-commands.
Parameters
...argsThe sub-commands to add.
Returns
A reference to this command.

◆ withLongDescription()

inline_t Command & cli::commands::Command::withLongDescription ( std::string_view  desc)

Set the long description for the command.

Parameters
descThe long description to set.
Returns
A reference to this command.

◆ withOptionArgument() [1/3]

template<typename T >
Command & cli::commands::Command::withOptionArgument ( OptionArgument< T > &&  arg)
inline

Add an option argument to the command.

Note
Arguments appear on the command line help messages in the order they were added to the command.
Template Parameters
TThe type of the option argument.
Parameters
argThe option argument to set.
Returns
A reference to this command.

◆ withOptionArgument() [2/3]

template<typename T >
Command & cli::commands::Command::withOptionArgument ( OptionArgument< T > &  arg)
inline

Add an option argument to the command.

Note
Arguments appear on the command line help messages in the order they were added to the command.
Template Parameters
TThe type of the option argument.
Parameters
argThe option argument to set.
Returns
A reference to this command.

◆ withOptionArgument() [3/3]

template<typename T >
Command & cli::commands::Command::withOptionArgument ( std::shared_ptr< OptionArgument< T > >  arg)
inline

Add an option argument to the command.

Note
Arguments appear on the command line help messages in the order they were added to
Template Parameters
TThe type of the option argument.
Parameters
argThe option argument to set.
Returns
A reference to this command.

◆ withPositionalArgument() [1/3]

template<typename T >
Command & cli::commands::Command::withPositionalArgument ( PositionalArgument< T > &&  arg)
inline

Add a positional argument to the command.

Note
Arguments appear on the command line help messages in the order they were added to the command.
Template Parameters
TThe type of the positional argument.
Parameters
argThe positional argument to set.
Returns
A reference to this command.

◆ withPositionalArgument() [2/3]

template<typename T >
Command & cli::commands::Command::withPositionalArgument ( PositionalArgument< T > &  arg)
inline

Add a positional argument to the command.

Note
Arguments appear on the command line help messages in the order they were added to the command.
Template Parameters
TThe type of the positional argument.
Parameters
argThe positional argument to set.
Returns
A reference to this command.

◆ withPositionalArgument() [3/3]

template<typename T >
Command & cli::commands::Command::withPositionalArgument ( std::shared_ptr< PositionalArgument< T > >  arg)
inline

Add a positional argument to the command.

Note
Arguments appear on the command line help messages in the order they were added to the command.
Template Parameters
TThe type of the positional argument.
Parameters
argThe positional argument to set.
Returns
A reference to this command.

◆ withShortDescription()

inline_t Command & cli::commands::Command::withShortDescription ( std::string_view  desc)

Set the short description for the command.

Parameters
descThe short description to set.
Returns
A reference to this command.

◆ withSubCommand()

inline_t Command & cli::commands::Command::withSubCommand ( std::unique_ptr< Command subCommandPtr)

Add a sub-command to this command.

Parameters
subCommandPtrThe sub-command to add.
Returns
A reference to this command.

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