blob: 57796bce73243ce12ac84e36cfb0298388ae610f [file] [log] [blame]
Georgios Pinitas12be7ab2018-07-03 12:06:23 +01001/*
Pablo Marquez Telloa774d632022-05-31 19:39:18 +01002 * Copyright (c) 2017-2020, 2022 Arm Limited.
Georgios Pinitas12be7ab2018-07-03 12:06:23 +01003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#ifndef ARM_COMPUTE_UTILS_COMMANDLINEPARSER
25#define ARM_COMPUTE_UTILS_COMMANDLINEPARSER
26
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010027#include "arm_compute/core/utils/misc/Utility.h"
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010028
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010029#include "Option.h"
Pablo Marquez Telloa774d632022-05-31 19:39:18 +010030#include <cstring>
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010031#include <iostream>
32#include <map>
33#include <memory>
34#include <regex>
35#include <string>
36#include <utility>
37#include <vector>
38
39namespace arm_compute
40{
41namespace utils
42{
43/** Class to parse command line arguments. */
44class CommandLineParser final
45{
46public:
47 /** Default constructor. */
48 CommandLineParser() = default;
49
50 /** Function to add a new option to the parser.
51 *
52 * @param[in] name Name of the option. Will be available under --name=VALUE.
53 * @param[in] args Option specific configuration arguments.
54 *
55 * @return Pointer to the option. The option is owned by the parser.
56 */
57 template <typename T, typename... As>
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010058 T *add_option(const std::string &name, As &&...args);
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010059
60 /** Function to add a new positional argument to the parser.
61 *
62 * @param[in] args Option specific configuration arguments.
63 *
64 * @return Pointer to the option. The option is owned by the parser.
65 */
66 template <typename T, typename... As>
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010067 T *add_positional_option(As &&...args);
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010068
69 /** Parses the command line arguments and updates the options accordingly.
70 *
71 * @param[in] argc Number of arguments.
72 * @param[in] argv Arguments.
73 */
74 void parse(int argc, char **argv);
75
76 /** Validates the previously parsed command line arguments.
77 *
78 * Validation fails if not all required options are provided. Additionally
79 * warnings are generated for options that have illegal values or unknown
80 * options.
81 *
82 * @return True if all required options have been provided.
83 */
84 bool validate() const;
85
86 /** Prints a help message for all configured options.
87 *
88 * @param[in] program_name Name of the program to be used in the help message.
89 */
90 void print_help(const std::string &program_name) const;
91
92private:
93 using OptionsMap = std::map<std::string, std::unique_ptr<Option>>;
94 using PositionalOptionsVector = std::vector<std::unique_ptr<Option>>;
95
96 OptionsMap _options{};
97 PositionalOptionsVector _positional_options{};
98 std::vector<std::string> _unknown_options{};
99 std::vector<std::string> _invalid_options{};
100};
101
102template <typename T, typename... As>
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100103inline T *CommandLineParser::add_option(const std::string &name, As &&...args)
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100104{
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000105 auto result = _options.emplace(name, std::make_unique<T>(name, std::forward<As>(args)...));
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100106 return static_cast<T *>(result.first->second.get());
107}
108
109template <typename T, typename... As>
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100110inline T *CommandLineParser::add_positional_option(As &&...args)
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100111{
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000112 _positional_options.emplace_back(std::make_unique<T>(std::forward<As>(args)...));
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100113 return static_cast<T *>(_positional_options.back().get());
114}
115
116inline void CommandLineParser::parse(int argc, char **argv)
117{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100118 const std::regex option_regex{"--((?:no-)?)([^=]+)(?:=(.*))?"};
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100119
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100120 const auto set_option = [&](const std::string &option, const std::string &name, const std::string &value)
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100121 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100122 if (_options.find(name) == _options.end())
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100123 {
124 _unknown_options.push_back(option);
125 return;
126 }
127
128 const bool success = _options[name]->parse(value);
129
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100130 if (!success)
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100131 {
132 _invalid_options.push_back(option);
133 }
134 };
135
136 unsigned int positional_index = 0;
137
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100138 for (int i = 1; i < argc; ++i)
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100139 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100140 std::string mixed_case_opt{argv[i]};
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100141 int equal_sign = mixed_case_opt.find('=');
142 int pos = (equal_sign == -1) ? strlen(argv[i]) : equal_sign;
143
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100144 const std::string option =
145 arm_compute::utility::tolower(mixed_case_opt.substr(0, pos)) + mixed_case_opt.substr(pos);
146 std::smatch option_matches;
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100147
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100148 if (std::regex_match(option, option_matches, option_regex))
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100149 {
150 // Boolean option
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100151 if (option_matches.str(3).empty())
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100152 {
153 set_option(option, option_matches.str(2), option_matches.str(1).empty() ? "true" : "false");
154 }
155 else
156 {
157 // Can't have "no-" and a value
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100158 if (!option_matches.str(1).empty())
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100159 {
160 _invalid_options.emplace_back(option);
161 }
162 else
163 {
164 set_option(option, option_matches.str(2), option_matches.str(3));
165 }
166 }
167 }
168 else
169 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100170 if (positional_index >= _positional_options.size())
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100171 {
172 _invalid_options.push_back(mixed_case_opt);
173 }
174 else
175 {
176 _positional_options[positional_index]->parse(mixed_case_opt);
177 ++positional_index;
178 }
179 }
180 }
181}
182
183inline bool CommandLineParser::validate() const
184{
185 bool is_valid = true;
186
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100187 for (const auto &option : _options)
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100188 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100189 if (option.second->is_required() && !option.second->is_set())
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100190 {
191 is_valid = false;
192 std::cerr << "ERROR: Option '" << option.second->name() << "' is required but not given!\n";
193 }
194 }
195
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100196 for (const auto &option : _positional_options)
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100197 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100198 if (option->is_required() && !option->is_set())
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100199 {
200 is_valid = false;
201 std::cerr << "ERROR: Option '" << option->name() << "' is required but not given!\n";
202 }
203 }
204
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100205 for (const auto &option : _unknown_options)
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100206 {
207 std::cerr << "WARNING: Skipping unknown option '" << option << "'!\n";
208 }
209
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100210 for (const auto &option : _invalid_options)
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100211 {
212 std::cerr << "WARNING: Skipping invalid option '" << option << "'!\n";
213 }
214
215 return is_valid;
216}
217
218inline void CommandLineParser::print_help(const std::string &program_name) const
219{
220 std::cout << "usage: " << program_name << " \n";
221
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100222 for (const auto &option : _options)
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100223 {
224 std::cout << option.second->help() << "\n";
225 }
226
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100227 for (const auto &option : _positional_options)
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100228 {
Georgios Pinitase71eb6f2019-06-17 18:25:43 +0100229 std::string help_to_print;
230
231 // Extract help sub-string
232 const std::string help_str = option->help();
233 const size_t help_pos = help_str.find(" - ");
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100234 if (help_pos != std::string::npos)
Georgios Pinitase71eb6f2019-06-17 18:25:43 +0100235 {
236 help_to_print = help_str.substr(help_pos);
237 }
238
239 std::cout << option->name() << help_to_print << "\n";
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100240 }
241}
242} // namespace utils
243} // namespace arm_compute
244#endif /* ARM_COMPUTE_UTILS_COMMANDLINEPARSER */