Coverage report for gui


src/
File: src/utils/CommandLineParser.cpp
Date: 2024-06-25 10:57:02
Lines:
32/32
100.0%
Functions:
4/4
100.0%
Branches:
33/50
66.0%

Line Branch Exec Source
1 #include "CommandLineParser.hpp"
2
3 #include <cstddef>
4 #include <iomanip>
5 #include <iostream>
6 #include <map>
7 #include <optional>
8 #include <string>
9 #include <tuple>
10
11 8 void CommandLineParser::addOption(const std::string &longOpt, const std::string &shortOpt, const std::string &description)
12 {
13
1/2
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
8 options[longOpt] = std::make_tuple(shortOpt, description, std::nullopt);
14 8 }
15
16 4 void CommandLineParser::parse()
17 {
18
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
8 for (std::size_t i = 1; i < args.size(); ++i) {
19
2/2
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 4 times.
6 if (args[i][0] != '-')
20 2 continue;
21 4 std::string option = args[i];
22 std::string argument;
23
24 bool optionFound = false;
25
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 2 times.
9 for (const auto &[fst, snd] : options) {
26
4/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 5 times.
7 if (option == fst || std::get<0>(snd) == option) {
27
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
2 option = (fst == option) ? option : (std::get<0>(snd) == option) ? fst : option;
28
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
2 if (i + 1 < args.size() && args[i + 1][0] != '-') {
29 argument = args[i + 1];
30 ++i;
31 }
32 optionFound = true;
33 break;
34 }
35 }
36
37
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (!optionFound)
38
2/4
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 2 times.
✗ Branch 6 not taken.
4 throw std::runtime_error("Unknown option: " + option);
39
40
3/6
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 2 times.
✗ Branch 8 not taken.
8 options[option] = std::make_tuple(std::get<0>(options[option]), std::get<1>(options[option]), argument);
41 }
42 2 }
43
44 3 [[nodiscard]] std::optional<std::string> CommandLineParser::getOption(const std::string &longOpt) const
45 {
46 if (options.count(longOpt) == 0)
47 1 return std::nullopt;
48 2 return std::get<2>(options.at(longOpt));
49 }
50
51 1 void CommandLineParser::displayHelp(const std::string &binaryName) const
52 {
53 1 std::cout << "Usage: " << binaryName;
54
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 for (const auto &[fst, snd] : options) {
55 1 std::string longOpt = fst;
56
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 std::string shortOpt = std::get<0>(snd);
57
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 std::string description = std::get<1>(snd);
58
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 std::cout << " ";
59
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (!shortOpt.empty())
60
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 std::cout << std::setw(2) << shortOpt << ", ";
61
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 std::cout << std::setw(10) << std::left << longOpt << " [";
62
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (!description.empty())
63 std::cout << description;
64
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 std::cout << "]\n";
65 }
66 1 }
67