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 ¶m) |
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 ¶m) |
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 ¶m) |
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 ¶meterValue) |
Get the value of the parameter. More... | |
void | getParameterValue (const std::string &execParamToCheck, int ¶meterValue) |
Get the value of the parameter. More... | |
void | getParameterValue (const std::string &execParamToCheck, size_t ¶meterValue) |
Get the value of the parameter. More... | |
void | getParameterValue (const std::string &execParamToCheck, float ¶meterValue) |
Get the value of the parameter. More... | |
void | getParameterValue (const std::string &execParamToCheck, std::string ¶meterValue) |
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< Parameter > | readConfigFile (const std::string &inputConfigFile, bool getDescription=true) |
Reads a pre-written configuration file using CmdParser::WriteConfigFile() More... | |
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 );
|
explicit |
The Constructor.
argc | The "argc" from executable |
argv | The "argv" from executable |
exe_name | Name of the executable, defaults to picking up from cbica::getExecutableName() |
|
explicit |
The Constructor.
argc | The "argc" from executable |
argv | The "argv" from executable |
exe_name | Name of the executable, defaults to picking up from cbica::getExecutableName() |
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.
commandExcludingExeName | A string which would correspond to the command line usage AFTER the executable has been called |
descriptionOfCommand | A string which would correspond to what the command is expected to do |
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.
laconic | The laconic variant |
verbose | The verbose variant |
expectedDataType | The data type expected for this parameter |
dataRange | The range of data expected for this parameter |
description_line1 | The first line of description for parameter |
description_line2 | The second line of description for parameter, defaults to a blank string |
description_line3 | The third line of description for parameter, defaults to a blank string |
description_line4 | The fourth line of description for parameter, defaults to a blank string |
description_line5 | The fifth line of description for parameter, defaults to a blank string |
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.
laconic | The laconic variant |
verbose | The verbose variant |
expectedDataType | The data type expected for this parameter |
dataRange | The range of data expected for this parameter |
description_line1 | The first line of description for parameter |
description_line2 | The second line of description for parameter, defaults to a blank string |
description_line3 | The third line of description for parameter, defaults to a blank string |
description_line4 | The fourth line of description for parameter, defaults to a blank string |
description_line5 | The fifth line of description for parameter, defaults to a blank string |
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.
laconic | The laconic variant |
verbose | The verbose variant |
expectedDataType | The data type expected for this parameter |
dataRange | The range of data expected for this parameter |
description_line1 | The first line of description for parameter |
description_line2 | The second line of description for parameter, defaults to a blank string |
description_line3 | The third line of description for parameter, defaults to a blank string |
description_line4 | The fourth line of description for parameter, defaults to a blank string |
description_line5 | The fifth line of description for parameter, defaults to a blank string |
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.
execParamToCheck | Which parameter to check |
position | Position of parameter in argv else -1 |
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.
execParamToCheck | Which parameter to check |
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.
usageOfExe | A string which would correspond to the command line usage AFTER the executable has been called |
int getDataTypeAsEnumCode | ( | const std::string & | execParamToCheck | ) |
Get the data type analogous with the parameter.
Can search using both laconic and verbose parameters.
parameter | Parameter whose description is requested |
std::string getDataTypeAsString | ( | const std::string & | execParamToCheck | ) |
Get the data type analogous with the parameter.
Can search using both laconic and verbose parameters.
parameter | Parameter whose description is requested |
std::string getDescription | ( | const std::string & | execParamToCheck, |
bool | NewLine | ||
) |
Get the description analogous with the parameter.
Can search using both laconic and verbose parameters.
parameter | Parameter whose description is requested |
NewLine Return with |
std::string getLaconic | ( | const std::string & | execParamToCheck | ) |
Get the laconic value from verbose.
Searches using the verbose value.
execParamToCheck | The verbose variant of the parameter |
void getParameterValue | ( | const std::string & | execParamToCheck, |
bool & | parameterValue | ||
) |
Get the value of the parameter.
Can search using both laconic and verbose parameters.
execParamToCheck | The laconic or verbose variant of the parameter |
parameterValue | The return value of the parameter as bool |
void getParameterValue | ( | const std::string & | execParamToCheck, |
int & | parameterValue | ||
) |
Get the value of the parameter.
Can search using both laconic and verbose parameters.
execParamToCheck | The laconic or verbose variant of the parameter |
parameterValue | The return value of the parameter as int |
void getParameterValue | ( | const std::string & | execParamToCheck, |
size_t & | parameterValue | ||
) |
Get the value of the parameter.
Can search using both laconic and verbose parameters.
execParamToCheck | The laconic or verbose variant of the parameter |
parameterValue | The return value of the parameter as size_t |
void getParameterValue | ( | const std::string & | execParamToCheck, |
float & | parameterValue | ||
) |
Get the value of the parameter.
Can search using both laconic and verbose parameters.
execParamToCheck | The laconic or verbose variant of the parameter |
parameterValue | The return value of the parameter as float |
void getParameterValue | ( | const std::string & | execParamToCheck, |
std::string & | parameterValue | ||
) |
Get the value of the parameter.
Can search using both laconic and verbose parameters.
execParamToCheck | The laconic or verbose variant of the parameter |
parameterValue | The return value of the parameter as std::string (valid for Parameter::Type::FILE, Parameter::Type::DIRECTORY, Parameter::Type::STRING) |
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.
execParamToCheck | Which parameter to check |
|
static |
Reads a pre-written configuration file using CmdParser::WriteConfigFile()
inputConfigFile | Full path to the configuration file which needs to be read |
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'.
dirName | The full path of the directory to save the file; defaults to directory specified in cbica::makeTempDir() |
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.
dirName | Full directory path to where the CWL spec will be produced |
workflowName | For more advanced CWL workflows |
overwriteFile | If the current file should be overwritten or not |