-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandLineParser.cpp
More file actions
100 lines (91 loc) · 3.73 KB
/
Copy pathCommandLineParser.cpp
File metadata and controls
100 lines (91 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
* @file
* Copyright (c) 2023 - Tidaly
* Authors:
* - Philippe CHEYPE <philippe.cheype@epitech.eu>
* NOTICE: All information contained herein is, and remains
* the property of Tidaly. Dissemination of this information
* or reproduction of this material is strictly forbidden
* unless prior written permission is obtained from Tidaly.
*/
#include "CommandLineParser.hpp"
type::Params CommandLineParser::parse(int ac, char **av) {
int opt;
this->_programName = av[0];
static struct option long_options[] = {
{"help", no_argument, 0, 'h' },
{"debug", no_argument, 0, 'd' },
{"input", no_argument, 0, 'i' },
{"output", required_argument, 0, 'o' },
{0, 0, 0, 0 }
};
try {
optind = 0; // Reset getopt index
while ((opt = getopt_long(ac, av, "hdio:", long_options, nullptr)) != -1) {
switch (opt) {
case 'h':
this->_display_help();
throw ParametersEnd();
case 'd':
this->_handle_debug_option();
break;
case 'o':
this->_handle_output_option(av);
break;
case 'i':
this->_handle_input_option(av);
break;
default:
THROW_EXCEPTION(ParametersError, "Unknown option");
}
}
} catch (const ParametersEnd& e) {
return this->_params;
}
this->_params.parsed = true;
return this->_params;
}
void CommandLineParser::_display_help() const {
std::cout
<< "USAGE: " << this->_programName << " [--help] [--debug] [--output <type> <path>] [--input <path>]"
<< "\n\nBy feeding an image or stream of images as an input the module will go trough"
<< " multiple steps and extract a numerical value from the image using OCR."
<< "\n\nOPTIONAL ARGUMENTS:"
<< "\n --help, -h\tShow this help message"
<< "\n --debug, -d\tEnable debug mode"
<< "\n --output, -o\tOutput type and path, requires two arguments"
<< "\n\t\t| <type>: The output type (ie: \"CSV\", \"FIFO\", \"PRINT\")"
<< "\n\t\t| <path>: Path to the output file"
<< "\n --input, -i\tInput mode enabled, requires one argument"
<< "\n\t\t| <path>: Path to the input image"
<< std::endl;
}
void CommandLineParser::_handle_debug_option() {
this->_params.debug = true;
std::cout << "[\033[0;33mINFO\033[0m] Debug mode enabled" << std::endl;
}
void CommandLineParser::_handle_output_option(char** argv) {
if (!optarg || !argv[optind]) {
THROW_EXCEPTION(ParametersError, "Invalid --output arguments");
}
this->_params.outputPath = argv[optind];
std::string type = optarg;
if (type == "csv" || type == "CSV") {
this->_params.outputType = type::OutputModuleBehaviour::CSV;
std::cerr << "[\033[0;33mINFO\033[0m] Output type: CSV (" << this->_params.outputPath << ")" << std::endl;
} else if (type == "fifo" || type == "FIFO") {
this->_params.outputType = type::OutputModuleBehaviour::FIFO;
std::cerr << "[\033[0;33mINFO\033[0m] Output type: FIFO" << std::endl;
} else if (type == "print" || type == "PRINT") {
this->_params.outputType = type::OutputModuleBehaviour::PRINT;
std::cerr << "[\033[0;33mINFO\033[0m] Output type: PRINT" << std::endl;
} else {
THROW_EXCEPTION(ParametersError, "Invalid output type");
}
optind++;
}
void CommandLineParser::_handle_input_option(char** argv) {
this->_params.inputPath = argv[optind];
std::cerr << "[\033[0;33mINFO\033[0m] Input mode enabled" << std::endl;
optind++;
}