blob: 84977ab424cc559bc157860b8adff02eaccd424f [file] [log] [blame]
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001/*
2 * Copyright (C) 2015 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 "Flags.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070018
19#include <iomanip>
20#include <iostream>
21#include <string>
22#include <vector>
23
Adam Lesinskid5083f62017-01-16 15:07:21 -080024#include "androidfw/StringPiece.h"
25
Adam Lesinskice5e56e2016-10-21 17:56:45 -070026#include "util/Util.h"
27
Adam Lesinskid5083f62017-01-16 15:07:21 -080028using android::StringPiece;
29
Adam Lesinski1ab598f2015-08-14 14:26:04 -070030namespace aapt {
31
Adam Lesinskice5e56e2016-10-21 17:56:45 -070032Flags& Flags::RequiredFlag(const StringPiece& name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070033 const StringPiece& description, std::string* value) {
34 auto func = [value](const StringPiece& arg) -> bool {
Adam Lesinskid5083f62017-01-16 15:07:21 -080035 *value = arg.to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070036 return true;
37 };
Adam Lesinski1ab598f2015-08-14 14:26:04 -070038
Adam Lesinskid5083f62017-01-16 15:07:21 -080039 flags_.push_back(Flag{name.to_string(), description.to_string(), func, true, 1, false});
Adam Lesinskicacb28f2016-10-19 12:18:14 -070040 return *this;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070041}
42
Adam Lesinskice5e56e2016-10-21 17:56:45 -070043Flags& Flags::RequiredFlagList(const StringPiece& name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070044 const StringPiece& description,
Adam Lesinski1ab598f2015-08-14 14:26:04 -070045 std::vector<std::string>* value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070046 auto func = [value](const StringPiece& arg) -> bool {
Adam Lesinskid5083f62017-01-16 15:07:21 -080047 value->push_back(arg.to_string());
Adam Lesinskicacb28f2016-10-19 12:18:14 -070048 return true;
49 };
Adam Lesinski1ab598f2015-08-14 14:26:04 -070050
Adam Lesinskid5083f62017-01-16 15:07:21 -080051 flags_.push_back(Flag{name.to_string(), description.to_string(), func, true, 1, false});
Adam Lesinskicacb28f2016-10-19 12:18:14 -070052 return *this;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070053}
54
Adam Lesinskice5e56e2016-10-21 17:56:45 -070055Flags& Flags::OptionalFlag(const StringPiece& name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070056 const StringPiece& description,
Adam Lesinski1ab598f2015-08-14 14:26:04 -070057 Maybe<std::string>* value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070058 auto func = [value](const StringPiece& arg) -> bool {
Adam Lesinskid5083f62017-01-16 15:07:21 -080059 *value = arg.to_string();
Adam Lesinskicacb28f2016-10-19 12:18:14 -070060 return true;
61 };
Adam Lesinski1ab598f2015-08-14 14:26:04 -070062
Adam Lesinskid5083f62017-01-16 15:07:21 -080063 flags_.push_back(Flag{name.to_string(), description.to_string(), func, false, 1, false});
Adam Lesinskicacb28f2016-10-19 12:18:14 -070064 return *this;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070065}
66
Adam Lesinskice5e56e2016-10-21 17:56:45 -070067Flags& Flags::OptionalFlagList(const StringPiece& name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070068 const StringPiece& description,
Adam Lesinski1ab598f2015-08-14 14:26:04 -070069 std::vector<std::string>* value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070 auto func = [value](const StringPiece& arg) -> bool {
Adam Lesinskid5083f62017-01-16 15:07:21 -080071 value->push_back(arg.to_string());
Adam Lesinskicacb28f2016-10-19 12:18:14 -070072 return true;
73 };
Adam Lesinski1ab598f2015-08-14 14:26:04 -070074
Adam Lesinskid5083f62017-01-16 15:07:21 -080075 flags_.push_back(Flag{name.to_string(), description.to_string(), func, false, 1, false});
Adam Lesinskicacb28f2016-10-19 12:18:14 -070076 return *this;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070077}
78
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079Flags& Flags::OptionalFlagList(const StringPiece& name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080 const StringPiece& description,
Adam Lesinski9756dec2016-08-08 12:35:04 -070081 std::unordered_set<std::string>* value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082 auto func = [value](const StringPiece& arg) -> bool {
Adam Lesinskid5083f62017-01-16 15:07:21 -080083 value->insert(arg.to_string());
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084 return true;
85 };
Adam Lesinski9756dec2016-08-08 12:35:04 -070086
Adam Lesinskid5083f62017-01-16 15:07:21 -080087 flags_.push_back(Flag{name.to_string(), description.to_string(), func, false, 1, false});
Adam Lesinskicacb28f2016-10-19 12:18:14 -070088 return *this;
Adam Lesinski9756dec2016-08-08 12:35:04 -070089}
90
Adam Lesinskice5e56e2016-10-21 17:56:45 -070091Flags& Flags::OptionalSwitch(const StringPiece& name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070092 const StringPiece& description, bool* value) {
93 auto func = [value](const StringPiece& arg) -> bool {
94 *value = true;
95 return true;
96 };
Adam Lesinski1ab598f2015-08-14 14:26:04 -070097
Adam Lesinskid5083f62017-01-16 15:07:21 -080098 flags_.push_back(Flag{name.to_string(), description.to_string(), func, false, 0, false});
Adam Lesinskicacb28f2016-10-19 12:18:14 -070099 return *this;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700100}
101
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700102void Flags::Usage(const StringPiece& command, std::ostream* out) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700103 constexpr size_t kWidth = 50;
Adam Lesinski52364f72016-01-11 13:10:24 -0800104
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700105 *out << command << " [options]";
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700106 for (const Flag& flag : flags_) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700107 if (flag.required) {
108 *out << " " << flag.name << " arg";
109 }
110 }
111
112 *out << " files...\n\nOptions:\n";
113
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700114 for (const Flag& flag : flags_) {
115 std::string argline = flag.name;
116 if (flag.num_args > 0) {
117 argline += " arg";
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700118 }
119
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700120 // Split the description by newlines and write out the argument (which is
121 // empty after
122 // the first line) followed by the description line. This will make sure
123 // that multiline
124 // descriptions are still right justified and aligned.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700125 for (StringPiece line : util::Tokenize(flag.description, '\n')) {
126 *out << " " << std::setw(kWidth) << std::left << argline << line << "\n";
127 argline = " ";
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700128 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700129 }
130 *out << " " << std::setw(kWidth) << std::left << "-h"
131 << "Displays this help menu\n";
132 out->flush();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700133}
134
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700135bool Flags::Parse(const StringPiece& command,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700136 const std::vector<StringPiece>& args,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700137 std::ostream* out_error) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700138 for (size_t i = 0; i < args.size(); i++) {
139 StringPiece arg = args[i];
140 if (*(arg.data()) != '-') {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800141 args_.push_back(arg.to_string());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700142 continue;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700143 }
144
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700145 if (arg == "-h" || arg == "--help") {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700146 Usage(command, out_error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700147 return false;
148 }
149
150 bool match = false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700151 for (Flag& flag : flags_) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700152 if (arg == flag.name) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700153 if (flag.num_args > 0) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700154 i++;
155 if (i >= args.size()) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700156 *out_error << flag.name << " missing argument.\n\n";
157 Usage(command, out_error);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700158 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700159 }
160 flag.action(args[i]);
161 } else {
162 flag.action({});
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700163 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700164 flag.parsed = true;
165 match = true;
166 break;
167 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700168 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700169
170 if (!match) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700171 *out_error << "unknown option '" << arg << "'.\n\n";
172 Usage(command, out_error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700173 return false;
174 }
175 }
176
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700177 for (const Flag& flag : flags_) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700178 if (flag.required && !flag.parsed) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700179 *out_error << "missing required flag " << flag.name << "\n\n";
180 Usage(command, out_error);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700181 return false;
182 }
183 }
184 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700185}
186
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700187const std::vector<std::string>& Flags::GetArgs() { return args_; }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700188
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700189} // namespace aapt