geopm  3.1.1.dev296+g5916b956
GEOPM - Global Extensible Open Power Manager
OptionParser.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 - 2024 Intel Corporation
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #ifndef OPTIONPARSER_HPP_INCLUDE
7 #define OPTIONPARSER_HPP_INCLUDE
8 
9 #include <string>
10 #include <map>
11 #include <ostream>
12 #include <vector>
13 
14 #include "geopm/Exception.hpp"
15 #include "geopm_public.h"
16 
17 namespace geopm
18 {
19  class GEOPM_PUBLIC OptionParser
20  {
21  public:
22  OptionParser(const std::string &prog_name,
23  std::ostream &std_out,
24  std::ostream &err_out);
25  OptionParser(const std::string &prog_name,
26  std::ostream &std_out,
27  std::ostream &err_out,
28  const std::string &custom_help);
30  void add_option(const std::string &name,
31  char short_form,
32  const std::string &long_form,
33  const std::string &default_val,
34  const std::string &description);
35  void add_option(const std::string &name,
36  char short_form,
37  const std::string &long_form,
38  const char *default_val,
39  const std::string &description);
41  void add_option(const std::string &name,
42  char short_form,
43  const std::string &long_form,
44  bool default_val,
45  const std::string &description);
48  bool parse(int argc, const char * const argv[]);
50  bool is_set(const std::string &name);
52  std::string get_value(const std::string &name);
54  std::vector<std::string> get_positional_args(void);
57  void add_example_usage(const std::string &example);
60  std::string format_help(void);
61  private:
62  void GEOPM_PRIVATE
63  check_add_option(const std::string &name,
64  char short_form,
65  const std::string &long_form);
66  void GEOPM_PRIVATE
67  format_option(std::ostream &tmp,
68  const std::string &short_form,
69  const std::string &long_form,
70  std::string description);
71 
72  static const std::string M_COPYRIGHT_TEXT;
73 
74  std::string m_prog_name;
75  std::ostream &m_std_out;
76  std::ostream &m_err_out;
77  std::string m_custom_help;
78  std::vector<std::string> m_example_usage;
79  std::vector<std::string> m_option_order;
80 
81  template <typename T>
82  struct m_opt_parse_s
83  {
84  char short_form;
85  std::string long_form;
86  T value;
87  T default_value;
88  std::string description;
89  };
90  std::map<std::string, struct m_opt_parse_s<bool> > m_bool_opts;
91  std::map<std::string, struct m_opt_parse_s<std::string> > m_str_opts;
92  std::map<char, std::string> m_bool_short_name;
93  std::map<char, std::string> m_str_short_name;
94 
95  std::vector<std::string> m_positional_args;
96  };
97 }
98 
99 #endif
Definition: OptionParser.hpp:20
Definition: Accumulator.cpp:12