blob: 09b466ce849ab81bf7839e83de8e59154db26fa3 [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 {
Giorgio Arena0c1d1482017-10-25 15:10:41 +010059 std::string mixed_case_opt{ argv[i] };
60 int equal_sign = mixed_case_opt.find('=');
61 int pos = (equal_sign == -1) ? strlen(argv[i]) : equal_sign;
62
63 const std::string option = tolower(mixed_case_opt.substr(0, pos)) + mixed_case_opt.substr(pos);
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010064 std::smatch option_matches;
65
66 if(std::regex_match(option, option_matches, option_regex))
67 {
68 // Boolean option
69 if(option_matches.str(3).empty())
70 {
71 set_option(option, option_matches.str(2), option_matches.str(1).empty() ? "true" : "false");
72 }
73 else
74 {
75 // Can't have "no-" and a value
76 if(!option_matches.str(1).empty())
77 {
78 _invalid_options.emplace_back(option);
79 }
80 else
81 {
82 set_option(option, option_matches.str(2), option_matches.str(3));
83 }
84 }
85 }
86 else
87 {
88 if(positional_index >= _positional_options.size())
89 {
Georgios Pinitasb40879e2017-12-01 14:16:54 +000090 _invalid_options.push_back(mixed_case_opt);
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010091 }
92 else
93 {
Georgios Pinitasb40879e2017-12-01 14:16:54 +000094 _positional_options[positional_index]->parse(mixed_case_opt);
Moritz Pflanzerbb4a79b2017-07-05 11:02:46 +010095 ++positional_index;
96 }
97 }
98 }
99}
100
101bool CommandLineParser::validate() const
102{
103 bool is_valid = true;
104
105 for(const auto &option : _options)
106 {
107 if(option.second->is_required() && !option.second->is_set())
108 {
109 is_valid = false;
110 std::cerr << "ERROR: Option '" << option.second->name() << "' is required but not given!\n";
111 }
112 }
113
114 for(const auto &option : _positional_options)
115 {
116 if(option->is_required() && !option->is_set())
117 {
118 is_valid = false;
119 std::cerr << "ERROR: Option '" << option->name() << "' is required but not given!\n";
120 }
121 }
122
123 for(const auto &option : _unknown_options)
124 {
125 std::cerr << "WARNING: Skipping unknown option '" << option << "'!\n";
126 }
127
128 for(const auto &option : _invalid_options)
129 {
130 std::cerr << "WARNING: Skipping invalid option '" << option << "'!\n";
131 }
132
133 return is_valid;
134}
135
136void CommandLineParser::print_help(const std::string &program_name) const
137{
138 std::cout << "usage: " << program_name << " \n";
139
140 for(const auto &option : _options)
141 {
142 std::cout << option.second->help() << "\n";
143 }
144
145 for(const auto &option : _positional_options)
146 {
147 //FIXME: Print help string as well
148 std::cout << option->name() << "\n";
149 }
150}
151} // namespace framework
152} // namespace test
153} // namespace arm_compute