1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
|
/*
* Copyright (c) 2017-2020, 2022 Arm Limited.
*
* SPDX-License-Identifier: MIT
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef ARM_COMPUTE_UTILS_COMMANDLINEPARSER
#define ARM_COMPUTE_UTILS_COMMANDLINEPARSER
#include "arm_compute/core/utils/misc/Utility.h"
#include "Option.h"
#include <cstring>
#include <iostream>
#include <map>
#include <memory>
#include <regex>
#include <string>
#include <utility>
#include <vector>
namespace arm_compute
{
namespace utils
{
/** Class to parse command line arguments. */
class CommandLineParser final
{
public:
/** Default constructor. */
CommandLineParser() = default;
/** Function to add a new option to the parser.
*
* @param[in] name Name of the option. Will be available under --name=VALUE.
* @param[in] args Option specific configuration arguments.
*
* @return Pointer to the option. The option is owned by the parser.
*/
template <typename T, typename... As>
T *add_option(const std::string &name, As &&...args);
/** Function to add a new positional argument to the parser.
*
* @param[in] args Option specific configuration arguments.
*
* @return Pointer to the option. The option is owned by the parser.
*/
template <typename T, typename... As>
T *add_positional_option(As &&...args);
/** Parses the command line arguments and updates the options accordingly.
*
* @param[in] argc Number of arguments.
* @param[in] argv Arguments.
*/
void parse(int argc, char **argv);
/** Validates the previously parsed command line arguments.
*
* Validation fails if not all required options are provided. Additionally
* warnings are generated for options that have illegal values or unknown
* options.
*
* @return True if all required options have been provided.
*/
bool validate() const;
/** Prints a help message for all configured options.
*
* @param[in] program_name Name of the program to be used in the help message.
*/
void print_help(const std::string &program_name) const;
private:
using OptionsMap = std::map<std::string, std::unique_ptr<Option>>;
using PositionalOptionsVector = std::vector<std::unique_ptr<Option>>;
OptionsMap _options{};
PositionalOptionsVector _positional_options{};
std::vector<std::string> _unknown_options{};
std::vector<std::string> _invalid_options{};
};
template <typename T, typename... As>
inline T *CommandLineParser::add_option(const std::string &name, As &&...args)
{
auto result = _options.emplace(name, std::make_unique<T>(name, std::forward<As>(args)...));
return static_cast<T *>(result.first->second.get());
}
template <typename T, typename... As>
inline T *CommandLineParser::add_positional_option(As &&...args)
{
_positional_options.emplace_back(std::make_unique<T>(std::forward<As>(args)...));
return static_cast<T *>(_positional_options.back().get());
}
inline void CommandLineParser::parse(int argc, char **argv)
{
const std::regex option_regex{"--((?:no-)?)([^=]+)(?:=(.*))?"};
const auto set_option = [&](const std::string &option, const std::string &name, const std::string &value)
{
if (_options.find(name) == _options.end())
{
_unknown_options.push_back(option);
return;
}
const bool success = _options[name]->parse(value);
if (!success)
{
_invalid_options.push_back(option);
}
};
unsigned int positional_index = 0;
for (int i = 1; i < argc; ++i)
{
std::string mixed_case_opt{argv[i]};
int equal_sign = mixed_case_opt.find('=');
int pos = (equal_sign == -1) ? strlen(argv[i]) : equal_sign;
const std::string option =
arm_compute::utility::tolower(mixed_case_opt.substr(0, pos)) + mixed_case_opt.substr(pos);
std::smatch option_matches;
if (std::regex_match(option, option_matches, option_regex))
{
// Boolean option
if (option_matches.str(3).empty())
{
set_option(option, option_matches.str(2), option_matches.str(1).empty() ? "true" : "false");
}
else
{
// Can't have "no-" and a value
if (!option_matches.str(1).empty())
{
_invalid_options.emplace_back(option);
}
else
{
set_option(option, option_matches.str(2), option_matches.str(3));
}
}
}
else
{
if (positional_index >= _positional_options.size())
{
_invalid_options.push_back(mixed_case_opt);
}
else
{
_positional_options[positional_index]->parse(mixed_case_opt);
++positional_index;
}
}
}
}
inline bool CommandLineParser::validate() const
{
bool is_valid = true;
for (const auto &option : _options)
{
if (option.second->is_required() && !option.second->is_set())
{
is_valid = false;
std::cerr << "ERROR: Option '" << option.second->name() << "' is required but not given!\n";
}
}
for (const auto &option : _positional_options)
{
if (option->is_required() && !option->is_set())
{
is_valid = false;
std::cerr << "ERROR: Option '" << option->name() << "' is required but not given!\n";
}
}
for (const auto &option : _unknown_options)
{
std::cerr << "WARNING: Skipping unknown option '" << option << "'!\n";
}
for (const auto &option : _invalid_options)
{
std::cerr << "WARNING: Skipping invalid option '" << option << "'!\n";
}
return is_valid;
}
inline void CommandLineParser::print_help(const std::string &program_name) const
{
std::cout << "usage: " << program_name << " \n";
for (const auto &option : _options)
{
std::cout << option.second->help() << "\n";
}
for (const auto &option : _positional_options)
{
std::string help_to_print;
// Extract help sub-string
const std::string help_str = option->help();
const size_t help_pos = help_str.find(" - ");
if (help_pos != std::string::npos)
{
help_to_print = help_str.substr(help_pos);
}
std::cout << option->name() << help_to_print << "\n";
}
}
} // namespace utils
} // namespace arm_compute
#endif /* ARM_COMPUTE_UTILS_COMMANDLINEPARSER */
|