HAL  v4.5.0-83-g30c8f0afc
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
program_options.cpp
Go to the documentation of this file.
2 
3 namespace hal
4 {
6  {
7  py::class_<ProgramArguments> py_program_arguments(m, "ProgramArguments", R"(
8  Holds the parsed command line arguments of the program.
9  )");
10 
11  py_program_arguments.def(py::init<>(), R"(
12  Construct an empty set of program arguments.
13  )");
14 
15  // get_original_arguments is deliberately not exposed: ProgramArguments stores the argv pointer
16  // without taking ownership of the strings, which no longer exist once parse() has returned to Python.
17 
18  py_program_arguments.def("get_set_options", &ProgramArguments::get_set_options, R"(
19  Get all options that are set.
20 
21  :returns: A list of the flags of all options that are set.
22  :rtype: list[str]
23  )");
24 
25  py_program_arguments.def("is_option_set", &ProgramArguments::is_option_set, py::arg("flag"), R"(
26  Check whether an option is set.
27 
28  :param str flag: A flag of the option.
29  :returns: ``True`` if the option is set, ``False`` otherwise.
30  :rtype: bool
31  )");
32 
33  py_program_arguments.def("set_option",
34  py::overload_cast<const std::string&, const std::string&>(&ProgramArguments::set_option),
35  py::arg("flag"),
36  py::arg("parameter"),
37  R"(
38  Set an option with a single parameter.
39 
40  :param str flag: The flag of the option.
41  :param str parameter: The parameter of the option.
42  )");
43 
44  py_program_arguments.def("set_option",
45  py::overload_cast<const std::string&, const std::vector<std::string>&>(&ProgramArguments::set_option),
46  py::arg("flag"),
47  py::arg("parameters"),
48  R"(
49  Set an option with multiple parameters.
50 
51  :param str flag: The flag of the option.
52  :param list[str] parameters: The parameters of the option.
53  )");
54 
55  py_program_arguments.def("set_option",
56  py::overload_cast<const std::string&, const std::set<std::string>&, const std::vector<std::string>&>(&ProgramArguments::set_option),
57  py::arg("flag"),
58  py::arg("equivalent_flags"),
59  py::arg("parameters"),
60  R"(
61  Set an option that can be addressed through any of several equivalent flags.
62 
63  :param str flag: The flag of the option.
64  :param set[str] equivalent_flags: All flags that address the same option.
65  :param list[str] parameters: The parameters of the option.
66  :returns: ``True`` on success, ``False`` otherwise.
67  :rtype: bool
68  )");
69 
70  py_program_arguments.def("get_parameter", &ProgramArguments::get_parameter, py::arg("flag"), R"(
71  Get the first parameter of an option.
72 
73  :param str flag: A flag of the option.
74  :returns: The parameter, or an empty string if the option is not set.
75  :rtype: str
76  )");
77 
78  py_program_arguments.def("get_parameters", &ProgramArguments::get_parameters, py::arg("flag"), R"(
79  Get all parameters of an option.
80 
81  :param str flag: A flag of the option.
82  :returns: A list of parameters, or an empty list if the option is not set.
83  :rtype: list[str]
84  )");
85 
86  py::class_<ProgramOptions> py_program_options(m, "ProgramOptions", R"(
87  Holds the command line options that can be configured from the command line or at runtime using ProgramArguments.
88  )");
89 
90  py_program_options.def_readonly_static("A_REQUIRED_PARAMETER", &ProgramOptions::A_REQUIRED_PARAMETER, R"(
91  Constant to specify that a parameter is required and does not have a default value.
92 
93  :type: str
94  )");
95 
96  py_program_options.def(py::init<const std::string&>(), py::arg("name") = std::string(), R"(
97  Construct a set of program options.
98 
99  :param str name: The name of this group of program options, used for grouping in ``get_options_string``. Defaults to an empty string.
100  )");
101 
102  py_program_options.def(
103  "parse",
104  [](ProgramOptions& self, const std::vector<std::string>& args) -> ProgramArguments {
105  std::vector<const char*> argv;
106  argv.reserve(args.size());
107  for (const auto& arg : args)
108  {
109  argv.push_back(arg.c_str());
110  }
111  return self.parse((int)argv.size(), argv.data());
112  },
113  py::arg("args"),
114  R"(
115  Parse the given command line arguments into the internal structure.
116  As on the command line, the first entry is expected to be the name of the program.
117 
118  :param list[str] args: The arguments.
119  :returns: The parsed arguments. The original arguments are not retained.
120  :rtype: hal_py.ProgramArguments
121  )");
122 
123  py_program_options.def("get_unknown_arguments", &ProgramOptions::get_unknown_arguments, R"(
124  Get the command line arguments that could not be parsed.
125  Only valid after ``parse`` was called.
126 
127  :returns: A list of all arguments that could not be parsed.
128  :rtype: list[str]
129  )");
130 
131  py_program_options.def("is_registered", &ProgramOptions::is_registered, py::arg("flag"), R"(
132  Check whether a flag is already registered for an option.
133  No flag can be registered twice.
134 
135  :param str flag: The flag to check.
136  :returns: ``True`` if the flag is already registered, ``False`` otherwise.
137  :rtype: bool
138  )");
139 
140  py_program_options.def("add",
141  [](ProgramOptions& self, const std::string& flag, const std::string& description, const std::vector<std::string>& parameters) { return self.add_flags({flag}, description, parameters); },
142  py::arg("flag"),
143  py::arg("description"),
144  py::arg("parameters") = std::vector<std::string>(),
145  R"(
146  Add a new option with a single flag.
147  The length of ``parameters`` is the number of parameters this option takes, its entries are the default values of these parameters.
148  Use ``hal_py.ProgramOptions.A_REQUIRED_PARAMETER`` to mark a parameter as required.
149 
150  :param str flag: The flag activating the option.
151  :param str description: A description of the option.
152  :param list[str] parameters: A list of default values for all parameters. Defaults to an empty list.
153  :returns: ``True`` on success, ``False`` otherwise.
154  :rtype: bool
155  )");
156 
157  py_program_options.def("add",
159  py::arg("flags"),
160  py::arg("description"),
161  py::arg("parameters") = std::vector<std::string>(),
162  R"(
163  Add a new option with multiple flags.
164  The length of ``parameters`` is the number of parameters this option takes, its entries are the default values of these parameters.
165  Use ``hal_py.ProgramOptions.A_REQUIRED_PARAMETER`` to mark a parameter as required.
166 
167  :param list[str] flags: The flags activating the option.
168  :param str description: A description of the option.
169  :param list[str] parameters: A list of default values for all parameters. Defaults to an empty list.
170  :returns: ``True`` on success, ``False`` otherwise.
171  :rtype: bool
172  )");
173 
174  py_program_options.def("add", py::overload_cast<const ProgramOptions&, const std::string&>(&ProgramOptions::add), py::arg("other_options"), py::arg("category") = std::string(), R"(
175  Add another set of options.
176 
177  :param hal_py.ProgramOptions other_options: The set of options to add.
178  :param str category: A category for the added options, used for grouping in ``get_options_string``. Defaults to an empty string.
179  :returns: ``True`` on success, ``False`` otherwise.
180  :rtype: bool
181  )");
182 
183  py_program_options.def("remove", &ProgramOptions::remove, py::arg("flag"), R"(
184  Remove a single flag.
185  If multiple flags for an option exist, the others will still remain available.
186 
187  :param str flag: The flag activating the option.
188  :returns: ``True`` if the option was found, ``False`` otherwise.
189  :rtype: bool
190  )");
191 
192  py_program_options.def("get_options_string", &ProgramOptions::get_options_string, R"(
193  Get a formatted string of all options and their description, including the categories of added options.
194  Useful for usage messages.
195 
196  :returns: The formatted string.
197  :rtype: str
198  )");
199 
200  py_program_options.def("get_options", &ProgramOptions::get_options, R"(
201  Get the flags and the description of all options.
202 
203  :returns: A list of tuples comprising the set of all flags of the option and the description of the option.
204  :rtype: list[tuple(set[str],str)]
205  )");
206  }
207 } // 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
bool is_option_set(const std::string &flag) const
void set_option(const std::string &flag, const std::string &parameter)
std::string get_options_string() const
bool add_flags(const std::vector< std::string > &flags, const std::string &description, const std::vector< std::string > &parameters={})
bool is_registered(const std::string &flag) const
bool remove(const std::string &flag)
std::vector< std::tuple< std::set< std::string >, std::string > > get_options() const
bool add(const std::string &flag, const std::string &description, const std::initializer_list< std::string > &parameters={})
std::vector< std::string > get_unknown_arguments()
static const std::string A_REQUIRED_PARAMETER
constant to specify that a parameter is required and does not have a default value.
void program_options_init(py::module &m)
const Module * module(const Gate *g, const NodeBoxes &boxes)
Definition: defines.h:45