Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Ryan Mitchell | 52e1f7a | 2019-04-12 12:31:42 -0700 | [diff] [blame] | 17 | #include "idmap2/CommandLineOptions.h" |
| 18 | |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 19 | #include <algorithm> |
Ryan Prichard | d9a3ffa | 2022-08-30 14:24:57 -0700 | [diff] [blame] | 20 | #include <cassert> |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 21 | #include <iomanip> |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 22 | #include <memory> |
Yurii Zubrytskyi | a29f3c0 | 2023-05-12 16:05:13 -0700 | [diff] [blame^] | 23 | #include <ostream> |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 24 | #include <set> |
Mårten Kongstad | 0c6ff1d | 2019-02-07 02:21:56 +0100 | [diff] [blame] | 25 | #include <sstream> |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 26 | #include <string> |
| 27 | #include <vector> |
| 28 | |
| 29 | #include "android-base/macros.h" |
Mårten Kongstad | 0c6ff1d | 2019-02-07 02:21:56 +0100 | [diff] [blame] | 30 | #include "idmap2/Result.h" |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 31 | |
Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 32 | namespace android::idmap2 { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 33 | |
| 34 | std::unique_ptr<std::vector<std::string>> CommandLineOptions::ConvertArgvToVector( |
| 35 | int argc, const char** argv) { |
Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 36 | return std::make_unique<std::vector<std::string>>(argv + 1, argv + argc); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | CommandLineOptions& CommandLineOptions::OptionalFlag(const std::string& name, |
| 40 | const std::string& description, bool* value) { |
| 41 | assert(value != nullptr); |
| 42 | auto func = [value](const std::string& arg ATTRIBUTE_UNUSED) -> void { *value = true; }; |
| 43 | options_.push_back(Option{name, description, func, Option::COUNT_OPTIONAL, false}); |
| 44 | return *this; |
| 45 | } |
| 46 | |
| 47 | CommandLineOptions& CommandLineOptions::MandatoryOption(const std::string& name, |
| 48 | const std::string& description, |
| 49 | std::string* value) { |
| 50 | assert(value != nullptr); |
| 51 | auto func = [value](const std::string& arg) -> void { *value = arg; }; |
| 52 | options_.push_back(Option{name, description, func, Option::COUNT_EXACTLY_ONCE, true}); |
| 53 | return *this; |
| 54 | } |
| 55 | |
| 56 | CommandLineOptions& CommandLineOptions::MandatoryOption(const std::string& name, |
| 57 | const std::string& description, |
| 58 | std::vector<std::string>* value) { |
| 59 | assert(value != nullptr); |
| 60 | auto func = [value](const std::string& arg) -> void { value->push_back(arg); }; |
| 61 | options_.push_back(Option{name, description, func, Option::COUNT_ONCE_OR_MORE, true}); |
| 62 | return *this; |
| 63 | } |
| 64 | |
| 65 | CommandLineOptions& CommandLineOptions::OptionalOption(const std::string& name, |
| 66 | const std::string& description, |
| 67 | std::string* value) { |
| 68 | assert(value != nullptr); |
| 69 | auto func = [value](const std::string& arg) -> void { *value = arg; }; |
| 70 | options_.push_back(Option{name, description, func, Option::COUNT_OPTIONAL, true}); |
| 71 | return *this; |
| 72 | } |
| 73 | |
Mårten Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame] | 74 | CommandLineOptions& CommandLineOptions::OptionalOption(const std::string& name, |
| 75 | const std::string& description, |
| 76 | std::vector<std::string>* value) { |
| 77 | assert(value != nullptr); |
| 78 | auto func = [value](const std::string& arg) -> void { value->push_back(arg); }; |
| 79 | options_.push_back(Option{name, description, func, Option::COUNT_OPTIONAL_ONCE_OR_MORE, true}); |
| 80 | return *this; |
| 81 | } |
| 82 | |
Mårten Kongstad | 0c6ff1d | 2019-02-07 02:21:56 +0100 | [diff] [blame] | 83 | Result<Unit> CommandLineOptions::Parse(const std::vector<std::string>& argv) const { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 84 | const auto pivot = std::partition(options_.begin(), options_.end(), [](const Option& opt) { |
Mårten Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame] | 85 | return opt.count != Option::COUNT_OPTIONAL && opt.count != Option::COUNT_OPTIONAL_ONCE_OR_MORE; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 86 | }); |
| 87 | std::set<std::string> mandatory_opts; |
| 88 | std::transform(options_.begin(), pivot, std::inserter(mandatory_opts, mandatory_opts.end()), |
| 89 | [](const Option& opt) -> std::string { return opt.name; }); |
| 90 | |
| 91 | const size_t argv_size = argv.size(); |
| 92 | for (size_t i = 0; i < argv_size; i++) { |
| 93 | const std::string arg = argv[i]; |
| 94 | if ("--help" == arg || "-h" == arg) { |
Mårten Kongstad | 0c6ff1d | 2019-02-07 02:21:56 +0100 | [diff] [blame] | 95 | std::stringstream stream; |
| 96 | Usage(stream); |
| 97 | return Error("%s", stream.str().c_str()); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 98 | } |
| 99 | bool match = false; |
| 100 | for (const Option& opt : options_) { |
| 101 | if (opt.name == arg) { |
| 102 | match = true; |
| 103 | |
| 104 | if (opt.argument) { |
| 105 | i++; |
| 106 | if (i >= argv_size) { |
Mårten Kongstad | 0c6ff1d | 2019-02-07 02:21:56 +0100 | [diff] [blame] | 107 | std::stringstream stream; |
| 108 | Usage(stream); |
| 109 | return Error("%s: missing argument\n%s", opt.name.c_str(), stream.str().c_str()); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | opt.action(argv[i]); |
| 113 | mandatory_opts.erase(opt.name); |
| 114 | break; |
| 115 | } |
| 116 | } |
| 117 | if (!match) { |
Mårten Kongstad | 0c6ff1d | 2019-02-07 02:21:56 +0100 | [diff] [blame] | 118 | std::stringstream stream; |
| 119 | Usage(stream); |
| 120 | return Error("%s: unknown option\n%s", arg.c_str(), stream.str().c_str()); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
| 124 | if (!mandatory_opts.empty()) { |
Mårten Kongstad | 0c6ff1d | 2019-02-07 02:21:56 +0100 | [diff] [blame] | 125 | std::stringstream stream; |
| 126 | bool separator = false; |
Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 127 | for (const auto& opt : mandatory_opts) { |
Mårten Kongstad | 0c6ff1d | 2019-02-07 02:21:56 +0100 | [diff] [blame] | 128 | if (separator) { |
| 129 | stream << ", "; |
| 130 | } |
| 131 | separator = true; |
| 132 | stream << opt << ": missing mandatory option"; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 133 | } |
Yurii Zubrytskyi | a29f3c0 | 2023-05-12 16:05:13 -0700 | [diff] [blame^] | 134 | stream << '\n'; |
Mårten Kongstad | 0c6ff1d | 2019-02-07 02:21:56 +0100 | [diff] [blame] | 135 | Usage(stream); |
| 136 | return Error("%s", stream.str().c_str()); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 137 | } |
Mårten Kongstad | 0c6ff1d | 2019-02-07 02:21:56 +0100 | [diff] [blame] | 138 | return Unit{}; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | void CommandLineOptions::Usage(std::ostream& out) const { |
| 142 | size_t maxLength = 0; |
| 143 | out << "usage: " << name_; |
| 144 | for (const Option& opt : options_) { |
Mårten Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame] | 145 | const bool mandatory = |
| 146 | opt.count != Option::COUNT_OPTIONAL && opt.count != Option::COUNT_OPTIONAL_ONCE_OR_MORE; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 147 | out << " "; |
| 148 | if (!mandatory) { |
| 149 | out << "["; |
| 150 | } |
| 151 | if (opt.argument) { |
| 152 | out << opt.name << " arg"; |
| 153 | maxLength = std::max(maxLength, opt.name.size() + 4); |
| 154 | } else { |
| 155 | out << opt.name; |
| 156 | maxLength = std::max(maxLength, opt.name.size()); |
| 157 | } |
Mårten Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame] | 158 | |
| 159 | if (opt.count == Option::COUNT_OPTIONAL_ONCE_OR_MORE) { |
| 160 | out << " [..]"; |
| 161 | } |
| 162 | |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 163 | if (!mandatory) { |
| 164 | out << "]"; |
| 165 | } |
Mårten Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame] | 166 | |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 167 | if (opt.count == Option::COUNT_ONCE_OR_MORE) { |
| 168 | out << " [" << opt.name << " arg [..]]"; |
| 169 | } |
| 170 | } |
Yurii Zubrytskyi | a29f3c0 | 2023-05-12 16:05:13 -0700 | [diff] [blame^] | 171 | out << "\n\n"; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 172 | for (const Option& opt : options_) { |
| 173 | out << std::left << std::setw(maxLength); |
| 174 | if (opt.argument) { |
| 175 | out << (opt.name + " arg"); |
| 176 | } else { |
| 177 | out << opt.name; |
| 178 | } |
| 179 | out << " " << opt.description; |
Mårten Kongstad | d10d06d | 2019-01-07 17:26:25 -0800 | [diff] [blame] | 180 | if (opt.count == Option::COUNT_ONCE_OR_MORE || |
| 181 | opt.count == Option::COUNT_OPTIONAL_ONCE_OR_MORE) { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 182 | out << " (can be provided multiple times)"; |
| 183 | } |
Yurii Zubrytskyi | a29f3c0 | 2023-05-12 16:05:13 -0700 | [diff] [blame^] | 184 | out << '\n'; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 188 | } // namespace android::idmap2 |