Repo-Init
 
Loading...
Searching...
No Matches
InputParser.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <string>
5#include <vector>
6
12 private:
13 std::vector<std::string> _tokens;
14
15 public:
21 InputParser(const int &argc, char **argv)
22 {
23 for (int i = 1; i < argc; ++i)
24 {
25 _tokens.emplace_back(argv[i]);
26 }
27 }
28
34 [[nodiscard]] const std::string &getCmdOption(const std::string &option) const
35 {
36 std::vector<std::string>::const_iterator itr;
37 itr = std::find(_tokens.begin(), _tokens.end(), option);
38 if (itr != _tokens.end() && ++itr != _tokens.end())
39 {
40 return *itr;
41 }
42 static const std::string empty_string;
43 return empty_string;
44 }
45
50 [[nodiscard]] std::vector<std::pair<std::string, std::string>> getCmdOptions() const
51 {
52 std::vector<std::pair<std::string, std::string>> options;
53 for (auto itr = _tokens.begin(); itr != _tokens.end(); ++itr)
54 {
55 if (!itr->empty() && itr->at(0) == '-')
56 {
57 auto nextItr = std::next(itr);
58 if (nextItr != _tokens.end() && !nextItr->empty() && nextItr->at(0) != '-')
59 {
60 options.emplace_back(*itr, *(nextItr));
61 }
62 else
63 {
64 options.emplace_back(*itr, "");
65 }
66 }
67 }
68 return options;
69 }
70
77 [[nodiscard]] bool cmdOptionExists(const std::string &option) const
78 {
79 return std::find(_tokens.begin(), _tokens.end(), option) != _tokens.end();
80 }
81};
InputParser(const int &argc, char **argv)
const std::string & getCmdOption(const std::string &option) const
std::vector< std::string > _tokens
std::vector< std::pair< std::string, std::string > > getCmdOptions() const
bool cmdOptionExists(const std::string &option) const