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

Command line program. More...

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

Collaboration diagram for cppassist::CommandLineProgram:
Collaboration graph
[legend]

Public Member Functions

 CommandLineProgram (const std::string &name="", const std::string &shortDesc="", const std::string &description="")
 Constructor. More...
 
virtual ~CommandLineProgram ()
 Destructor. More...
 
const std::string & name () const
 Get program name. More...
 
void setName (const std::string &name)
 Set program name. More...
 
const std::string & shortDesc () const
 Get short description. More...
 
void setShortDesc (const std::string &shortDesc)
 Set short description. More...
 
const std::string & description () const
 Get description. More...
 
void setDescription (const std::string &description)
 Set description. More...
 
const std::vector< CommandLineAction * > & actions () const
 Get actions. More...
 
CommandLineActiongetAction (const std::string &name) const
 Get action by name. More...
 
void add (CommandLineAction *action)
 Add action. More...
 
virtual std::string help (CommandLineAction *forAction=nullptr) const
 Get help text. More...
 
virtual void print (const std::string &msg)
 Print message. More...
 
void reset ()
 Reset state (forget all information from previous parsings) More...
 
void parse (int argc, char *argv[])
 Parse command line. More...
 
int execute (int argc, char *argv[])
 Execute command line program. More...
 
virtual int executeAction (CommandLineAction *action)
 Execute command line action. More...
 
bool hasErrors () const
 Check if there were any errors during parsing. More...
 
CommandLineActionselectedAction () const
 Get action that has been selected. More...
 

Protected Attributes

std::string m_name
 Program name. More...
 
std::string m_shortDesc
 Short description (e.g., version and license) More...
 
std::string m_description
 Description text. More...
 
std::vector< CommandLineAction * > m_actions
 List of registered actions. More...
 
CommandLineActionm_selectedAction
 Action that has been selected (can be null) More...
 

Detailed Description

Command line program.

This class can be used to define the logic of a command line program.

Constructor & Destructor Documentation

◆ CommandLineProgram()

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

Constructor.

Parameters
[in]nameProgram name
[in]shortDescShort description (e.g., version and license)
[in]descriptionDescription text
Remarks
If no name is set, the name will be set internally to the value of argv[0] when calling parse().

◆ ~CommandLineProgram()

virtual cppassist::CommandLineProgram::~CommandLineProgram ( )
virtual

Destructor.

Member Function Documentation

◆ name()

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

Get program name.

Returns
Program name

◆ setName()

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

Set program name.

Parameters
[in]nameProgram name

◆ shortDesc()

const std::string& cppassist::CommandLineProgram::shortDesc ( ) const

Get short description.

Returns
Short description (e.g., version and license)

◆ setShortDesc()

void cppassist::CommandLineProgram::setShortDesc ( const std::string &  shortDesc)

Set short description.

Parameters
[in]shortDescShort description (e.g., version and license)

◆ description()

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

Get description.

Returns
Description text

◆ setDescription()

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

Set description.

Parameters
[in]descriptionDescription text

◆ actions()

const std::vector<CommandLineAction *>& cppassist::CommandLineProgram::actions ( ) const

Get actions.

Returns
List of actions

◆ getAction()

CommandLineAction* cppassist::CommandLineProgram::getAction ( const std::string &  name) const

Get action by name.

Parameters
[in]nameAction name
Returns
Pointer to action, null on error

◆ add()

void cppassist::CommandLineProgram::add ( CommandLineAction action)

Add action.

Parameters
[in]actionCommand line action (must NOT be null!)

◆ help()

virtual std::string cppassist::CommandLineProgram::help ( CommandLineAction forAction = nullptr) const
virtual

Get help text.

Parameters
[in]forActionAction for which help is requested, null for all actions
Returns
Help text
Remarks
By default, the help text contains the short description, the full program description, and the usage and options for each action. If help for a specific action is requested, only the usage and options for that action are given. You can change this behaviour by overriding this function.

◆ print()

virtual void cppassist::CommandLineProgram::print ( const std::string &  msg)
virtual

Print message.

Parameters
[in]msgMessage
Remarks
By default, the message is output to cppassist::info(). The method can be overridden to change that.

◆ reset()

void cppassist::CommandLineProgram::reset ( )

Reset state (forget all information from previous parsings)

◆ parse()

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

Parse command line.

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

◆ execute()

int cppassist::CommandLineProgram::execute ( int  argc,
char *  argv[] 
)

Execute command line program.

This function parses the command line and executes the selected action. If no action was selected or there were parsing error, the help text is displayed and and error code (1) is returned.

Parameters
[in]argcNumber of arguments
[in]argvList of arguments
Returns
Error code (0 on success)

◆ executeAction()

virtual int cppassist::CommandLineProgram::executeAction ( CommandLineAction action)
virtual

Execute command line action.

This function is called from execute(), when an action has been identified. Override this function to insert code that shall allways be executed (when no error has occured), regardless of the selected action. For example, a program can apply its global options here. Afterwards, the function is expected to call the specified action and return its error code.

Parameters
[in]actionCommand line action to execute
Returns
Error code (0 on success)

◆ hasErrors()

bool cppassist::CommandLineProgram::hasErrors ( ) const

Check if there were any errors during parsing.

Returns
true if errors have been found, else false

◆ selectedAction()

CommandLineAction* cppassist::CommandLineProgram::selectedAction ( ) const

Get action that has been selected.

Returns
Action, can be null

Member Data Documentation

◆ m_name

std::string cppassist::CommandLineProgram::m_name
protected

Program name.

◆ m_shortDesc

std::string cppassist::CommandLineProgram::m_shortDesc
protected

Short description (e.g., version and license)

◆ m_description

std::string cppassist::CommandLineProgram::m_description
protected

Description text.

◆ m_actions

std::vector<CommandLineAction *> cppassist::CommandLineProgram::m_actions
protected

List of registered actions.

◆ m_selectedAction

CommandLineAction* cppassist::CommandLineProgram::m_selectedAction
protected

Action that has been selected (can be null)


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