Moritz Pflanzer | bb4a79b | 2017-07-05 11:02:46 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 ARM Limited. |
| 3 | * |
| 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_TEST_COMMANDLINEPARSER |
| 25 | #define ARM_COMPUTE_TEST_COMMANDLINEPARSER |
| 26 | |
| 27 | #include "../Utils.h" |
| 28 | #include "Option.h" |
| 29 | |
| 30 | #include <map> |
| 31 | #include <memory> |
| 32 | #include <string> |
| 33 | #include <utility> |
| 34 | #include <vector> |
| 35 | |
| 36 | namespace arm_compute |
| 37 | { |
| 38 | namespace test |
| 39 | { |
| 40 | namespace framework |
| 41 | { |
| 42 | /** Class to parse command line arguments. */ |
| 43 | class CommandLineParser final |
| 44 | { |
| 45 | public: |
| 46 | /** Default constructor. */ |
| 47 | CommandLineParser() = default; |
| 48 | |
| 49 | /** Function to add a new option to the parser. |
| 50 | * |
| 51 | * @param[in] name Name of the option. Will be available under --name=VALUE. |
| 52 | * @param[in] args Option specific configuration arguments. |
| 53 | * |
| 54 | * @return Pointer to the option. The option is owned by the parser. |
| 55 | */ |
| 56 | template <typename T, typename... As> |
| 57 | T *add_option(const std::string &name, As &&... args); |
| 58 | |
| 59 | /** Function to add a new positional argument to the parser. |
| 60 | * |
| 61 | * @param[in] args Option specific configuration arguments. |
| 62 | * |
| 63 | * @return Pointer to the option. The option is owned by the parser. |
| 64 | */ |
| 65 | template <typename T, typename... As> |
| 66 | T *add_positional_option(As &&... args); |
| 67 | |
| 68 | /** Parses the command line arguments and updates the options accordingly. |
| 69 | * |
| 70 | * @param[in] argc Number of arguments. |
| 71 | * @param[in] argv Arguments. |
| 72 | */ |
| 73 | void parse(int argc, char **argv); |
| 74 | |
| 75 | /** Validates the previously parsed command line arguments. |
| 76 | * |
| 77 | * Validation fails if not all required options are provided. Additionally |
| 78 | * warnings are generated for options that have illegal values or unknown |
| 79 | * options. |
| 80 | * |
| 81 | * @return True if all required options have been provided. |
| 82 | */ |
| 83 | bool validate() const; |
| 84 | |
| 85 | /** Prints a help message for all configured options. |
| 86 | * |
| 87 | * @param[in] program_name Name of the program to be used in the help message. |
| 88 | */ |
| 89 | void print_help(const std::string &program_name) const; |
| 90 | |
| 91 | private: |
| 92 | using OptionsMap = std::map<std::string, std::unique_ptr<Option>>; |
| 93 | using PositionalOptionsVector = std::vector<std::unique_ptr<Option>>; |
| 94 | |
| 95 | OptionsMap _options{}; |
| 96 | PositionalOptionsVector _positional_options{}; |
| 97 | std::vector<std::string> _unknown_options{}; |
| 98 | std::vector<std::string> _invalid_options{}; |
| 99 | }; |
| 100 | |
| 101 | template <typename T, typename... As> |
| 102 | inline T *CommandLineParser::add_option(const std::string &name, As &&... args) |
| 103 | { |
| 104 | auto result = _options.emplace(name, support::cpp14::make_unique<T>(name, std::forward<As>(args)...)); |
| 105 | return static_cast<T *>(result.first->second.get()); |
| 106 | } |
| 107 | |
| 108 | template <typename T, typename... As> |
| 109 | inline T *CommandLineParser::add_positional_option(As &&... args) |
| 110 | { |
| 111 | _positional_options.emplace_back(support::cpp14::make_unique<T>(std::forward<As>(args)...)); |
| 112 | return static_cast<T *>(_positional_options.back().get()); |
| 113 | } |
| 114 | } // namespace framework |
| 115 | } // namespace test |
| 116 | } // namespace arm_compute |
| 117 | #endif /* ARM_COMPUTE_TEST_COMMANDLINEPARSER */ |