Directory: | ./ |
---|---|
File: | Tools/simple_cmd.hpp |
Date: | 2024-04-14 07:32:34 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 25 | 25 | 100.0% |
Branches: | 27 | 62 | 43.5% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | // ______ ______ _ _ _____ ______ | ||
2 | // | ____| ____| | (_)/ ____| | ____| | ||
3 | // | |__ | |__ | | _| (___ ___| |__ | ||
4 | // | __| | __| | | | |\___ \ / __| __| | ||
5 | // | | | |____| |____| |____) | (__| |____ | ||
6 | // |_| |______|______|_|_____/ \___|______| | ||
7 | // Finite Elements for Life Sciences and Engineering | ||
8 | // | ||
9 | // License: LGL2.1 License | ||
10 | // FELiScE default license: LICENSE in root folder | ||
11 | // | ||
12 | // Main authors: Vicente Mataix Ferrandiz | ||
13 | // | ||
14 | |||
15 | #ifndef _SIMPLE_CMD_HPP | ||
16 | #define _SIMPLE_CMD_HPP | ||
17 | |||
18 | // System includes | ||
19 | #include <functional> // std::function | ||
20 | #include <iostream> // std::cout, std::endl | ||
21 | #include <map> // std::map | ||
22 | #include <memory> // std::unique_ptr | ||
23 | #include <string> // std::string | ||
24 | #include <sstream> // std::stringstream | ||
25 | #include <string_view> // std::string_view | ||
26 | #include <variant> // std::variant | ||
27 | #include <vector> // std::vector | ||
28 | |||
29 | // External includes | ||
30 | |||
31 | // Project includes | ||
32 | |||
33 | /** | ||
34 | * @brief This class provides a simplified CMD interface | ||
35 | * @author Vicente Mataix Ferrandiz | ||
36 | */ | ||
37 | template <class Opts> | ||
38 | struct CmdOptions : Opts | ||
39 | { | ||
40 | using Property = std::variant<std::string Opts::*, int Opts::*, double Opts::*, bool Opts::*>; | ||
41 | using Argument = std::pair<std::string, Property>; | ||
42 | |||
43 | 4 | ~CmdOptions() = default; | |
44 | |||
45 | 4 | Opts parse(int argc, const char* argv[]) | |
46 | { | ||
47 |
1/2✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
|
4 | std::vector<std::string_view> vargv(argv, argv+argc); |
48 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 4 times.
|
18 | for (int idx = 0; idx < argc; ++idx) |
49 |
2/2✓ Branch 5 taken 38 times.
✓ Branch 6 taken 14 times.
|
52 | for (auto& cbk : callbacks) |
50 |
1/2✓ Branch 1 taken 38 times.
✗ Branch 2 not taken.
|
38 | cbk.second(idx, vargv); |
51 | |||
52 |
1/2✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
|
8 | return static_cast<Opts>(*this); |
53 | 4 | } | |
54 | |||
55 | 4 | static std::unique_ptr<CmdOptions> Create(std::initializer_list<Argument> args) | |
56 | { | ||
57 |
1/2✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
|
4 | auto cmdOpts = std::unique_ptr<CmdOptions>(new CmdOptions()); |
58 |
5/8✓ Branch 3 taken 10 times.
✗ Branch 4 not taken.
✓ Branch 7 taken 10 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 10 times.
✗ Branch 11 not taken.
✓ Branch 14 taken 10 times.
✓ Branch 15 taken 4 times.
|
14 | for (auto arg : args) cmdOpts->register_callback(arg); |
59 | 4 | return cmdOpts; | |
60 | } | ||
61 | |||
62 | private: | ||
63 | using callback_t = std::function<void(int, const std::vector<std::string_view>&)>; | ||
64 | std::map<std::string, callback_t> callbacks; | ||
65 | |||
66 | 4 | CmdOptions() = default; | |
67 | CmdOptions(const CmdOptions&) = delete; | ||
68 | CmdOptions(CmdOptions&&) = delete; | ||
69 | CmdOptions& operator=(const CmdOptions&) = delete; | ||
70 | CmdOptions& operator=(CmdOptions&&) = delete; | ||
71 | |||
72 | 10 | auto register_callback(std::string name, Property prop) | |
73 | { | ||
74 |
2/4✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 10 times.
✗ Branch 6 not taken.
|
48 | callbacks[name] = [this, name, prop](int idx, const std::vector<std::string_view>& argv) { |
75 |
2/2✓ Branch 3 taken 5 times.
✓ Branch 4 taken 33 times.
|
38 | if (argv[idx] == name) { |
76 | 5 | visit( | |
77 |
2/8✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 10 taken 4 times.
✗ Branch 11 not taken.
|
10 | [this, idx, &argv](auto&& arg) { |
78 | if (idx < static_cast<int>(argv.size()) - 1) { | ||
79 |
2/8✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 10 taken 4 times.
✗ Branch 11 not taken.
|
5 | std::stringstream value; |
80 |
2/8✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✓ Branch 14 taken 4 times.
✗ Branch 15 not taken.
|
5 | value << argv[idx+1]; |
81 |
2/8✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 10 taken 4 times.
✗ Branch 11 not taken.
|
5 | value >> this->*arg; |
82 | 5 | } | |
83 | }, | ||
84 |
1/2✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
|
5 | prop); |
85 | } | ||
86 | }; | ||
87 | 10 | }; | |
88 | |||
89 |
1/2✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
|
10 | auto register_callback(Argument p) { return register_callback(p.first, p.second); } |
90 | }; | ||
91 | |||
92 | #endif | ||
93 |