Public Member Functions | List of all members
openfluid::utils::CommandLineParser Class Reference

Class for management of command line arguments. More...

#include <utils/CommandLineParser.hpp>

Public Member Functions

 CommandLineParser ()
 
 CommandLineParser (const std::string &ProgramName, const std::string &HelpText)
 
const std::vector< std::string > & extraArgs () const
 
std::string getHelpText () const
 
std::string getProgramName () const
 
std::string getParsingMessage () const
 
std::string getActiveCommand () const
 
const CommandLineCommandcommand (const std::string &Name) const
 
void addCommand (const CommandLineCommand &Command)
 
void addOption (const CommandLineOption &Option)
 
bool parse (std::list< std::string > ArgValues)
 
bool parse (int ArgC, char **ArgV)
 
void reset ()
 
bool isHelpAsked ()
 
void printHelp (std::ostream &OutStm)
 
void printState (std::ostream &OutStm)
 

Detailed Description

Class for management of command line arguments.

This class allows to manage the command line arguments as commands, options and extra arguments.

The example below is for a command line program with two commands ('run' and 'search') and associated options

int main(int argc, char** argv)
{
// ------- Definition of the parser for "myprogram"
openfluid::utils::CommandLineParser Parser("myprogram","this is my program");
// add of the "run" command with associated options
openfluid::utils::CommandLineCommand RunCmd("run","run the process");
RunCmd.addOption(openfluid::utils::CommandLineOption("input-path","i","input path",true));
RunCmd.addOption(openfluid::utils::CommandLineOption("output-path","o","output path",true));
RunCmd.addOption(openfluid::utils::CommandLineOption("verbose","","verbose mode"));
Parser.addCommand(RunCmd);
// add of the "search" command with associated options
openfluid::utils::CommandLineCommand SearchCmd("search","search for data");
SearchCmd.addOption(openfluid::utils::CommandLineOption("path","p","path to search",true));
SearchCmd.addOption(openfluid::utils::CommandLineOption("extended","e","enable extended search"));
Parser.addCommand(SearchCmd);
// ------- Parsing of the given arguments
Parser.parse(argc,argv);
// ------- Processing of commands and options
std::string ActiveCommand = Parser.getActiveCommand();
if (ActiveCommand.empty())
{
// .. code here when no command
}
else if (ActiveCommand == "run")
{
// .. code here for "run" command
if (Parser.command(ActiveCommand).isOptionActive("input-path"))
{
// .. code here for "input-path" option
}
if (Parser.command(ActiveCommand).isOptionActive("output-path"))
{
// .. code here for "output-path" option
}
// ...
}
else if (ActiveCommand == "search")
{
// .. code here for "search" command
}
}

Constructor & Destructor Documentation

◆ CommandLineParser() [1/2]

openfluid::utils::CommandLineParser::CommandLineParser ( )
inline

◆ CommandLineParser() [2/2]

openfluid::utils::CommandLineParser::CommandLineParser ( const std::string &  ProgramName,
const std::string &  HelpText 
)
inline

Instanciates a command line parser with the given parameters

Parameters
[in]ProgramNameThe name of the programn
[in]HelpTextThe help text associated to the option

Member Function Documentation

◆ addCommand()

void openfluid::utils::CommandLineParser::addCommand ( const CommandLineCommand Command)
inline

Adds a command to the parser

Parameters
[in]CommandThe command to add

◆ addOption()

void openfluid::utils::CommandLineParser::addOption ( const CommandLineOption Option)
inline

Adds a global option to the parser

Parameters
[in]OptionThe option to add

◆ command()

const CommandLineCommand& openfluid::utils::CommandLineParser::command ( const std::string &  Name) const
inline

Returns the command corresponding to the given name

Parameters
[in]Namethe name of the command
Returns
The command object

◆ extraArgs()

const std::vector<std::string>& openfluid::utils::CommandLineParser::extraArgs ( ) const
inline

Returns the extra arguments given to the command

Returns
a vector of arguments

◆ getActiveCommand()

std::string openfluid::utils::CommandLineParser::getActiveCommand ( ) const
inline

Returns the active command name

Returns
The active command name

◆ getHelpText()

std::string openfluid::utils::CommandLineParser::getHelpText ( ) const
inline

Returns the help text of the parser

Returns
The help text

◆ getParsingMessage()

std::string openfluid::utils::CommandLineParser::getParsingMessage ( ) const
inline

Returns the message given by the parsing process in case of error(s)

Returns
The message

◆ getProgramName()

std::string openfluid::utils::CommandLineParser::getProgramName ( ) const
inline

Returns the program name

Returns
The program name

◆ isHelpAsked()

bool openfluid::utils::CommandLineParser::isHelpAsked ( )
inline

Tests if the help is asked

Returns
true if the help is asked

◆ parse() [1/2]

bool openfluid::utils::CommandLineParser::parse ( std::list< std::string >  ArgValues)
inline

Parses a list of string arguments

Parameters
[in]ArgValuesThe lists of arguments to parse
Returns
true if the parsing is succesful, false otherwise

◆ parse() [2/2]

bool openfluid::utils::CommandLineParser::parse ( int  ArgC,
char **  ArgV 
)
inline

Parses arguments from the standard parameters (int argc,char** argv) given through the main function

Parameters
[in]ArgCThe number of arguments
[in]ArgVThe array of arguments
Returns
true if the parsing is succesful, false otherwise

◆ printHelp()

void openfluid::utils::CommandLineParser::printHelp ( std::ostream &  OutStm)
inline

Prints the help text

Parameters
[in]OutStmThe stream where the help text is printed (e.g. std::cout)

◆ printState()

void openfluid::utils::CommandLineParser::printState ( std::ostream &  OutStm)
inline

Prints the state of the parser

Parameters
[in]OutStmThe stream where the help text is printed (e.g. std::cout)

◆ reset()

void openfluid::utils::CommandLineParser::reset ( )
inline

Resets the parser to default (no active command, no extra arguments, no option activated)


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