blob: 4424a357c1dd2b59031756e4445dcfa2226c58a1 [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
28#include "util/Util.h"
29
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080030using android::base::StringPrintf;
Ryan Mitchell833a1a62018-07-10 13:51:36 -070031using android::StringPiece;
32
33namespace aapt {
34
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080035std::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
53void 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 Mitchell833a1a62018-07-10 13:51:36 -070057 return true;
58 };
59
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080060 flags_.emplace_back(Flag(name, description, /* required */ true, /* num_args */ 1, func));
Ryan Mitchell833a1a62018-07-10 13:51:36 -070061}
62
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080063void 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 Mitchell833a1a62018-07-10 13:51:36 -070067 return true;
68 };
69
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080070 flags_.emplace_back(Flag(name, description, /* required */ true, /* num_args */ 1, func));
Ryan Mitchell833a1a62018-07-10 13:51:36 -070071}
72
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080073void 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 Mitchell833a1a62018-07-10 13:51:36 -070077 return true;
78 };
79
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080080 flags_.emplace_back(Flag(name, description, /* required */ false, /* num_args */ 1, func));
Ryan Mitchell833a1a62018-07-10 13:51:36 -070081}
82
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080083void 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 Mitchell833a1a62018-07-10 13:51:36 -070087 return true;
88 };
89
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080090 flags_.emplace_back(Flag(name, description, /* required */ false, /* num_args */ 1, func));
Ryan Mitchell833a1a62018-07-10 13:51:36 -070091}
92
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080093void Command::AddOptionalFlagList(const StringPiece& name, const StringPiece& description,
94 std::unordered_set<std::string>* value) {
Ryan Mitchell833a1a62018-07-10 13:51:36 -070095 auto func = [value](const StringPiece& arg) -> bool {
96 value->insert(arg.to_string());
97 return true;
98 };
99
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800100 flags_.emplace_back(Flag(name, description, /* required */ false, /* num_args */ 1, func));
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700101}
102
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800103void Command::AddOptionalSwitch(const StringPiece& name, const StringPiece& description,
104 bool* value) {
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700105 auto func = [value](const StringPiece& arg) -> bool {
106 *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
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800122void Command::SetDescription(const StringPiece& description) {
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700123 description_ = description.to_string();
124}
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) {
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700181 std::vector<std::string> file_args;
182
183 for (size_t i = 0; i < args.size(); i++) {
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800184 const StringPiece& arg = args[i];
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700185 if (*(arg.data()) != '-') {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700186 // Continue parsing as the subcommand if the first argument matches one of the subcommands
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700187 if (i == 0) {
188 for (auto& subcommand : subcommands_) {
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800189 if (arg == subcommand->name_ || (!subcommand->short_name_.empty()
190 && arg == subcommand->short_name_)) {
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700191 return subcommand->Execute(
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800192 std::vector<StringPiece>(args.begin() + 1, args.end()), out_error);
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700193 }
194 }
Ryan Mitchell214846d2018-09-19 16:57:01 -0700195 for (auto& subcommand : experimental_subcommands_) {
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800196 if (arg == subcommand->name_ || (!subcommand->short_name_.empty()
197 && arg == subcommand->short_name_)) {
Ryan Mitchell214846d2018-09-19 16:57:01 -0700198 return subcommand->Execute(
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800199 std::vector<StringPiece>(args.begin() + 1, args.end()), out_error);
Ryan Mitchell214846d2018-09-19 16:57:01 -0700200 }
201 }
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700202 }
203
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800204 file_args.push_back(GetSafePath(arg));
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700205 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 Mitchell2c8fc862018-12-13 16:56:07 -0800227 flag.found = true;
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700228 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 Mitchell2c8fc862018-12-13 16:56:07 -0800241 if (flag.is_required && !flag.found) {
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700242 *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