Xmipp  v3.23.11-Nereus
Public Member Functions | Public Attributes | List of all members
ProgramDef Class Reference

#include <argsparser.h>

Inheritance diagram for ProgramDef:
Inheritance graph
[legend]
Collaboration diagram for ProgramDef:
Collaboration graph
[legend]

Public Member Functions

 ProgramDef ()
 
 ~ProgramDef ()
 
virtual bool parse ()
 
virtual void check (std::stringstream &errors)
 
ParamDeffindParam (const String &param)
 
ParamDeffindAndFillParam (const String &param)
 
const char * getParam (const char *paramName, size_t paramNumber=0)
 
const char * getParam (const char *paramName, const char *subParam, size_t paramNumber=0)
 
void addParamName (const String &name, ParamDef *param)
 
void addParamRequires (const String &name)
 
void addParamExclusiveGroup (ParamDef *param)
 
void clear ()
 clear read arguments More...
 
void read (int argc, const char **argv, bool reportErrors=true)
 Read and validate commmand line. More...
 
SectionDefaddSection (String sectionName, int visibility=0)
 
- Public Member Functions inherited from ASTNode
 ASTNode (ArgLexer *lexer=NULL, ASTNode *parent=NULL)
 
virtual ~ASTNode ()
 
virtual bool consume (ArgTokenType type)
 
ArgTokenType lookahead () const
 
bool lookahead (ArgTokenType type) const
 
ArgTokencurrentToken () const
 
void nextToken ()
 
bool parseCommentList (CommentList &comments)
 
void error (String msg)
 
void unexpectedToken (String msg="")
 

Public Attributes

std::vector< SectionDef * > sections
 
CommentList usageComments
 comments of usage More...
 
CommentList examples
 examples of use More...
 
std::map< String, ParamDef * > paramsMap
 Dictionary with all params and alias names. More...
 
StringVector pendingRequires
 This is for checking that requires names exists. More...
 
String keywords
 
String seeAlso
 
bool singleOption
 
- Public Attributes inherited from ASTNode
ASTNodeparent
 
ArgLexerpLexer
 
ArgToken token
 
String name
 
int visible
 

Detailed Description

Definition at line 236 of file argsparser.h.

Constructor & Destructor Documentation

◆ ProgramDef()

ProgramDef::ProgramDef ( )

Definition at line 810 of file argsparser.cpp.

810  :
811  ASTNode()
812 {
813  pLexer = new ArgLexer();
814  singleOption = false;
815  exclusiveGroup = NULL;
816 }
bool singleOption
Definition: argsparser.h:250
ArgLexer * pLexer
Definition: argsparser.h:150
ASTNode(ArgLexer *lexer=NULL, ASTNode *parent=NULL)
Definition: argsparser.cpp:349

◆ ~ProgramDef()

ProgramDef::~ProgramDef ( )

Definition at line 818 of file argsparser.cpp.

819 {
820  delete pLexer;
821  for (size_t i = 0; i < sections.size(); ++i)
822  delete sections[i];
823 }
std::vector< SectionDef * > sections
Definition: argsparser.h:241
#define i
ArgLexer * pLexer
Definition: argsparser.h:150

Member Function Documentation

◆ addParamExclusiveGroup()

void ProgramDef::addParamExclusiveGroup ( ParamDef param)

Definition at line 925 of file argsparser.cpp.

926 {
927  if (exclusiveGroup == NULL || param->orBefore == false)
928  exclusiveGroup = new std::vector<ParamDef*>();
929  exclusiveGroup->push_back(param);
930  param->exclusiveGroup = exclusiveGroup;
931 }
std::vector< ParamDef * > * exclusiveGroup
Definition: argsparser.h:200
bool orBefore
Definition: argsparser.h:196

◆ addParamName()

void ProgramDef::addParamName ( const String name,
ParamDef param 
)

Definition at line 912 of file argsparser.cpp.

913 {
914  if (paramsMap.find(name) != paramsMap.end())
915  error((String) "The param '" + name + "' is repeated.");
916  else
917  paramsMap[name] = param;
918 }
String name
Definition: argsparser.h:152
void error(String msg)
Definition: argsparser.cpp:406
struct _parameter * param
std::string String
Definition: xmipp_strings.h:34
std::map< String, ParamDef * > paramsMap
Dictionary with all params and alias names.
Definition: argsparser.h:244

◆ addParamRequires()

void ProgramDef::addParamRequires ( const String name)

Definition at line 920 of file argsparser.cpp.

921 {
922  pendingRequires.push_back(name);
923 }
String name
Definition: argsparser.h:152
StringVector pendingRequires
This is for checking that requires names exists.
Definition: argsparser.h:245

◆ addSection()

SectionDef * ProgramDef::addSection ( String  sectionName,
int  visibility = 0 
)

Add a section to the program definition and return a pointer to it, useful for manually

Definition at line 1009 of file argsparser.cpp.

1010 {
1011  SectionDef * section = new SectionDef(NULL, this);
1012  section->name = sectionName;
1013  section->visible = visibility;
1014  sections.push_back(section);
1015  return section;
1016 }
String name
Definition: argsparser.h:152
std::vector< SectionDef * > sections
Definition: argsparser.h:241
int visible
Definition: argsparser.h:153

◆ check()

void ProgramDef::check ( std::stringstream &  errors)
virtual

Implements ASTNode.

Definition at line 883 of file argsparser.cpp.

884 {
885  std::vector<ParamDef*> exclusive;
886  SectionDef * section;
887  ParamDef * param;
888 
889  for (size_t i = 0; i < sections.size(); ++i)
890  {
891  section = sections[i];
892  for (size_t j = 0; j < section->params.size(); ++j)
893  {
894  param = section->params[j];
895  //Doesn't check for alias, for doesn't repeat error messages
896  param->check(errors);
897  if (!param->orBefore)
898  reportExclusiveErrors(errors, exclusive);
899  exclusive.push_back(param);
900  }
901  }
902  reportExclusiveErrors(errors, exclusive);
903 }
virtual void check(std::stringstream &errors)
Definition: argsparser.cpp:715
std::vector< SectionDef * > sections
Definition: argsparser.h:241
#define i
#define j
struct _parameter * param
std::vector< ParamDef * > params
All params defined for the program.
Definition: argsparser.h:224
void reportExclusiveErrors(std::stringstream &errors, std::vector< ParamDef *> &exclusive)
Definition: argsparser.cpp:857
bool orBefore
Definition: argsparser.h:196

◆ clear()

void ProgramDef::clear ( )

clear read arguments

Definition at line 933 of file argsparser.cpp.

934 {
935 
936  SectionDef * section;
937  ParamDef * param;
938 
939  for (size_t i = 0; i < sections.size(); ++i)
940  {
941  section = sections[i];
942  for (size_t j = 0; j < section->params.size(); ++j)
943  {
944  param = section->params[j];
945  param->counter = 0;
946  param->cmdArguments.clear();
947  }
948  }
949 }
std::vector< SectionDef * > sections
Definition: argsparser.h:241
#define i
std::vector< const char * > cmdArguments
Definition: argsparser.h:199
#define j
struct _parameter * param
std::vector< ParamDef * > params
All params defined for the program.
Definition: argsparser.h:224
int counter
for count the number of times it appears in command line
Definition: argsparser.h:201

◆ findAndFillParam()

ParamDef * ProgramDef::findAndFillParam ( const String param)

Find a param and if not provided in cmd line, fill with its defaults values

Param was provided, not need to fill it

Definition at line 1018 of file argsparser.cpp.

1019 {
1020  ParamDef * paramDef = findParam(param);
1021  if (paramDef == NULL)
1022  REPORT_ERROR(ERR_ARG_INCORRECT, formatString("Doesn't exists param: %s", param.c_str()));
1024  //if (paramDef->counter == 1)
1025  // return paramDef;
1026  std::stringstream errors;
1027  size_t argIndex = 0;
1028  for (size_t i = 0; i < paramDef->arguments.size(); ++i)
1029  if (!paramDef->arguments[i]->acceptArguments(errors, argIndex, paramDef->cmdArguments))
1030  {
1031  errors << " parameter: " << paramDef->name << std::endl;
1032  REPORT_ERROR(ERR_ARG_INCORRECT, errors.str());
1033  }
1034  return paramDef;
1035 }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
String name
Definition: argsparser.h:152
ParamDef * findParam(const String &param)
Definition: argsparser.cpp:905
#define i
Incorrect argument received.
Definition: xmipp_error.h:113
std::vector< const char * > cmdArguments
Definition: argsparser.h:199
std::vector< ArgumentDef * > arguments
Definition: argsparser.h:198
struct _parameter * param
String formatString(const char *format,...)

◆ findParam()

ParamDef * ProgramDef::findParam ( const String param)

Definition at line 905 of file argsparser.cpp.

906 {
907  if (paramsMap.find(name) != paramsMap.end())
908  return paramsMap[name];
909  return NULL;
910 }
String name
Definition: argsparser.h:152
std::map< String, ParamDef * > paramsMap
Dictionary with all params and alias names.
Definition: argsparser.h:244

◆ getParam() [1/2]

const char * ProgramDef::getParam ( const char *  paramName,
size_t  paramNumber = 0 
)

Definition at line 1037 of file argsparser.cpp.

1038 {
1039  ParamDef * param = findAndFillParam(paramName);
1040  if (param == NULL)
1041  REPORT_ERROR(ERR_ARG_INCORRECT, formatString("Doesn't exists param: %s", paramName));
1042  if (argNumber < 0 || argNumber >= param->cmdArguments.size())
1043  REPORT_ERROR(ERR_ARG_INCORRECT, formatString("Argument index %d in param %s out of bounds.", argNumber, paramName));
1044  return param->cmdArguments.at(argNumber);
1045 }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
ParamDef * findAndFillParam(const String &param)
Incorrect argument received.
Definition: xmipp_error.h:113
std::vector< const char * > cmdArguments
Definition: argsparser.h:199
struct _parameter * param
String formatString(const char *format,...)

◆ getParam() [2/2]

const char * ProgramDef::getParam ( const char *  paramName,
const char *  subParam,
size_t  paramNumber = 0 
)

Definition at line 1047 of file argsparser.cpp.

1048 {
1049  ParamDef * param = findAndFillParam(paramName);
1050 
1051  size_t i = 0;
1052  for (i = 0; i < param->cmdArguments.size(); ++i)
1053  if (strcmp(param->cmdArguments[i], subParam) == 0)
1054  break;
1055 
1056  if (i == param->cmdArguments.size())
1057  REPORT_ERROR(ERR_ARG_INCORRECT, formatString("Sub-param %s was not supplied in command line.", subParam));
1058 
1059  return param->cmdArguments.at(i + 1 + argNumber);
1060 }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
ParamDef * findAndFillParam(const String &param)
#define i
Incorrect argument received.
Definition: xmipp_error.h:113
std::vector< const char * > cmdArguments
Definition: argsparser.h:199
struct _parameter * param
String formatString(const char *format,...)

◆ parse()

bool ProgramDef::parse ( )
virtual

Parse the program definition.

Implements ASTNode.

Definition at line 825 of file argsparser.cpp.

826 {
827 
828  // P -> ID CL OL
829  //consume(TOK_ID);
830  //name = token.lexeme;
831  //Usage comments
832  //parseCommentList(usageComments);
833 
834  //Ask for first token
835  pLexer->nextToken();
836 
837  while (!lookahead(TOK_END))
838  {
839  SectionDef * s = new SectionDef(pLexer, this);
840  s->parse();
841  String name = s->name;
842  sections.push_back(s);
843  }
844  consume(TOK_END);
845 
846  return true;
847 }
virtual bool consume(ArgTokenType type)
Definition: argsparser.cpp:376
String name
Definition: argsparser.h:152
std::vector< SectionDef * > sections
Definition: argsparser.h:241
ArgLexer * pLexer
Definition: argsparser.h:150
bool nextToken()
Definition: argsparser.cpp:164
std::string String
Definition: xmipp_strings.h:34
virtual bool parse()
Definition: argsparser.cpp:783
ArgTokenType lookahead() const
Definition: argsparser.cpp:356

◆ read()

void ProgramDef::read ( int  argc,
const char **  argv,
bool  reportErrors = true 
)

Read and validate commmand line.

Definition at line 951 of file argsparser.cpp.

952 {
953  clear();
954  std::stringstream errors;
955  //Set the name with the first argument
956  name = argv[0];
957  singleOption = false;
958 
959  ParamDef * param = NULL;
960 
961  //We assume that all options start with -
962  if (argc > 1 && argv[1][0] != '-')
963  {
964  //Assume if the first is missing, treat as -i
965  if ((param = findParam("-i")) == NULL) //-i is not allowed, report error
966  REPORT_ERROR(ERR_ARG_INCORRECT, "Parameters should start with a -");
967  else
968  {
969  ++(param->counter);
970  if (param->independent)
971  singleOption = true;
972  }
973  //Assume if the first is missing, treat as -i
974  }
975 
976  //Read command line params and arguments
977  for (int i = 1; i < argc; ++i)
978  {
979 
980  if (argv[i][0] == '-' && !isdigit(argv[i][1]))
981  {
982  param = findParam(argv[i]);
983  if (param == NULL)
984  errors << "Unrecognized parameter: " << argv[i] << std::endl;
985  else
986  {
987  ++(param->counter);
988  if (param->independent)
989  singleOption = true;
990  }
991  }
992  else if (param != NULL)
993  param->cmdArguments.push_back(argv[i]);
994  }
995 
996  if (!singleOption)
997  check(errors);
998 
999  //Report errors found
1000  if (reportErrors && errors.str().length() > 0)
1001  {
1002  //Unrecognized parameters
1003  //for (size_t i = 0; i < unrecognized.size(); ++i)
1004  // std::cerr << "Unrecognized parameter: " << unrecognized[i] << std::endl;
1005  REPORT_ERROR(ERR_ARG_BADCMDLINE, errors.str().c_str());
1006  }
1007 }
Errors on command line parameters.
Definition: xmipp_error.h:112
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
String name
Definition: argsparser.h:152
void clear()
clear read arguments
Definition: argsparser.cpp:933
ParamDef * findParam(const String &param)
Definition: argsparser.cpp:905
bool singleOption
Definition: argsparser.h:250
#define i
Incorrect argument received.
Definition: xmipp_error.h:113
virtual void check(std::stringstream &errors)
Definition: argsparser.cpp:883
std::vector< const char * > cmdArguments
Definition: argsparser.h:199
bool independent
Definition: argsparser.h:197
struct _parameter * param
int counter
for count the number of times it appears in command line
Definition: argsparser.h:201

Member Data Documentation

◆ examples

CommentList ProgramDef::examples

examples of use

Definition at line 243 of file argsparser.h.

◆ keywords

String ProgramDef::keywords

Definition at line 246 of file argsparser.h.

◆ paramsMap

std::map<String, ParamDef*> ProgramDef::paramsMap

Dictionary with all params and alias names.

Definition at line 244 of file argsparser.h.

◆ pendingRequires

StringVector ProgramDef::pendingRequires

This is for checking that requires names exists.

Definition at line 245 of file argsparser.h.

◆ sections

std::vector<SectionDef*> ProgramDef::sections

Definition at line 241 of file argsparser.h.

◆ seeAlso

String ProgramDef::seeAlso

Definition at line 247 of file argsparser.h.

◆ singleOption

bool ProgramDef::singleOption

This flag is used to check if an independent option was found like: –more, –help that avoid others options restrictions.

Definition at line 250 of file argsparser.h.

◆ usageComments

CommentList ProgramDef::usageComments

comments of usage

Definition at line 242 of file argsparser.h.


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