CBICA Toolkit  1.0.0
CmdParser Class Reference

Simple command line parsing. More...

#include <cbicaCmdParser.h>

Public Member Functions

void addApplicationDescription (const std::string &description)
 Adds descrition for the application for which the class is being initialized.
 
void addExampleUsage (const std::string &commandExcludingExeName, const std::string &descriptionOfCommand)
 Gives a brief example of how to use the executable. More...
 
void addInputs (const std::string &param)
 
void addOptionalParameter (const std::string &laconic, const std::string &verbose, const int &expectedDataType, const std::string &dataRange, const std::string &description_line1, const std::string &description_line2="", const std::string &description_line3="", const std::string &description_line4="", const std::string &description_line5="")
 Adding Optional parameters. More...
 
void addOutputs (const std::string &param)
 
void addParameter (const std::string &laconic, const std::string &verbose, const int &expectedDataType, const std::string &dataRange, const std::string &description_line1, const std::string &description_line2="", const std::string &description_line3="", const std::string &description_line4="", const std::string &description_line5="")
 Adding parameters: defaults to optional parameters. More...
 
void addRequiredParameter (const std::string &laconic, const std::string &verbose, const int &expectedDataType, const std::string &dataRange, const std::string &description_line1, const std::string &description_line2="", const std::string &description_line3="", const std::string &description_line4="", const std::string &description_line5="")
 Adding Required parameters. More...
 
std::string checkDefault (const std::string &param)
 
 CmdParser (const int argc, char **argv, const std::string &exe_name="")
 The Constructor. More...
 
 CmdParser (const int argc, const char **argv, const std::string &exe_name="")
 The Constructor. More...
 
bool compareParameter (const std::string &execParamToCheck, int &position, bool automaticEcho=true)
 Check parameters WITHOUT hyphens. More...
 
bool compareParameter (const std::string &execParamToCheck, bool automaticEcho=true)
 Check parameters WITHOUT hyphens. More...
 
void createNode (const std::string &nodeString)
 
void cwlrunner (const std::string &cwl_spec_path, const std::string &cwl_input_path, bool getDefaultFlag)
 
void deleteNode (const std::string &nodeString)
 
void echoHelp ()
 Display verbose usage.
 
void echoUsage ()
 Display the usage.
 
void echoVersion ()
 Display the version details.
 
DEPRECATED void exampleUsage (const std::string &usageOfExe)
 Gives a brief example of how to use the executable. More...
 
std::string GetCommandFromCWL (const std::string &inpDir, const std::string &cwlDir)
 
int getDataTypeAsEnumCode (const std::string &execParamToCheck)
 Get the data type analogous with the parameter. More...
 
std::string getDataTypeAsString (const std::string &execParamToCheck)
 Get the data type analogous with the parameter. More...
 
std::string getDescription (const std::string &execParamToCheck, bool NewLine)
 Get the description analogous with the parameter. More...
 
std::string getExeName ()
 \get the executable name
 
std::string getLaconic (const std::string &execParamToCheck)
 Get the laconic value from verbose. More...
 
void getParameterValue (const std::string &execParamToCheck, bool &parameterValue)
 Get the value of the parameter. More...
 
void getParameterValue (const std::string &execParamToCheck, int &parameterValue)
 Get the value of the parameter. More...
 
void getParameterValue (const std::string &execParamToCheck, size_t &parameterValue)
 Get the value of the parameter. More...
 
void getParameterValue (const std::string &execParamToCheck, float &parameterValue)
 Get the value of the parameter. More...
 
void getParameterValue (const std::string &execParamToCheck, std::string &parameterValue)
 Get the value of the parameter. More...
 
void ignoreArgc1 ()
 This function ensures that argc < 2 isn't checked.
 
bool isPresent (const std::string &execParamToCheck, bool automaticEcho=true)
 Check if supplied parameter is present in the argument list. More...
 
void readCWLFile (const std::string &path_to_config_file, bool getDescription)
 
void setExeName (const std::string exeName)
 Set a custom executable name.
 
void writeConfigFile (const std::string &dirName="")
 Write the parser configuration as a JSON file. More...
 
void writeCWLFile (const std::string &dirName, bool overwriteFile)
 Writes out a CWL specification file from cmd parser. More...
 
virtual ~CmdParser ()
 The Destructor.
 

Static Public Member Functions

static std::vector< ParameterreadConfigFile (const std::string &inputConfigFile, bool getDescription=true)
 Reads a pre-written configuration file using CmdParser::WriteConfigFile() More...
 

Detailed Description

Simple command line parsing.

This is a pure c++ implementation. Executable name and project version are picked up automatically from the main CMakeLists file. Only the executable name can be modified in this class.

An example of usage is shown below:

cbica::CmdParser parser = cbica::CmdParser(argc, argv); // OR,
//cbica::CmdParser parser = cbica::CmdParser(argc, argv, "exe_name"); // if a different exe_name is desired

/// The parameters "u"/"usage", "h"/"help" and "v"/"version" are automatically added ///

// add parameters to the variable
parser.addOptionalParameter("m","marvel", cbica::Parameter::INTEGER, "1 to 10", "I like The Avengers");
parser.addOptionalParameter("d", "dc", cbica::Parameter::FLOAT, "1.00 to 10.00", "I prefer the Justice League");
parser.addRequiredParameter("p", "people", cbica::Parameter::STRING, "max length = 1024", "People are always required");

// note that there should be no spaces for either of the parameter types; they will be removed.

/// checks for required parameters are done internally.

std::string peopleString;
parser.getParameterValue("p", peopleString);

int marvelValue = 5; // set default value
parser.getParameterValue("m", marvelValue);

float dcValue = 5.15; // set default value
parser.getParameterValue("d", dcValue);

doSomethingWithTheParameters( peopleString, marvelValue, dcValue );

Constructor & Destructor Documentation

◆ CmdParser() [1/2]

CmdParser ( const int  argc,
char **  argv,
const std::string &  exe_name = "" 
)
explicit

The Constructor.

Parameters
argcThe "argc" from executable
argvThe "argv" from executable
exe_nameName of the executable, defaults to picking up from cbica::getExecutableName()

◆ CmdParser() [2/2]

CmdParser ( const int  argc,
const char **  argv,
const std::string &  exe_name = "" 
)
explicit

The Constructor.

Parameters
argcThe "argc" from executable
argvThe "argv" from executable
exe_nameName of the executable, defaults to picking up from cbica::getExecutableName()

Member Function Documentation

◆ addExampleUsage()

void addExampleUsage ( const std::string &  commandExcludingExeName,
const std::string &  descriptionOfCommand 
)

Gives a brief example of how to use the executable.

This should not contain any references to the executable name (it is automatically picked up). It should start directly with the parameters to be put in.

Parameters
commandExcludingExeNameA string which would correspond to the command line usage AFTER the executable has been called
descriptionOfCommandA string which would correspond to what the command is expected to do

◆ addOptionalParameter()

void addOptionalParameter ( const std::string &  laconic,
const std::string &  verbose,
const int &  expectedDataType,
const std::string &  dataRange,
const std::string &  description_line1,
const std::string &  description_line2 = "",
const std::string &  description_line3 = "",
const std::string &  description_line4 = "",
const std::string &  description_line5 = "" 
)

Adding Optional parameters.

As a standard, neither the laconic nor verbose parameters should have any '-' in the constructor.

Parameters
laconicThe laconic variant
verboseThe verbose variant
expectedDataTypeThe data type expected for this parameter
dataRangeThe range of data expected for this parameter
description_line1The first line of description for parameter
description_line2The second line of description for parameter, defaults to a blank string
description_line3The third line of description for parameter, defaults to a blank string
description_line4The fourth line of description for parameter, defaults to a blank string
description_line5The fifth line of description for parameter, defaults to a blank string

◆ addParameter()

void addParameter ( const std::string &  laconic,
const std::string &  verbose,
const int &  expectedDataType,
const std::string &  dataRange,
const std::string &  description_line1,
const std::string &  description_line2 = "",
const std::string &  description_line3 = "",
const std::string &  description_line4 = "",
const std::string &  description_line5 = "" 
)

Adding parameters: defaults to optional parameters.

As a standard, neither the laconic nor verbose parameters should have any '-' in the constructor.

Parameters
laconicThe laconic variant
verboseThe verbose variant
expectedDataTypeThe data type expected for this parameter
dataRangeThe range of data expected for this parameter
description_line1The first line of description for parameter
description_line2The second line of description for parameter, defaults to a blank string
description_line3The third line of description for parameter, defaults to a blank string
description_line4The fourth line of description for parameter, defaults to a blank string
description_line5The fifth line of description for parameter, defaults to a blank string

◆ addRequiredParameter()

void addRequiredParameter ( const std::string &  laconic,
const std::string &  verbose,
const int &  expectedDataType,
const std::string &  dataRange,
const std::string &  description_line1,
const std::string &  description_line2 = "",
const std::string &  description_line3 = "",
const std::string &  description_line4 = "",
const std::string &  description_line5 = "" 
)

Adding Required parameters.

As a standard, neither the laconic nor verbose parameters should have any '-' in the constructor.

Parameters
laconicThe laconic variant
verboseThe verbose variant
expectedDataTypeThe data type expected for this parameter
dataRangeThe range of data expected for this parameter
description_line1The first line of description for parameter
description_line2The second line of description for parameter, defaults to a blank string
description_line3The third line of description for parameter, defaults to a blank string
description_line4The fourth line of description for parameter, defaults to a blank string
description_line5The fifth line of description for parameter, defaults to a blank string

◆ compareParameter() [1/2]

bool compareParameter ( const std::string &  execParamToCheck,
int &  position,
bool  automaticEcho = true 
)

Check parameters WITHOUT hyphens.

Checks for both laconic and verbose variants of the specified parameter.

Parameters
execParamToCheckWhich parameter to check
positionPosition of parameter in argv else -1
Returns
True if parameter found else False

◆ compareParameter() [2/2]

bool compareParameter ( const std::string &  execParamToCheck,
bool  automaticEcho = true 
)

Check parameters WITHOUT hyphens.

Checks for both laconic and verbose variants of the specified parameter. Can be used to see if the parameter is present or not.

Parameters
execParamToCheckWhich parameter to check
Returns
True if parameter found else False

◆ exampleUsage()

DEPRECATED void exampleUsage ( const std::string &  usageOfExe)

Gives a brief example of how to use the executable.

This should not contain any references to the executable name (it is automatically picked up). It should start directly with the parameters to be put in.

Parameters
usageOfExeA string which would correspond to the command line usage AFTER the executable has been called

◆ getDataTypeAsEnumCode()

int getDataTypeAsEnumCode ( const std::string &  execParamToCheck)

Get the data type analogous with the parameter.

Can search using both laconic and verbose parameters.

Parameters
parameterParameter whose description is requested
Returns
Description of parameter as Enum Code (Parameter::Type)

◆ getDataTypeAsString()

std::string getDataTypeAsString ( const std::string &  execParamToCheck)

Get the data type analogous with the parameter.

Can search using both laconic and verbose parameters.

Parameters
parameterParameter whose description is requested
Returns
Description of parameter as string

◆ getDescription()

std::string getDescription ( const std::string &  execParamToCheck,
bool  NewLine 
)

Get the description analogous with the parameter.

Can search using both laconic and verbose parameters.

Parameters
parameterParameter whose description is requested
NewLine Return with

◆ getLaconic()

std::string getLaconic ( const std::string &  execParamToCheck)

Get the laconic value from verbose.

Searches using the verbose value.

Parameters
execParamToCheckThe verbose variant of the parameter

◆ getParameterValue() [1/5]

void getParameterValue ( const std::string &  execParamToCheck,
bool &  parameterValue 
)

Get the value of the parameter.

Can search using both laconic and verbose parameters.

Parameters
execParamToCheckThe laconic or verbose variant of the parameter
parameterValueThe return value of the parameter as bool

◆ getParameterValue() [2/5]

void getParameterValue ( const std::string &  execParamToCheck,
int &  parameterValue 
)

Get the value of the parameter.

Can search using both laconic and verbose parameters.

Parameters
execParamToCheckThe laconic or verbose variant of the parameter
parameterValueThe return value of the parameter as int

◆ getParameterValue() [3/5]

void getParameterValue ( const std::string &  execParamToCheck,
size_t &  parameterValue 
)

Get the value of the parameter.

Can search using both laconic and verbose parameters.

Parameters
execParamToCheckThe laconic or verbose variant of the parameter
parameterValueThe return value of the parameter as size_t

◆ getParameterValue() [4/5]

void getParameterValue ( const std::string &  execParamToCheck,
float &  parameterValue 
)

Get the value of the parameter.

Can search using both laconic and verbose parameters.

Parameters
execParamToCheckThe laconic or verbose variant of the parameter
parameterValueThe return value of the parameter as float

◆ getParameterValue() [5/5]

void getParameterValue ( const std::string &  execParamToCheck,
std::string &  parameterValue 
)

Get the value of the parameter.

Can search using both laconic and verbose parameters.

Parameters
execParamToCheckThe laconic or verbose variant of the parameter
parameterValueThe return value of the parameter as std::string (valid for Parameter::Type::FILE, Parameter::Type::DIRECTORY, Parameter::Type::STRING)

◆ isPresent()

bool isPresent ( const std::string &  execParamToCheck,
bool  automaticEcho = true 
)

Check if supplied parameter is present in the argument list.

Checks for both laconic and verbose variants of the specified parameter. Uses compareParameter() internally.

Parameters
execParamToCheckWhich parameter to check
Returns
True if parameter found else False

◆ readConfigFile()

static std::vector< Parameter > readConfigFile ( const std::string &  inputConfigFile,
bool  getDescription = true 
)
static

Reads a pre-written configuration file using CmdParser::WriteConfigFile()

Parameters
inputConfigFileFull path to the configuration file which needs to be read
Returns
Vector of the Parameter structure where laconic paramter is always empty for all variables

◆ writeConfigFile()

void writeConfigFile ( const std::string &  dirName = "")

Write the parser configuration as a JSON file.

Write the configuration file for the executable for use in the common GUI framework

The generated config file is always named 'EXE_NAME.txt'.

Parameters
dirNameThe full path of the directory to save the file; defaults to directory specified in cbica::makeTempDir()

◆ writeCWLFile()

void writeCWLFile ( const std::string &  dirName,
bool  overwriteFile 
)

Writes out a CWL specification file from cmd parser.

This should be invoked everytime the cmd parser is called for an application.

Parameters
dirNameFull directory path to where the CWL spec will be produced
workflowNameFor more advanced CWL workflows
overwriteFileIf the current file should be overwritten or not

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