blob: 514651e92c275f0fc4591c7b50de91764554fcd7 [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
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070036std::string GetSafePath(StringPiece arg) {
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080037#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
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070050 return std::string(arg);
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080051#endif
52}
53
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070054void Command::AddRequiredFlag(StringPiece name, StringPiece description, std::string* value,
55 uint32_t flags) {
56 auto func = [value, flags](StringPiece arg) -> bool {
57 *value = (flags & Command::kPath) ? GetSafePath(arg) : std::string(arg);
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
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070064void Command::AddRequiredFlagList(StringPiece name, StringPiece description,
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080065 std::vector<std::string>* value, uint32_t flags) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070066 auto func = [value, flags](StringPiece arg) -> bool {
67 value->push_back((flags & Command::kPath) ? GetSafePath(arg) : std::string(arg));
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
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070074void Command::AddOptionalFlag(StringPiece name, StringPiece description,
Ryan Mitchell4382e442021-07-14 12:53:01 -070075 std::optional<std::string>* value, uint32_t flags) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070076 auto func = [value, flags](StringPiece arg) -> bool {
77 *value = (flags & Command::kPath) ? GetSafePath(arg) : std::string(arg);
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
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070084void Command::AddOptionalFlagList(StringPiece name, StringPiece description,
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080085 std::vector<std::string>* value, uint32_t flags) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070086 auto func = [value, flags](StringPiece arg) -> bool {
87 value->push_back((flags & Command::kPath) ? GetSafePath(arg) : std::string(arg));
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
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070094void Command::AddOptionalFlagList(StringPiece name, StringPiece description,
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080095 std::unordered_set<std::string>* value) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070096 auto func = [value](StringPiece arg) -> bool {
97 value->emplace(arg);
Ryan Mitchell833a1a62018-07-10 13:51:36 -070098 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
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700104void Command::AddOptionalSwitch(StringPiece name, StringPiece description, bool* value) {
105 auto func = [value](StringPiece arg) -> bool {
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700106 *value = true;
107 return true;
108 };
109
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800110 flags_.emplace_back(Flag(name, description, /* required */ false, /* num_args */ 0, func));
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700111}
112
Ryan Mitchell214846d2018-09-19 16:57:01 -0700113void Command::AddOptionalSubcommand(std::unique_ptr<Command>&& subcommand, bool experimental) {
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800114 subcommand->full_subcommand_name_ = StringPrintf("%s %s", name_.data(), subcommand->name_.data());
Ryan Mitchell214846d2018-09-19 16:57:01 -0700115 if (experimental) {
116 experimental_subcommands_.push_back(std::move(subcommand));
117 } else {
118 subcommands_.push_back(std::move(subcommand));
119 }
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700120}
121
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700122void Command::SetDescription(StringPiece description) {
123 description_ = std::string(description);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700124}
125
126void Command::Usage(std::ostream* out) {
127 constexpr size_t kWidth = 50;
128
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800129 *out << full_subcommand_name_;
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700130
131 if (!subcommands_.empty()) {
132 *out << " [subcommand]";
133 }
134
135 *out << " [options]";
136 for (const Flag& flag : flags_) {
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800137 if (flag.is_required) {
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700138 *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 Mitchell2c8fc862018-12-13 16:56:07 -0800180int Command::Execute(const std::vector<StringPiece>& args, std::ostream* out_error) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800181 TRACE_NAME_ARGS("Command::Execute", args);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700182 std::vector<std::string> file_args;
183
184 for (size_t i = 0; i < args.size(); i++) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700185 StringPiece arg = args[i];
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700186 if (*(arg.data()) != '-') {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700187 // Continue parsing as the subcommand if the first argument matches one of the subcommands
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700188 if (i == 0) {
189 for (auto& subcommand : subcommands_) {
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800190 if (arg == subcommand->name_ || (!subcommand->short_name_.empty()
191 && arg == subcommand->short_name_)) {
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700192 return subcommand->Execute(
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800193 std::vector<StringPiece>(args.begin() + 1, args.end()), out_error);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700194 }
195 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700196 for (auto& subcommand : experimental_subcommands_) {
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800197 if (arg == subcommand->name_ || (!subcommand->short_name_.empty()
198 && arg == subcommand->short_name_)) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700199 return subcommand->Execute(
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800200 std::vector<StringPiece>(args.begin() + 1, args.end()), out_error);
Ryan Mitchell214846d2018-09-19 16:57:01 -0700201 }
202 }
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700203 }
204
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800205 file_args.push_back(GetSafePath(arg));
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700206 continue;
207 }
208
209 if (arg == "-h" || arg == "--help") {
210 Usage(out_error);
211 return 1;
212 }
213
214 bool match = false;
215 for (Flag& flag : flags_) {
216 if (arg == flag.name) {
217 if (flag.num_args > 0) {
218 i++;
219 if (i >= args.size()) {
220 *out_error << flag.name << " missing argument.\n\n";
221 Usage(out_error);
222 return false;
223 }
224 flag.action(args[i]);
225 } else {
226 flag.action({});
227 }
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800228 flag.found = true;
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700229 match = true;
230 break;
231 }
232 }
233
234 if (!match) {
235 *out_error << "unknown option '" << arg << "'.\n\n";
236 Usage(out_error);
237 return 1;
238 }
239 }
240
241 for (const Flag& flag : flags_) {
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800242 if (flag.is_required && !flag.found) {
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700243 *out_error << "missing required flag " << flag.name << "\n\n";
244 Usage(out_error);
245 return 1;
246 }
247 }
248
249 return Action(file_args);
250}
251
252} // namespace aapt