Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [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 | |
| 17 | #include "Command.h" |
| 18 | |
| 19 | #include <iomanip> |
| 20 | #include <iostream> |
| 21 | #include <string> |
| 22 | #include <vector> |
| 23 | |
Ryan Mitchell | 2c8fc86 | 2018-12-13 16:56:07 -0800 | [diff] [blame^] | 24 | #include "android-base/stringprintf.h" |
| 25 | #include "android-base/utf8.h" |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 26 | #include "androidfw/StringPiece.h" |
| 27 | |
| 28 | #include "util/Util.h" |
| 29 | |
Ryan Mitchell | 2c8fc86 | 2018-12-13 16:56:07 -0800 | [diff] [blame^] | 30 | using android::base::StringPrintf; |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 31 | using android::StringPiece; |
| 32 | |
| 33 | namespace aapt { |
| 34 | |
Ryan Mitchell | 2c8fc86 | 2018-12-13 16:56:07 -0800 | [diff] [blame^] | 35 | std::string GetSafePath(const StringPiece& arg) { |
| 36 | #ifdef _WIN32 |
| 37 | // If the path exceeds the maximum path length for Windows, encode the path using the |
| 38 | // extended-length prefix |
| 39 | std::wstring path16; |
| 40 | CHECK(android::base::UTF8PathToWindowsLongPath(arg.data(), &path16)) |
| 41 | << "Failed to convert file path to UTF-16: file path " << arg.data(); |
| 42 | |
| 43 | std::string path8; |
| 44 | CHECK(android::base::WideToUTF8(path16, &path8)) |
| 45 | << "Failed to convert file path back to UTF-8: file path " << arg.data(); |
| 46 | |
| 47 | return path8; |
| 48 | #else |
| 49 | return arg.to_string(); |
| 50 | #endif |
| 51 | } |
| 52 | |
| 53 | void Command::AddRequiredFlag(const StringPiece& name, const StringPiece& description, |
| 54 | std::string* value, uint32_t flags) { |
| 55 | auto func = [value, flags](const StringPiece& arg) -> bool { |
| 56 | *value = (flags & Command::kPath) ? GetSafePath(arg) : arg.to_string(); |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 57 | return true; |
| 58 | }; |
| 59 | |
Ryan Mitchell | 2c8fc86 | 2018-12-13 16:56:07 -0800 | [diff] [blame^] | 60 | flags_.emplace_back(Flag(name, description, /* required */ true, /* num_args */ 1, func)); |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Ryan Mitchell | 2c8fc86 | 2018-12-13 16:56:07 -0800 | [diff] [blame^] | 63 | void Command::AddRequiredFlagList(const StringPiece& name, const StringPiece& description, |
| 64 | std::vector<std::string>* value, uint32_t flags) { |
| 65 | auto func = [value, flags](const StringPiece& arg) -> bool { |
| 66 | value->push_back((flags & Command::kPath) ? GetSafePath(arg) : arg.to_string()); |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 67 | return true; |
| 68 | }; |
| 69 | |
Ryan Mitchell | 2c8fc86 | 2018-12-13 16:56:07 -0800 | [diff] [blame^] | 70 | flags_.emplace_back(Flag(name, description, /* required */ true, /* num_args */ 1, func)); |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Ryan Mitchell | 2c8fc86 | 2018-12-13 16:56:07 -0800 | [diff] [blame^] | 73 | void Command::AddOptionalFlag(const StringPiece& name, const StringPiece& description, |
| 74 | Maybe<std::string>* value, uint32_t flags) { |
| 75 | auto func = [value, flags](const StringPiece& arg) -> bool { |
| 76 | *value = (flags & Command::kPath) ? GetSafePath(arg) : arg.to_string(); |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 77 | return true; |
| 78 | }; |
| 79 | |
Ryan Mitchell | 2c8fc86 | 2018-12-13 16:56:07 -0800 | [diff] [blame^] | 80 | flags_.emplace_back(Flag(name, description, /* required */ false, /* num_args */ 1, func)); |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Ryan Mitchell | 2c8fc86 | 2018-12-13 16:56:07 -0800 | [diff] [blame^] | 83 | void Command::AddOptionalFlagList(const StringPiece& name, const StringPiece& description, |
| 84 | std::vector<std::string>* value, uint32_t flags) { |
| 85 | auto func = [value, flags](const StringPiece& arg) -> bool { |
| 86 | value->push_back((flags & Command::kPath) ? GetSafePath(arg) : arg.to_string()); |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 87 | return true; |
| 88 | }; |
| 89 | |
Ryan Mitchell | 2c8fc86 | 2018-12-13 16:56:07 -0800 | [diff] [blame^] | 90 | flags_.emplace_back(Flag(name, description, /* required */ false, /* num_args */ 1, func)); |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Ryan Mitchell | 2c8fc86 | 2018-12-13 16:56:07 -0800 | [diff] [blame^] | 93 | void Command::AddOptionalFlagList(const StringPiece& name, const StringPiece& description, |
| 94 | std::unordered_set<std::string>* value) { |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 95 | auto func = [value](const StringPiece& arg) -> bool { |
| 96 | value->insert(arg.to_string()); |
| 97 | return true; |
| 98 | }; |
| 99 | |
Ryan Mitchell | 2c8fc86 | 2018-12-13 16:56:07 -0800 | [diff] [blame^] | 100 | flags_.emplace_back(Flag(name, description, /* required */ false, /* num_args */ 1, func)); |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Ryan Mitchell | 2c8fc86 | 2018-12-13 16:56:07 -0800 | [diff] [blame^] | 103 | void Command::AddOptionalSwitch(const StringPiece& name, const StringPiece& description, |
| 104 | bool* value) { |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 105 | auto func = [value](const StringPiece& arg) -> bool { |
| 106 | *value = true; |
| 107 | return true; |
| 108 | }; |
| 109 | |
Ryan Mitchell | 2c8fc86 | 2018-12-13 16:56:07 -0800 | [diff] [blame^] | 110 | flags_.emplace_back(Flag(name, description, /* required */ false, /* num_args */ 0, func)); |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 113 | void Command::AddOptionalSubcommand(std::unique_ptr<Command>&& subcommand, bool experimental) { |
Ryan Mitchell | 2c8fc86 | 2018-12-13 16:56:07 -0800 | [diff] [blame^] | 114 | subcommand->full_subcommand_name_ = StringPrintf("%s %s", name_.data(), subcommand->name_.data()); |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 115 | if (experimental) { |
| 116 | experimental_subcommands_.push_back(std::move(subcommand)); |
| 117 | } else { |
| 118 | subcommands_.push_back(std::move(subcommand)); |
| 119 | } |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Ryan Mitchell | 2c8fc86 | 2018-12-13 16:56:07 -0800 | [diff] [blame^] | 122 | void Command::SetDescription(const StringPiece& description) { |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 123 | description_ = description.to_string(); |
| 124 | } |
| 125 | |
| 126 | void Command::Usage(std::ostream* out) { |
| 127 | constexpr size_t kWidth = 50; |
| 128 | |
Ryan Mitchell | 2c8fc86 | 2018-12-13 16:56:07 -0800 | [diff] [blame^] | 129 | *out << full_subcommand_name_; |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 130 | |
| 131 | if (!subcommands_.empty()) { |
| 132 | *out << " [subcommand]"; |
| 133 | } |
| 134 | |
| 135 | *out << " [options]"; |
| 136 | for (const Flag& flag : flags_) { |
Ryan Mitchell | 2c8fc86 | 2018-12-13 16:56:07 -0800 | [diff] [blame^] | 137 | if (flag.is_required) { |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 138 | *out << " " << flag.name << " arg"; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | *out << " files...\n"; |
| 143 | |
| 144 | if (!subcommands_.empty()) { |
| 145 | *out << "\nSubcommands:\n"; |
| 146 | for (auto& subcommand : subcommands_) { |
| 147 | std::string argline = subcommand->name_; |
| 148 | |
| 149 | // Split the description by newlines and write out the argument (which is |
| 150 | // empty after the first line) followed by the description line. This will make sure |
| 151 | // that multiline descriptions are still right justified and aligned. |
| 152 | for (StringPiece line : util::Tokenize(subcommand->description_, '\n')) { |
| 153 | *out << " " << std::setw(kWidth) << std::left << argline << line << "\n"; |
| 154 | argline = " "; |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | *out << "\nOptions:\n"; |
| 160 | |
| 161 | for (const Flag& flag : flags_) { |
| 162 | std::string argline = flag.name; |
| 163 | if (flag.num_args > 0) { |
| 164 | argline += " arg"; |
| 165 | } |
| 166 | |
| 167 | // Split the description by newlines and write out the argument (which is |
| 168 | // empty after the first line) followed by the description line. This will make sure |
| 169 | // that multiline descriptions are still right justified and aligned. |
| 170 | for (StringPiece line : util::Tokenize(flag.description, '\n')) { |
| 171 | *out << " " << std::setw(kWidth) << std::left << argline << line << "\n"; |
| 172 | argline = " "; |
| 173 | } |
| 174 | } |
| 175 | *out << " " << std::setw(kWidth) << std::left << "-h" |
| 176 | << "Displays this help menu\n"; |
| 177 | out->flush(); |
| 178 | } |
| 179 | |
Ryan Mitchell | 2c8fc86 | 2018-12-13 16:56:07 -0800 | [diff] [blame^] | 180 | int Command::Execute(const std::vector<StringPiece>& args, std::ostream* out_error) { |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 181 | std::vector<std::string> file_args; |
| 182 | |
| 183 | for (size_t i = 0; i < args.size(); i++) { |
Ryan Mitchell | 2c8fc86 | 2018-12-13 16:56:07 -0800 | [diff] [blame^] | 184 | const StringPiece& arg = args[i]; |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 185 | if (*(arg.data()) != '-') { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 186 | // Continue parsing as the subcommand if the first argument matches one of the subcommands |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 187 | if (i == 0) { |
| 188 | for (auto& subcommand : subcommands_) { |
Ryan Mitchell | 2c8fc86 | 2018-12-13 16:56:07 -0800 | [diff] [blame^] | 189 | if (arg == subcommand->name_ || (!subcommand->short_name_.empty() |
| 190 | && arg == subcommand->short_name_)) { |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 191 | return subcommand->Execute( |
Ryan Mitchell | 2c8fc86 | 2018-12-13 16:56:07 -0800 | [diff] [blame^] | 192 | std::vector<StringPiece>(args.begin() + 1, args.end()), out_error); |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 193 | } |
| 194 | } |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 195 | for (auto& subcommand : experimental_subcommands_) { |
Ryan Mitchell | 2c8fc86 | 2018-12-13 16:56:07 -0800 | [diff] [blame^] | 196 | if (arg == subcommand->name_ || (!subcommand->short_name_.empty() |
| 197 | && arg == subcommand->short_name_)) { |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 198 | return subcommand->Execute( |
Ryan Mitchell | 2c8fc86 | 2018-12-13 16:56:07 -0800 | [diff] [blame^] | 199 | std::vector<StringPiece>(args.begin() + 1, args.end()), out_error); |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 200 | } |
| 201 | } |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Ryan Mitchell | 2c8fc86 | 2018-12-13 16:56:07 -0800 | [diff] [blame^] | 204 | file_args.push_back(GetSafePath(arg)); |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 205 | continue; |
| 206 | } |
| 207 | |
| 208 | if (arg == "-h" || arg == "--help") { |
| 209 | Usage(out_error); |
| 210 | return 1; |
| 211 | } |
| 212 | |
| 213 | bool match = false; |
| 214 | for (Flag& flag : flags_) { |
| 215 | if (arg == flag.name) { |
| 216 | if (flag.num_args > 0) { |
| 217 | i++; |
| 218 | if (i >= args.size()) { |
| 219 | *out_error << flag.name << " missing argument.\n\n"; |
| 220 | Usage(out_error); |
| 221 | return false; |
| 222 | } |
| 223 | flag.action(args[i]); |
| 224 | } else { |
| 225 | flag.action({}); |
| 226 | } |
Ryan Mitchell | 2c8fc86 | 2018-12-13 16:56:07 -0800 | [diff] [blame^] | 227 | flag.found = true; |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 228 | match = true; |
| 229 | break; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | if (!match) { |
| 234 | *out_error << "unknown option '" << arg << "'.\n\n"; |
| 235 | Usage(out_error); |
| 236 | return 1; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | for (const Flag& flag : flags_) { |
Ryan Mitchell | 2c8fc86 | 2018-12-13 16:56:07 -0800 | [diff] [blame^] | 241 | if (flag.is_required && !flag.found) { |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 242 | *out_error << "missing required flag " << flag.name << "\n\n"; |
| 243 | Usage(out_error); |
| 244 | return 1; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | return Action(file_args); |
| 249 | } |
| 250 | |
| 251 | } // namespace aapt |