EventAnalysis  1.3.0
CommandLineParser.h
Go to the documentation of this file.
1 /*
2  * CommandLineParser.h
3  *
4  * Created on: 1 Sep 2017
5  * Author: Nicola Mori
6  */
7 
10 #ifndef COMMANDLINEPARSER_H_
11 #define COMMANDLINEPARSER_H_
12 
13 #include <string>
14 #include <vector>
15 
16 namespace EA {
17 
34 public:
43  void AddOption(const std::string &shortInd, const std::string &longInd, bool mandatory = false);
44 
53  void AddMultiOption(const std::string &shortInd, const std::string &longInd, bool mandatory = false);
54 
63  void AddFlag(const std::string &shortInd, const std::string &longInd);
64 
74  bool ProcessCommandLine(int argc, const char *const *const argv);
75 
81  int GetNOptions(const std::string &indicator);
82 
88  int GetNMultiOptions(const std::string &indicator);
89 
102  const std::string &GetOption(const std::string &indicator, int optionNo = 0);
103 
116  const std::vector<std::string> &GetMultiOption(const std::string &indicator, int optionNo = 0);
117 
127  bool GetFlag(const std::string &indicator);
128 
136  const std::vector<std::string> &GetArguments();
137 
138 private:
139  struct OptionStruct {
140  std::string shortInd;
141  std::string longInd;
142  std::string value;
143  bool mandatory;
144  OptionStruct(const std::string &shortIndicator, const std::string &longIndicator, bool mand = false)
145  : shortInd(shortIndicator), longInd(longIndicator), value(""), mandatory(mand) {}
146  };
147  std::vector<OptionStruct> _options;
148 
150  std::string shortInd;
151  std::string longInd;
152  std::vector<std::string> value;
153  bool mandatory;
154  MultiOptionStruct(const std::string &shortIndicator, const std::string &longIndicator, bool mand = false)
155  : shortInd(shortIndicator), longInd(longIndicator), value(0), mandatory(mand) {}
156  };
157  std::vector<MultiOptionStruct> _multiOptions;
158 
159  struct FlagStruct {
160  std::string shortInd;
161  std::string longInd;
162  bool value;
163  FlagStruct(const std::string &shortIndicator, const std::string &longIndicator)
164  : shortInd(shortIndicator), longInd(longIndicator), value(false) {}
165  };
166  std::vector<FlagStruct> _flags;
167 
168  std::vector<std::string> _arguments;
169 };
170 
171 } // namespace EA
172 
173 #endif /* COMMANDLINEPARSER_H_ */
std::string longInd
Definition: CommandLineParser.h:141
Parser for command line options.
Definition: CommandLineParser.h:33
std::string shortInd
Definition: CommandLineParser.h:140
void AddOption(const std::string &shortInd, const std::string &longInd, bool mandatory=false)
Add and option with a single value.
Definition: CommandLineParser.cpp:13
std::vector< std::string > value
Definition: CommandLineParser.h:152
Definition: CommandLineParser.h:139
void AddFlag(const std::string &shortInd, const std::string &longInd)
Add a flag option.
Definition: CommandLineParser.cpp:23
bool value
Definition: CommandLineParser.h:162
std::vector< OptionStruct > _options
Definition: CommandLineParser.h:147
std::string shortInd
Definition: CommandLineParser.h:160
int GetNMultiOptions(const std::string &indicator)
Returns the number of times the given multioption has been found in command line. ...
Definition: CommandLineParser.cpp:156
std::vector< FlagStruct > _flags
Definition: CommandLineParser.h:166
IncludeFileExc.h IncludeFileExc class declaration.
Definition: Algorithm.h:21
Definition: CommandLineParser.h:159
OptionStruct(const std::string &shortIndicator, const std::string &longIndicator, bool mand=false)
Definition: CommandLineParser.h:144
bool mandatory
Definition: CommandLineParser.h:153
FlagStruct(const std::string &shortIndicator, const std::string &longIndicator)
Definition: CommandLineParser.h:163
bool ProcessCommandLine(int argc, const char *const *const argv)
Process the command line.
Definition: CommandLineParser.cpp:28
const std::string & GetOption(const std::string &indicator, int optionNo=0)
Returns the value of the mandatory option.
Definition: CommandLineParser.cpp:168
MultiOptionStruct(const std::string &shortIndicator, const std::string &longIndicator, bool mand=false)
Definition: CommandLineParser.h:154
const std::vector< std::string > & GetMultiOption(const std::string &indicator, int optionNo=0)
Returns a vector of values of the mandatory multioption.
Definition: CommandLineParser.cpp:184
int GetNOptions(const std::string &indicator)
Returns the number of times the given option has been found in command line.
Definition: CommandLineParser.cpp:145
std::vector< std::string > _arguments
Definition: CommandLineParser.h:168
bool GetFlag(const std::string &indicator)
Checks if a flag has been defined.
Definition: CommandLineParser.cpp:200
std::string longInd
Definition: CommandLineParser.h:151
void AddMultiOption(const std::string &shortInd, const std::string &longInd, bool mandatory=false)
Add and option with multiple values.
Definition: CommandLineParser.cpp:18
std::string longInd
Definition: CommandLineParser.h:161
std::string shortInd
Definition: CommandLineParser.h:150
std::vector< MultiOptionStruct > _multiOptions
Definition: CommandLineParser.h:157
bool mandatory
Definition: CommandLineParser.h:143
Definition: CommandLineParser.h:149
std::string value
Definition: CommandLineParser.h:142
const std::vector< std::string > & GetArguments()
Getter method for argument list.
Definition: CommandLineParser.cpp:212