blob: 228b18d84223cb2ae07c14dae06afea73eee5c70 [file] [log] [blame]
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +01001/*
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#include "CommandLineParser.h"
25
26#include <iostream>
27#include <regex>
28
29namespace arm_compute
30{
31namespace test
32{
33namespace framework
34{
35void CommandLineParser::parse(int argc, char **argv)
36{
37 const std::regex option_regex{ "--((?:no-)?)([^=]+)(?:=(.*))?" };
38
39 const auto set_option = [&](const std::string & option, const std::string & name, const std::string & value)
40 {
41 if(_options.find(name) == _options.end())
42 {
43 _unknown_options.push_back(option);
44 return;
45 }
46
47 const bool success = _options[name]->parse(value);
48
49 if(!success)
50 {
51 _invalid_options.push_back(option);
52 }
53 };
54
55 unsigned int positional_index = 0;
56
57 for(int i = 1; i < argc; ++i)
58 {
59 const std::string option{ argv[i] };
60 std::smatch option_matches;
61
62 if(std::regex_match(option, option_matches, option_regex))
63 {
64 // Boolean option
65 if(option_matches.str(3).empty())
66 {
67 set_option(option, option_matches.str(2), option_matches.str(1).empty() ? "true" : "false");
68 }
69 else
70 {
71 // Can't have "no-" and a value
72 if(!option_matches.str(1).empty())
73 {
74 _invalid_options.emplace_back(option);
75 }
76 else
77 {
78 set_option(option, option_matches.str(2), option_matches.str(3));
79 }
80 }
81 }
82 else
83 {
84 if(positional_index >= _positional_options.size())
85 {
86 _invalid_options.push_back(option);
87 }
88 else
89 {
90 _positional_options[positional_index]->parse(option);
91 ++positional_index;
92 }
93 }
94 }
95}
96
97bool CommandLineParser::validate() const
98{
99 bool is_valid = true;
100
101 for(const auto &option : _options)
102 {
103 if(option.second->is_required() && !option.second->is_set())
104 {
105 is_valid = false;
106 std::cerr << "ERROR: Option '" << option.second->name() << "' is required but not given!\n";
107 }
108 }
109
110 for(const auto &option : _positional_options)
111 {
112 if(option->is_required() && !option->is_set())
113 {
114 is_valid = false;
115 std::cerr << "ERROR: Option '" << option->name() << "' is required but not given!\n";
116 }
117 }
118
119 for(const auto &option : _unknown_options)
120 {
121 std::cerr << "WARNING: Skipping unknown option '" << option << "'!\n";
122 }
123
124 for(const auto &option : _invalid_options)
125 {
126 std::cerr << "WARNING: Skipping invalid option '" << option << "'!\n";
127 }
128
129 return is_valid;
130}
131
132void CommandLineParser::print_help(const std::string &program_name) const
133{
134 std::cout << "usage: " << program_name << " \n";
135
136 for(const auto &option : _options)
137 {
138 std::cout << option.second->help() << "\n";
139 }
140
141 for(const auto &option : _positional_options)
142 {
143 //FIXME: Print help string as well
144 std::cout << option->name() << "\n";
145 }
146}
147} // namespace framework
148} // namespace test
149} // namespace arm_compute