1+ #include " Argument.h"
2+ #include < iostream>
3+
4+ Argument::Argument () {}
5+
6+ Argument::Argument (const std::string& name, ArgumentType argumentType, const std::string& shortoption, const std::string& longoption, const type_of_arguments_value& defaultValue, bool required, const std::string& help)
7+ : name(name),
8+ argumentType(argumentType),
9+ shortoption(shortoption),
10+ longoption(longoption),
11+ defaultValue(defaultValue),
12+ required(required),
13+ value(defaultValue),
14+ help(help)
15+ {
16+
17+ }
18+
19+
20+ int Argument::parse (int argc, char * argv[], int current_index) {
21+ if (this ->argumentType == ArgumentType::BooleanSwitchArgument) {
22+ // Parsing BooleanSwitchArgument
23+ if (current_index <= (argc-1 )) {
24+ if ((argv[current_index] == this ->shortoption ) || (argv[current_index] == this ->longoption )) {
25+ current_index += 1 ;
26+ this ->value = true ;
27+ }
28+ else {
29+ this ->value = false ;
30+ }
31+ // std::cout << "Parsed BooleanSwitchArgument " << this->name << "\n";
32+ }
33+ }
34+ else if (this ->argumentType == ArgumentType::StringArgument) {
35+ // Parsing StringArgument
36+
37+ if (current_index <= (argc-2 )) {
38+ if ((argv[current_index] == this ->shortoption ) || (argv[current_index] == this ->longoption )) {
39+ this ->value = std::string (argv[current_index + 1 ]);
40+ current_index += 2 ;
41+ // std::cout << "Parsed StringArgument " << this->name << "\n";
42+ }
43+ }
44+ }
45+ else if (this ->argumentType == ArgumentType::IntegerArgument) {
46+ // Parsing IntegerArgument
47+ if (current_index <= (argc - 2 )) {
48+ if ((argv[current_index] == this ->shortoption ) || (argv[current_index] == this ->longoption )) {
49+ this ->value = std::stoi (argv[current_index + 1 ]);
50+ current_index += 2 ;
51+ // std::cout << "Parsed IntegerArgument " << this->name << "\n";
52+ }
53+ }
54+ }
55+ else if (this ->argumentType == ArgumentType::PositionalIntegerArgument) {
56+ // Parsing PositionalIntegerArgument
57+ if (current_index <= (argc - 1 )) {
58+ this ->value = std::stoi (argv[current_index]);
59+ current_index += 1 ;
60+ // std::cout << "Parsed PositionalIntegerArgument " << this->name << "\n";
61+ }
62+ }
63+ else if (this ->argumentType == ArgumentType::PositionalStringArgument) {
64+ // Parsing PositionalStringArgument
65+ if (current_index <= (argc - 1 )) {
66+ this ->value = std::string (argv[current_index]);
67+ current_index += 1 ;
68+ // std::cout << "Parsed PositionalStringArgument " << this->name << "\n";
69+ }
70+ }
71+ return current_index;
72+ }
0 commit comments