blob: 919b4c98fa8f6645e7eaa5e26723b6dc5c9446e3 [file] [log] [blame]
Ryan Mitchell833a1a62018-07-10 13:51:36 -07001/*
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 Mitchell2c8fc862018-12-13 16:56:07 -080024#include "android-base/stringprintf.h"
25#include "android-base/utf8.h"
Ryan Mitchell833a1a62018-07-10 13:51:36 -070026#include "androidfw/StringPiece.h"
27
Fabien Sanglard2d34e762019-02-21 15:13:29 -080028#include "trace/TraceBuffer.h"
Ryan Mitchell833a1a62018-07-10 13:51:36 -070029#include "util/Util.h"
30
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080031using android::base::StringPrintf;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070032using android::StringPiece;
33
34namespace aapt {
35
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080036std::string GetSafePath(const StringPiece& arg) {
37#ifdef _WIN32
38 // If the path exceeds the maximum path length for Windows, encode the path using the
39 // extended-length prefix
40 std::wstring path16;
41 CHECK(android::base::UTF8PathToWindowsLongPath(arg.data(), &path16))
42 << "Failed to convert file path to UTF-16: file path " << arg.data();
43
44 std::string path8;
45 CHECK(android::base::WideToUTF8(path16, &path8))
46 << "Failed to convert file path back to UTF-8: file path " << arg.data();
47
48 return path8;
49#else
50 return arg.to_string();
51#endif
52}
53
54void Command::AddRequiredFlag(const StringPiece& name, const StringPiece& description,
55 std::string* value, uint32_t flags) {
56 auto func = [value, flags](const StringPiece& arg) -> bool {
57 *value = (flags & Command::kPath) ? GetSafePath(arg) : arg.to_string();
Ryan Mitchell833a1a62018-07-10 13:51:36 -070058 return true;
59 };
60
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080061 flags_.emplace_back(Flag(name, description, /* required */ true, /* num_args */ 1, func));
Ryan Mitchell833a1a62018-07-10 13:51:36 -070062}
63
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080064void Command::AddRequiredFlagList(const StringPiece& name, const StringPiece& description,
65 std::vector<std::string>* value, uint32_t flags) {
66 auto func = [value, flags](const StringPiece& arg) -> bool {
67 value->push_back((flags & Command::kPath) ? GetSafePath(arg) : arg.to_string());
Ryan Mitchell833a1a62018-07-10 13:51:36 -070068 return true;
69 };
70
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080071 flags_.emplace_back(Flag(name, description, /* required */ true, /* num_args */ 1, func));
Ryan Mitchell833a1a62018-07-10 13:51:36 -070072}
73
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080074void Command::AddOptionalFlag(const StringPiece& name, const StringPiece& description,
75 Maybe<std::string>* value, uint32_t flags) {
76 auto func = [value, flags](const StringPiece& arg) -> bool {
77 *value = (flags & Command::kPath) ? GetSafePath(arg) : arg.to_string();
Ryan Mitchell833a1a62018-07-10 13:51:36 -070078 return true;
79 };
80
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080081 flags_.emplace_back(Flag(name, description, /* required */ false, /* num_args */ 1, func));
Ryan Mitchell833a1a62018-07-10 13:51:36 -070082}
83
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080084void Command::AddOptionalFlagList(const StringPiece& name, const StringPiece& description,
85 std::vector<std::string>* value, uint32_t flags) {
86 auto func = [value, flags](const StringPiece& arg) -> bool {
87 value->push_back((flags & Command::kPath) ? GetSafePath(arg) : arg.to_string());
Ryan Mitchell833a1a62018-07-10 13:51:36 -070088 return true;
89 };
90
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080091 flags_.emplace_back(Flag(name, description, /* required */ false, /* num_args */ 1, func));
Ryan Mitchell833a1a62018-07-10 13:51:36 -070092}
93
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080094void Command::AddOptionalFlagList(const StringPiece& name, const StringPiece& description,
95 std::unordered_set<std::string>* value) {
Ryan Mitchell833a1a62018-07-10 13:51:36 -070096 auto func = [value](const StringPiece& arg) -> bool {
97 value->insert(arg.to_string());
98 return true;
99 };
100
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800101 flags_.emplace_back(Flag(name, description, /* required */ false, /* num_args */ 1, func));
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700102}
103
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800104void Command::AddOptionalSwitch(const StringPiece& name, const StringPiece& description,
105 bool* value) {
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700106 auto func = [value](const StringPiece& arg) -> bool {
107 *value = true;
108 return true;
109 };
110
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800111 flags_.emplace_back(Flag(name, description, /* required */ false, /* num_args */ 0, func));
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700112}
113
Ryan Mitchell214846d2018-09-19 16:57:01 -0700114void Command::AddOptionalSubcommand(std::unique_ptr<Command>&& subcommand, bool experimental) {
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800115 subcommand->full_subcommand_name_ = StringPrintf("%s %s", name_.data(), subcommand->name_.data());
Ryan Mitchell214846d2018-09-19 16:57:01 -0700116 if (experimental) {
117 experimental_subcommands_.push_back(std::move(subcommand));
118 } else {
119 subcommands_.push_back(std::move(subcommand));
120 }
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700121}
122
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800123void Command::SetDescription(const StringPiece& description) {
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700124 description_ = description.to_string();
125}
126
127void Command::Usage(std::ostream* out) {
128 constexpr size_t kWidth = 50;
129
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800130 *out << full_subcommand_name_;
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700131
132 if (!subcommands_.empty()) {
133 *out << " [subcommand]";
134 }
135
136 *out << " [options]";
137 for (const Flag& flag : flags_) {
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800138 if (flag.is_required) {
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700139 *out << " " << flag.name << " arg";
140 }
141 }
142
143 *out << " files...\n";
144
145 if (!subcommands_.empty()) {
146 *out << "\nSubcommands:\n";
147 for (auto& subcommand : subcommands_) {
148 std::string argline = subcommand->name_;
149
150 // Split the description by newlines and write out the argument (which is
151 // empty after the first line) followed by the description line. This will make sure
152 // that multiline descriptions are still right justified and aligned.
153 for (StringPiece line : util::Tokenize(subcommand->description_, '\n')) {
154 *out << " " << std::setw(kWidth) << std::left << argline << line << "\n";
155 argline = " ";
156 }
157 }
158 }
159
160 *out << "\nOptions:\n";
161
162 for (const Flag& flag : flags_) {
163 std::string argline = flag.name;
164 if (flag.num_args > 0) {
165 argline += " arg";
166 }
167
168 // Split the description by newlines and write out the argument (which is
169 // empty after the first line) followed by the description line. This will make sure
170 // that multiline descriptions are still right justified and aligned.
171 for (StringPiece line : util::Tokenize(flag.description, '\n')) {
172 *out << " " << std::setw(kWidth) << std::left << argline << line << "\n";
173 argline = " ";
174 }
175 }
176 *out << " " << std::setw(kWidth) << std::left << "-h"
177 << "Displays this help menu\n";
178 out->flush();
179}
180
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800181int Command::Execute(const std::vector<StringPiece>& args, std::ostream* out_error) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800182 TRACE_NAME_ARGS("Command::Execute", args);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700183 std::vector<std::string> file_args;
184
185 for (size_t i = 0; i < args.size(); i++) {
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800186 const StringPiece& arg = args[i];
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700187 if (*(arg.data()) != '-') {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700188 // Continue parsing as the subcommand if the first argument matches one of the subcommands
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700189 if (i == 0) {
190 for (auto& subcommand : subcommands_) {
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800191 if (arg == subcommand->name_ || (!subcommand->short_name_.empty()
192 && arg == subcommand->short_name_)) {
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700193 return subcommand->Execute(
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800194 std::vector<StringPiece>(args.begin() + 1, args.end()), out_error);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700195 }
196 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700197 for (auto& subcommand : experimental_subcommands_) {
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800198 if (arg == subcommand->name_ || (!subcommand->short_name_.empty()
199 && arg == subcommand->short_name_)) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700200 return subcommand->Execute(
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800201 std::vector<StringPiece>(args.begin() + 1, args.end()), out_error);
Ryan Mitchell214846d2018-09-19 16:57:01 -0700202 }
203 }
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700204 }
205
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800206 file_args.push_back(GetSafePath(arg));
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700207 continue;
208 }
209
210 if (arg == "-h" || arg == "--help") {
211 Usage(out_error);
212 return 1;
213 }
214
215 bool match = false;
216 for (Flag& flag : flags_) {
217 if (arg == flag.name) {
218 if (flag.num_args > 0) {
219 i++;
220 if (i >= args.size()) {
221 *out_error << flag.name << " missing argument.\n\n";
222 Usage(out_error);
223 return false;
224 }
225 flag.action(args[i]);
226 } else {
227 flag.action({});
228 }
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800229 flag.found = true;
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700230 match = true;
231 break;
232 }
233 }
234
235 if (!match) {
236 *out_error << "unknown option '" << arg << "'.\n\n";
237 Usage(out_error);
238 return 1;
239 }
240 }
241
242 for (const Flag& flag : flags_) {
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800243 if (flag.is_required && !flag.found) {
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700244 *out_error << "missing required flag " << flag.name << "\n\n";
245 Usage(out_error);
246 return 1;
247 }
248 }
249
250 return Action(file_args);
251}
252
253} // namespace aapt