blob: cb1619657b4f9193d6e2ef248779808cc3ca81f4 [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"
18#include "util/StringPiece.h"
Adam Lesinski2ae4a872015-11-02 16:10:55 -080019#include "util/Util.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070020
21#include <iomanip>
22#include <iostream>
23#include <string>
24#include <vector>
25
26namespace aapt {
27
Adam Lesinskicacb28f2016-10-19 12:18:14 -070028Flags& Flags::requiredFlag(const StringPiece& name,
29 const StringPiece& description, std::string* value) {
30 auto func = [value](const StringPiece& arg) -> bool {
31 *value = arg.toString();
32 return true;
33 };
Adam Lesinski1ab598f2015-08-14 14:26:04 -070034
Adam Lesinskicacb28f2016-10-19 12:18:14 -070035 mFlags.push_back(
36 Flag{name.toString(), description.toString(), func, true, 1, false});
37 return *this;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070038}
39
Adam Lesinskicacb28f2016-10-19 12:18:14 -070040Flags& Flags::requiredFlagList(const StringPiece& name,
41 const StringPiece& description,
Adam Lesinski1ab598f2015-08-14 14:26:04 -070042 std::vector<std::string>* value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070043 auto func = [value](const StringPiece& arg) -> bool {
44 value->push_back(arg.toString());
45 return true;
46 };
Adam Lesinski1ab598f2015-08-14 14:26:04 -070047
Adam Lesinskicacb28f2016-10-19 12:18:14 -070048 mFlags.push_back(
49 Flag{name.toString(), description.toString(), func, true, 1, false});
50 return *this;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070051}
52
Adam Lesinskicacb28f2016-10-19 12:18:14 -070053Flags& Flags::optionalFlag(const StringPiece& name,
54 const StringPiece& description,
Adam Lesinski1ab598f2015-08-14 14:26:04 -070055 Maybe<std::string>* value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070056 auto func = [value](const StringPiece& arg) -> bool {
57 *value = arg.toString();
58 return true;
59 };
Adam Lesinski1ab598f2015-08-14 14:26:04 -070060
Adam Lesinskicacb28f2016-10-19 12:18:14 -070061 mFlags.push_back(
62 Flag{name.toString(), description.toString(), func, false, 1, false});
63 return *this;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070064}
65
Adam Lesinskicacb28f2016-10-19 12:18:14 -070066Flags& Flags::optionalFlagList(const StringPiece& name,
67 const StringPiece& description,
Adam Lesinski1ab598f2015-08-14 14:26:04 -070068 std::vector<std::string>* value) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070069 auto func = [value](const StringPiece& arg) -> bool {
70 value->push_back(arg.toString());
71 return true;
72 };
Adam Lesinski1ab598f2015-08-14 14:26:04 -070073
Adam Lesinskicacb28f2016-10-19 12:18:14 -070074 mFlags.push_back(
75 Flag{name.toString(), description.toString(), func, false, 1, false});
76 return *this;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070077}
78
Adam Lesinskicacb28f2016-10-19 12:18:14 -070079Flags& Flags::optionalFlagList(const StringPiece& name,
80 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 {
83 value->insert(arg.toString());
84 return true;
85 };
Adam Lesinski9756dec2016-08-08 12:35:04 -070086
Adam Lesinskicacb28f2016-10-19 12:18:14 -070087 mFlags.push_back(
88 Flag{name.toString(), description.toString(), func, false, 1, false});
89 return *this;
Adam Lesinski9756dec2016-08-08 12:35:04 -070090}
91
Adam Lesinskicacb28f2016-10-19 12:18:14 -070092Flags& Flags::optionalSwitch(const StringPiece& name,
93 const StringPiece& description, bool* value) {
94 auto func = [value](const StringPiece& arg) -> bool {
95 *value = true;
96 return true;
97 };
Adam Lesinski1ab598f2015-08-14 14:26:04 -070098
Adam Lesinskicacb28f2016-10-19 12:18:14 -070099 mFlags.push_back(
100 Flag{name.toString(), description.toString(), func, false, 0, false});
101 return *this;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700102}
103
104void Flags::usage(const StringPiece& command, std::ostream* out) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700105 constexpr size_t kWidth = 50;
Adam Lesinski52364f72016-01-11 13:10:24 -0800106
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700107 *out << command << " [options]";
108 for (const Flag& flag : mFlags) {
109 if (flag.required) {
110 *out << " " << flag.name << " arg";
111 }
112 }
113
114 *out << " files...\n\nOptions:\n";
115
116 for (const Flag& flag : mFlags) {
117 std::string argLine = flag.name;
118 if (flag.numArgs > 0) {
119 argLine += " arg";
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700120 }
121
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700122 // Split the description by newlines and write out the argument (which is
123 // empty after
124 // the first line) followed by the description line. This will make sure
125 // that multiline
126 // descriptions are still right justified and aligned.
127 for (StringPiece line : util::tokenize(flag.description, '\n')) {
128 *out << " " << std::setw(kWidth) << std::left << argLine << line << "\n";
129 argLine = " ";
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700130 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700131 }
132 *out << " " << std::setw(kWidth) << std::left << "-h"
133 << "Displays this help menu\n";
134 out->flush();
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700135}
136
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700137bool Flags::parse(const StringPiece& command,
138 const std::vector<StringPiece>& args,
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700139 std::ostream* outError) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700140 for (size_t i = 0; i < args.size(); i++) {
141 StringPiece arg = args[i];
142 if (*(arg.data()) != '-') {
143 mArgs.push_back(arg.toString());
144 continue;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700145 }
146
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700147 if (arg == "-h" || arg == "--help") {
148 usage(command, outError);
149 return false;
150 }
151
152 bool match = false;
153 for (Flag& flag : mFlags) {
154 if (arg == flag.name) {
155 if (flag.numArgs > 0) {
156 i++;
157 if (i >= args.size()) {
158 *outError << flag.name << " missing argument.\n\n";
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700159 usage(command, outError);
160 return false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700161 }
162 flag.action(args[i]);
163 } else {
164 flag.action({});
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700165 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700166 flag.parsed = true;
167 match = true;
168 break;
169 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700170 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700171
172 if (!match) {
173 *outError << "unknown option '" << arg << "'.\n\n";
174 usage(command, outError);
175 return false;
176 }
177 }
178
179 for (const Flag& flag : mFlags) {
180 if (flag.required && !flag.parsed) {
181 *outError << "missing required flag " << flag.name << "\n\n";
182 usage(command, outError);
183 return false;
184 }
185 }
186 return true;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700187}
188
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700189const std::vector<std::string>& Flags::getArgs() { return mArgs; }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700190
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700191} // namespace aapt