cppassist  1.0.0.f4fab4f962ff
C++ sanctuary for small but powerful and frequently required, stand alone features.
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
cppassist::CommandLineAction Class Reference

Command line action. More...

#include <cppassist/include/cppassist/cmdline/CommandLineAction.h>

Public Member Functions

 CommandLineAction (const std::string &name="", const std::string &description="")
 Constructor. More...
 
virtual ~CommandLineAction ()
 Destructor. More...
 
const std::string & name () const
 Get action name. More...
 
void setName (const std::string &name)
 Set action name. More...
 
const std::string & description () const
 Get description. More...
 
void setDescription (const std::string &description)
 Set description. More...
 
const std::vector< CommandLineCommand * > & commands () const
 Get commands. More...
 
CommandLineCommandgetCommand (const std::string &name) const
 Get command by name. More...
 
void add (CommandLineCommand *command)
 Add command. More...
 
const std::vector< CommandLineOption * > & options () const
 Get options. More...
 
CommandLineOptiongetOption (const std::string &name) const
 Get option by name. More...
 
void add (CommandLineOption *option)
 Add option. More...
 
const std::vector< CommandLineSwitch * > & switches () const
 Get switches. More...
 
CommandLineSwitchgetSwitch (const std::string &name) const
 Get switch by name. More...
 
void add (CommandLineSwitch *sw)
 Add switch. More...
 
const std::vector< CommandLineParameter * > & parameters () const
 Get parameters. More...
 
CommandLineParametergetParameter (const std::string &name) const
 Get parameter by name. More...
 
CommandLineParametergetParameter (size_t index) const
 Get parameter by index. More...
 
void add (CommandLineParameter *parameter)
 Add parameter. More...
 
bool optionalParametersAllowed () const
 Check if optional parameters are allowed by the action. More...
 
void setOptionalParametersAllowed (bool allowed)
 Set if optional parameters are allowed by the action. More...
 
const std::string & optionalParameterName () const
 Get optional parameter name. More...
 
void setOptionalParameterName (const std::string &name)
 Set optional parameter name. More...
 
std::string usage () const
 Get usage text. More...
 
void reset ()
 Reset state (forget all information from previous parsings) More...
 
void parse (int argc, char *argv[])
 Parse command line. More...
 
bool hasErrors () const
 Check if there were any errors during parsing. More...
 
const std::vector< std::string > & errors () const
 Get errors. More...
 
bool activated () const
 Check if action has been activated. More...
 
const std::vector< std::string > & optionalParameters () const
 Get optional parameters specified on the command line. More...
 
virtual int execute ()
 Execute action. More...
 

Protected Member Functions

bool checkActivated ()
 Check if this action has been activated. More...
 
void checkErrors ()
 Checks for errors and collects error messages. More...
 

Protected Attributes

std::string m_name
 Action name. More...
 
std::string m_description
 Description text. More...
 
std::vector< CommandLineCommand * > m_commands
 List of commands. More...
 
std::vector< CommandLineOption * > m_options
 List of options. More...
 
std::vector< CommandLineSwitch * > m_switches
 List of switches. More...
 
std::vector< CommandLineParameter * > m_parameters
 List of parameters. More...
 
bool m_optionalParametersAllowed
 true if optional parameters are allowed, else false More...
 
std::string m_optionalParameterName
 Name for optional parameters. More...
 
bool m_activated
 true if activated, else false More...
 
std::vector< std::string > m_optionalParameters
 List of optional parameters. More...
 
std::vector< std::string > m_errors
 List of parsing errors. More...
 

Detailed Description

Command line action.

Specifies an action that can be invoked on the command line. An action is defined by a set of items (commands, options, switches, and parameters), which may be present on the command line. If all non-optional commands, switches, and options are found, the action is invoked.

Example: myapp list <category> [<type>] [--show-details]

CommandLineAction actionList("list", "List objects");
CommandLineCommand cmdList("list")
actionList.add(&cmdList);
CommandLineParameter paramCategory(
"category",
CommandLineParameter::NonOptional
);
actionList.add(&paramCategory);
CommandLineParameter paramType("type", CommandLineParameter::Optional);
actionList.add(&paramType);
CommandLineSwitch swHelp(
"--show-details", "-d",
"Show detailed object information",
CommandLineSwitch::Optional
);
actionList.add(&swHelp);

Constructor & Destructor Documentation

◆ CommandLineAction()

cppassist::CommandLineAction::CommandLineAction ( const std::string &  name = "",
const std::string &  description = "" 
)

Constructor.

Parameters
[in]nameProgram name
[in]descriptionDescription text

◆ ~CommandLineAction()

virtual cppassist::CommandLineAction::~CommandLineAction ( )
virtual

Destructor.

Member Function Documentation

◆ name()

const std::string& cppassist::CommandLineAction::name ( ) const

Get action name.

Returns
Action name

◆ setName()

void cppassist::CommandLineAction::setName ( const std::string &  name)

Set action name.

Parameters
[in]nameAction name

◆ description()

const std::string& cppassist::CommandLineAction::description ( ) const

Get description.

Returns
Description text

◆ setDescription()

void cppassist::CommandLineAction::setDescription ( const std::string &  description)

Set description.

Parameters
[in]descriptionDescription text

◆ commands()

const std::vector<CommandLineCommand *>& cppassist::CommandLineAction::commands ( ) const

Get commands.

Returns
List of commands

◆ getCommand()

CommandLineCommand* cppassist::CommandLineAction::getCommand ( const std::string &  name) const

Get command by name.

Parameters
[in]nameCommand name
Returns
Pointer to command, null on error

◆ add() [1/4]

void cppassist::CommandLineAction::add ( CommandLineCommand command)

Add command.

Parameters
[in]commandCommand line command (must NOT be null!)

◆ options()

const std::vector<CommandLineOption *>& cppassist::CommandLineAction::options ( ) const

Get options.

Returns
List of options

◆ getOption()

CommandLineOption* cppassist::CommandLineAction::getOption ( const std::string &  name) const

Get option by name.

Parameters
[in]nameOption name
Returns
Pointer to option, null on error

◆ add() [2/4]

void cppassist::CommandLineAction::add ( CommandLineOption option)

Add option.

Parameters
[in]optionCommand line option (must NOT be null!)

◆ switches()

const std::vector<CommandLineSwitch *>& cppassist::CommandLineAction::switches ( ) const

Get switches.

Returns
List of switches

◆ getSwitch()

CommandLineSwitch* cppassist::CommandLineAction::getSwitch ( const std::string &  name) const

Get switch by name.

Parameters
[in]nameSwitch name
Returns
Pointer to switch, null on error

◆ add() [3/4]

void cppassist::CommandLineAction::add ( CommandLineSwitch sw)

Add switch.

Parameters
[in]swCommand line switch (must NOT be null!)

◆ parameters()

const std::vector<CommandLineParameter *>& cppassist::CommandLineAction::parameters ( ) const

Get parameters.

Returns
List of parameters

◆ getParameter() [1/2]

CommandLineParameter* cppassist::CommandLineAction::getParameter ( const std::string &  name) const

Get parameter by name.

Parameters
[in]nameParameter name
Returns
Pointer to parameter, null on error

◆ getParameter() [2/2]

CommandLineParameter* cppassist::CommandLineAction::getParameter ( size_t  index) const

Get parameter by index.

Parameters
[in]indexParameter index
Returns
Pointer to parameter, null on error

◆ add() [4/4]

void cppassist::CommandLineAction::add ( CommandLineParameter parameter)

Add parameter.

Parameters
[in]parameterCommand line parameter (must NOT be null!)

◆ optionalParametersAllowed()

bool cppassist::CommandLineAction::optionalParametersAllowed ( ) const

Check if optional parameters are allowed by the action.

Returns
true if optional parameters are allowed, else false

◆ setOptionalParametersAllowed()

void cppassist::CommandLineAction::setOptionalParametersAllowed ( bool  allowed)

Set if optional parameters are allowed by the action.

Parameters
[in]allowedtrue if optional parameters are allowed, else false

◆ optionalParameterName()

const std::string& cppassist::CommandLineAction::optionalParameterName ( ) const

Get optional parameter name.

Returns
Parameter name (e.g., "path")

◆ setOptionalParameterName()

void cppassist::CommandLineAction::setOptionalParameterName ( const std::string &  name)

Set optional parameter name.

Parameters
[in]nameParameter name (e.g., "path")

◆ usage()

std::string cppassist::CommandLineAction::usage ( ) const

Get usage text.

Returns
Usage text

◆ reset()

void cppassist::CommandLineAction::reset ( )

Reset state (forget all information from previous parsings)

◆ parse()

void cppassist::CommandLineAction::parse ( int  argc,
char *  argv[] 
)

Parse command line.

Parameters
[in]argcNumber of arguments
[in]argvList of arguments

◆ hasErrors()

bool cppassist::CommandLineAction::hasErrors ( ) const

Check if there were any errors during parsing.

Returns
true if errors have been found, else false

◆ errors()

const std::vector<std::string>& cppassist::CommandLineAction::errors ( ) const

Get errors.

Returns
List of parsing errors

◆ activated()

bool cppassist::CommandLineAction::activated ( ) const

Check if action has been activated.

Returns
true if activated, else false

◆ optionalParameters()

const std::vector<std::string>& cppassist::CommandLineAction::optionalParameters ( ) const

Get optional parameters specified on the command line.

Returns
List of optional parameters

◆ execute()

virtual int cppassist::CommandLineAction::execute ( )
virtual

Execute action.

Returns
Error code (0 on success)

◆ checkActivated()

bool cppassist::CommandLineAction::checkActivated ( )
protected

Check if this action has been activated.

Returns
true if activated, else false

◆ checkErrors()

void cppassist::CommandLineAction::checkErrors ( )
protected

Checks for errors and collects error messages.

See also
errors

Member Data Documentation

◆ m_name

std::string cppassist::CommandLineAction::m_name
protected

Action name.

◆ m_description

std::string cppassist::CommandLineAction::m_description
protected

Description text.

◆ m_commands

std::vector<CommandLineCommand *> cppassist::CommandLineAction::m_commands
protected

List of commands.

◆ m_options

std::vector<CommandLineOption *> cppassist::CommandLineAction::m_options
protected

List of options.

◆ m_switches

std::vector<CommandLineSwitch *> cppassist::CommandLineAction::m_switches
protected

List of switches.

◆ m_parameters

std::vector<CommandLineParameter *> cppassist::CommandLineAction::m_parameters
protected

List of parameters.

◆ m_optionalParametersAllowed

bool cppassist::CommandLineAction::m_optionalParametersAllowed
protected

true if optional parameters are allowed, else false

◆ m_optionalParameterName

std::string cppassist::CommandLineAction::m_optionalParameterName
protected

Name for optional parameters.

◆ m_activated

bool cppassist::CommandLineAction::m_activated
protected

true if activated, else false

◆ m_optionalParameters

std::vector<std::string> cppassist::CommandLineAction::m_optionalParameters
protected

List of optional parameters.

◆ m_errors

std::vector<std::string> cppassist::CommandLineAction::m_errors
protected

List of parsing errors.


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