HAL
program_arguments.cpp
Go to the documentation of this file.
2 
4 
5 namespace hal
6 {
8  {
9  m_argc = 0;
10  m_argv = nullptr;
11  }
12 
13  ProgramArguments::ProgramArguments(int argc, const char** argv)
14  {
15  m_argc = argc;
16  m_argv = argv;
17  }
18 
19  void ProgramArguments::get_original_arguments(int* argc, const char*** argv)
20  {
21  *argc = m_argc;
22  *argv = m_argv;
23  }
24 
25  std::vector<std::string> ProgramArguments::get_set_options() const
26  {
27  return m_given_flags;
28  }
29 
30  bool ProgramArguments::is_option_set(const std::string& flag) const
31  {
32  // checks all groups of equivalent flags too
33  for (const auto& it : m_set_options)
34  {
35  if (it.first.find(flag) != it.first.end())
36  {
37  return true;
38  }
39  }
40  return false;
41  }
42 
43  void ProgramArguments::set_option(const std::string& flag, const std::string& parameter)
44  {
45  set_option(flag, {}, {parameter});
46  }
47 
48  void ProgramArguments::set_option(const std::string& flag, const std::vector<std::string>& parameters)
49  {
50  set_option(flag, {}, parameters);
51  }
52 
53  bool ProgramArguments::set_option(const std::string& flag, const std::set<std::string>& equivalent_flags, const std::vector<std::string>& parameters)
54  {
55  std::set<std::string> option_flags = equivalent_flags;
56  option_flags.insert(flag);
57 
58  // remember the first matching option here (if it exists)
59  auto it_matching_option = m_set_options.end();
60 
61  // "count" options that contain any of the given flags here
62  std::set<const std::set<std::string>*> options_containing_flags;
63 
64  for (auto it = m_set_options.begin(); it != m_set_options.end(); ++it)
65  {
66  for (const auto& f : option_flags)
67  {
68  if (it->first.find(f) != it->first.end())
69  {
70  options_containing_flags.insert(&(it->first));
71  it_matching_option = it;
72  }
73  }
74  }
75 
76  // if the flags were split over multiple options --> error
77  if (options_containing_flags.size() > 1)
78  {
79  log_error("core",
80  "Given flags ({}) are parts of {} different options.",
81  utils::join(", ", std::vector<std::string>(option_flags.begin(), option_flags.end())),
82  options_containing_flags.size());
83  return false;
84  }
85  // if the flags are found in a single option, 'it_matching_option' points to it
86  else if (options_containing_flags.size() == 1)
87  {
88  // if all given flags are not a subset of the already known flags --> error
89  if (!std::includes(it_matching_option->first.begin(), it_matching_option->first.end(), option_flags.begin(), option_flags.end()))
90  {
91  log_error("core",
92  "Some flags of ({}) are already set, but not all given flags are flags for that option.",
93  utils::join(", ", std::vector<std::string>(option_flags.begin(), option_flags.end())));
94  return false;
95  }
96  }
97 
98  // overwrite an existing flag (keeping the old flag list as the new one has to be a subset), or register a new one
99  if (it_matching_option != m_set_options.end())
100  {
101  it_matching_option->second = parameters;
102  }
103  else
104  {
105  m_set_options[option_flags] = parameters;
106  }
107 
108  if (std::find(m_given_flags.begin(), m_given_flags.end(), flag) == m_given_flags.end())
109  {
110  m_given_flags.push_back(flag);
111  }
112 
113  return true;
114  }
115 
116  std::string ProgramArguments::get_parameter(const std::string& flag) const
117  {
118  auto list = get_parameters(flag);
119  if (list.empty())
120  {
121  return "";
122  }
123  return list[0];
124  }
125 
126  std::vector<std::string> ProgramArguments::get_parameters(const std::string& flag) const
127  {
128  for (const auto& it : m_set_options)
129  {
130  if (it.first.find(flag) != it.first.end())
131  {
132  return it.second;
133  }
134  }
135 
136  log_error("core", "The flag '{}' is not registered.", flag);
137  return {};
138  }
139 } // namespace hal
std::vector< std::string > get_set_options() const
std::string get_parameter(const std::string &flag) const
std::vector< std::string > get_parameters(const std::string &flag) const
void get_original_arguments(int *argc, const char ***argv)
bool is_option_set(const std::string &flag) const
void set_option(const std::string &flag, const std::string &parameter)
#define log_error(channel,...)
Definition: log.h:78
CORE_API std::string join(const std::string &joiner, const Iterator &begin, const Iterator &end, const Transform &transform)
Definition: utils.h:412