Line data Source code
1 : #include "../../inc/options/optionsParser.h"
2 :
3 : namespace nicole {
4 :
5 108 : bool OptionsParser::isOption(const std::string_view argument) noexcept {
6 108 : if (argument == "-o" or argument == "--optimize" or argument == "-n" or
7 108 : argument == "--name" or argument == "-h" or argument == "--help" or
8 108 : argument == "-p" or argument == "--printTree" or argument == "-i" or
9 108 : argument == "--printIR" or argument == "-v" or argument == "--validate") {
10 9 : return true;
11 9 : }
12 99 : return false;
13 108 : }
14 :
15 : const std::expected<Options, Error>
16 243 : OptionsParser::parse(const std::vector<std::string_view> &arguments) noexcept {
17 243 : const std::size_t size{arguments.size()};
18 :
19 243 : if (!size) {
20 9 : return createError(ERROR_TYPE::MISSING_ARGUMENTS,
21 9 : "at least must give the name of the entry file");
22 9 : }
23 :
24 234 : bool help{false};
25 234 : bool optimize{false};
26 234 : bool printTree{false};
27 234 : bool printIR{false};
28 234 : bool validateTree{false};
29 234 : std::string binaryName{"a.out"};
30 234 : std::filesystem::path entryFilePath{""};
31 :
32 234 : bool entryFileParsed{false};
33 :
34 738 : for (std::size_t i{0}; i < size; ++i) {
35 531 : const std::string_view argument{arguments[i]};
36 531 : if (argument == "-h" or argument == "--help") {
37 90 : help = true;
38 : // break; not using break in case that we let the user use help and
39 : // execute
40 441 : } else if (argument == "-o" or argument == "--optimize") {
41 144 : optimize = true;
42 297 : } else if (argument == "-p" or argument == "--printTree") {
43 0 : printTree = true;
44 297 : } else if (argument == "-i" or argument == "--printIR") {
45 0 : printIR = true;
46 297 : } else if (argument == "-v" or argument == "--validate") {
47 0 : validateTree = true;
48 297 : } else if (argument == "-n" or argument == "--name") {
49 126 : if (++i == size or isOption(arguments[i])) {
50 27 : return createError(ERROR_TYPE::MISSING_BINARY_NAME,
51 27 : "missing binary name");
52 27 : }
53 99 : binaryName = arguments[i];
54 171 : } else {
55 171 : if (entryFileParsed) {
56 0 : return createError(ERROR_TYPE::UNKOWN_ARGUMENT,
57 0 : "argument: " + std::string{argument} +
58 0 : " not supported");
59 0 : }
60 171 : entryFilePath = argument;
61 171 : entryFileParsed = true;
62 171 : }
63 531 : }
64 : // in case that help exists we skip the error
65 207 : if (entryFilePath.empty() and !help) {
66 27 : return createError(ERROR_TYPE::MISSING_ENTRY_FILE,
67 27 : "must specify the entry file");
68 27 : }
69 :
70 180 : return Options{help, optimize, printTree, printIR,
71 180 : validateTree, binaryName, entryFilePath};
72 207 : }
73 :
74 : } // namespace nicole
|