GameKit  0.0.1a
C++ gamedev tools
ArgumentParser.cpp
Go to the documentation of this file.
1 /*
2  * =====================================================================================
3  *
4  * Filename: ArgumentParser.cpp
5  *
6  * Description:
7  *
8  * Created: 21/01/2019 23:18:08
9  *
10  * Author: Quentin Bazin, <quent42340@gmail.com>
11  *
12  * =====================================================================================
13  */
15 #include "gk/core/Debug.hpp"
16 
17 namespace gk {
18 
19 ArgumentParser::ArgumentParser(int argc, char **argv) {
20  for (int i = 0 ; i < argc ; ++i) {
21  m_argv.emplace_back(argv[i]);
22  }
23 
24  addArgument("help", {"", "--help"});
25 }
26 
28  for (std::size_t i = 0 ; i < m_argv.size() ; ++i) {
29  for (auto &it : m_arguments) {
30  if (m_argv[i] == it.second.longName || (!it.second.shortName.empty() && m_argv[i] == it.second.shortName)) {
31  it.second.isFound = true;
32  if (it.second.hasParameter && i + 1 < m_argv.size())
33  it.second.parameter = m_argv[i + 1];
34  }
35  }
36  }
37 
38  if (getArgument("help").isFound)
39  printHelp();
40 }
41 
43  std::cout << "Available options:" << std::endl;
44  for (auto &it : m_arguments)
45  std::cout << " " << it.second.longName << "/" << it.second.shortName << std::endl;
46 }
47 
49  DEBUG("========== ARGS ==========");
50  for (auto &it : m_arguments)
51  DEBUG(it.second.longName, "/", it.second.shortName, "=", it.second.isFound);
52  DEBUG("==========================");
53 }
54 
55 } // namespace gk
56 
ArgumentParser(int argc, char **argv)
#define DEBUG(args...)
Definition: Debug.hpp:31
const Argument & getArgument(const std::string &name)
std::vector< std::string > m_argv
std::unordered_map< std::string, Argument > m_arguments
void addArgument(const std::string &name, const Argument &argument)