14 :p_enableHelpOption(enableHelpOption), p_programVersion(programVersion)
74 bool isRequired,
const PString & docString)
78 Option option(longOption, shortOption, value, isRequired, docString);
92 bool isRequired,
bool isAllowEmpty,
const PString & docString)
96 Option option(longOption, shortOption, value, isRequired, docString);
111 bool isRequired,
bool isAllowEmpty,
const PVecString & vecPossibleValue,
const PString & docString)
116 Option option(longOption, shortOption, value, isRequired, docString);
124 std::cout <<
"Usage :" << std::endl;
128 std::cout <<
"Parameters :" << std::endl;
129 VecMode::const_iterator it(
p_vecMode.begin());
171 if(it->getName() == name){
184 if(it->getName() == name){
197 VecMode::const_iterator it(
p_vecMode.begin());
198 while(isSearch && it !=
p_vecMode.end()){
199 isSearch &= it->getName() != name;
248 modeError =
" in mode '"+modeName+
"' ";
250 throw std::runtime_error(
"OptionParser::parseArgument : unknown option '"+parser.
getCurrentOption()+
"'" + modeError);
254 throw std::runtime_error(
"OptionParser::parseArgument : missing argument");
265 Option * partialOption = NULL;
268 VecMode::iterator itMode =
p_vecMode.begin();
270 isSearch = !itMode->parseOption(parser, partialOption);
288 std::cout << possibleValue << std::endl;
296 std::cout << possibleOption << std::endl;
311 VecMode::const_iterator it(
p_vecMode.begin());
313 isArgOk = it->checkArgument();
340 VecMode::const_iterator it(
p_vecMode.begin());
341 while(it !=
p_vecMode.end() && mode == NULL){
342 if(it->isCurrentlyParsed() && it->getName() !=
""){
357 PString valueToBeCompleted(
"");
377 if(cursorOption ==
""){
return NULL;}
379 VecMode::const_iterator itMode(
p_vecMode.begin());
380 while(itMode !=
p_vecMode.end() && op == NULL){
381 if(itMode->isCurrentlyParsed()){
382 const VecOption & vecOp = itMode->getVecOption();
383 VecOption::const_iterator itOp(vecOp.begin());
384 while(itOp != vecOp.end() && op == NULL){
385 PString fullOp(
"--" + itOp->getLongName() +
"=");
388 valueToBeCompleted = cursorOption.substr(fullOp.size());
405 if(prevCursorOption ==
""){
return NULL;}
407 VecMode::const_iterator itMode(
p_vecMode.begin());
408 while(itMode !=
p_vecMode.end() && op == NULL){
409 if(itMode->isCurrentlyParsed()){
410 const VecOption & vecOp = itMode->getVecOption();
411 VecOption::const_iterator itOp(vecOp.begin());
412 while(itOp != vecOp.end() && op == NULL){
413 PString fullLongOp(
"--" + itOp->getLongName()), fullShortOption(
"-" + itOp->getShortName());
414 if(fullLongOp == prevCursorOption){
416 valueToBeCompleted = cursorOption;
417 }
else if(fullShortOption == prevCursorOption){
419 valueToBeCompleted = cursorOption;
435 p_vecMode.front().getPossibleOption(possibleOption, cursorOption);
438 if(currentlyParsedMode != NULL){
441 for(VecMode::const_iterator itMode =
p_vecMode.begin(); itMode !=
p_vecMode.end(); ++itMode){
442 itMode->getPossibleMode(possibleOption, cursorOption);
453 std::vector<PString> vecOtherOption;
454 vecOtherOption.push_back(
"--help");
455 vecOtherOption.push_back(
"-h");
456 vecOtherOption.push_back(
"--version");
457 vecOtherOption.push_back(
"-v");
459 for(std::vector<PString>::iterator it(vecOtherOption.begin()); it != vecOtherOption.end(); ++it){
461 if(cursorOption ==
""){
462 possibleOption += optionStr +
" ";
465 possibleOption += optionStr +
" ";
std::vector< Option > VecOption
Vector of option.
std::vector< PString > PVecString
Parse the list of arguments passed to a program.
const PString & getCurrentOption() const
Get the current option.
const PString & getCursorOption() const
Get the cursor option given to the program, in bash completion mode.
bool isBashCompletionMode() const
Say if the program is in bash completion mode.
void getNextOption()
Move to the next option.
const PString & getPrevCursorOption() const
Get the previous option before the cursor option.
bool isEndOfOption() const
Say if is it the end of the options.
Describe a mode in the program arguments.
void getPossibleOption(PString &possibleOption, const PString &cursorOption) const
Get the possible options for the bash completion.
void setEnableHelpOption(bool b)
Set the attribtue which enables help option.
void setProgramVersion(const PString &programVersion)
Set the program version.
bool parseOption(ArgParser &parser)
Parse the options in the current OptionMode.
const PString & getName() const
Get the name of the OptionMode.
Parse the options passed to a program.
void parseArgument(int argc, char **argv)
Parse the arguments passed to the program.
void addOption(const PString &longOption, const PString &shortOption, OptionType::OptionType optionType, bool isRequired, const PString &docString)
Add an option in the OptionParser.
PString p_exempleLongOption
Usage example with long options.
const Option * getSplitOptionValue(PString &valueToBeCompleted, const PString &cursorOption, const PString &prevCursorOption) const
Get the split option (without =) value to be completed.
OptionParser & operator=(const OptionParser &other)
Definition of equal operator of OptionParser.
bool p_enableHelpOption
True to enable automatically the printing of the help option when the program is called with –help or...
const OptionMode & getMode(const PString &name) const
Get mode by name.
void setExampleShortOption(const PString &example)
Set the example usage of the program.
void closeMode()
Close the current mode and go back to be default one.
void addMode(const PString &modeName)
Add a mode in the option.
void print() const
Print all the options.
virtual ~OptionParser()
Destructeur of OptionParser.
bool isModeExist(const PString &name) const
Check if the given mode name does exist.
VecMode p_vecMode
Vector of all the defined mode in the OptionParser.
void copyOptionParser(const OptionParser &other)
Copy function of OptionParser.
bool completeOptionValue(PString &possibleValue, const PString &cursorOption, const PString &prevCursorOption) const
Complete the possible value of an option (FILENAME, DIRECTORY, FILE_OR_DIR)
bool checkArgument() const
Check the argument of the parser.
void getPossibleOtherOption(PString &possibleOption, const PString &cursorOption) const
Get the possible other options which can be used.
size_t p_currentMode
Index of the current mode in the OptionParser.
void getPossibleOption(PString &possibleOption, const PString &cursorOption) const
Get the possible options which can be used.
OptionMode & getParserMode(ArgParser &parser)
Get a mode if it exist.
void initialisationOptionParser()
Initialisation function of the class OptionParser.
OptionMode * p_currentParserMode
Current mode parsed.
const Option * getLongOptionValue(PString &valueToBeCompleted, const PString &cursorOption) const
Get the long option value to be completed.
OptionParser(bool enableHelpOption=true, const PString &programVersion="")
Default constructeur of OptionParser.
const OptionMode * getCurrentlyParsedMode() const
Get the currently parsed OptionMode.
void setExampleLongOption(const PString &example)
Set the example usage of the program.
PString p_programVersion
Program version to be printed on –version or -v option.
PString p_exempleShortOption
Usage example with short options.
void parseArgumentNormalUse(ArgParser &parser)
Classical argument parsing mode.
const OptionMode & getDefaultMode() const
Get default mode.
void parseArgumentBashCompletion(ArgParser &parser)
Bash completion argument parsing mode.
Describe the value of an option passed to a program.
void setVecPossibleValue(const PVecString &vecPossibleValue)
Set the vector of possible values.
void setType(OptionType::OptionType type)
Set the type of the OptionValue.
Describes an option passed to a program.
void setIsAllowEmpty(bool isAllowEmpty)
Say if the option can be empty or not.
void getPossibleValue(PString &possibleValue, const PString &cursorOption) const
Complete the possible values of the Option.
bool isSameBegining(const PString &beginStr) const
Say if the current PString has the same begining of beginStr.