blob: 3731ac74f4eda1fc6f4aa6641f687bdd795ee4d5 [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
28Flags& Flags::requiredFlag(const StringPiece& name, const StringPiece& description,
29 std::string* value) {
30 auto func = [value](const StringPiece& arg) -> bool {
31 *value = arg.toString();
32 return true;
33 };
34
35 mFlags.push_back(Flag{ name.toString(), description.toString(), func, true, 1, false});
36 return *this;
37}
38
39Flags& Flags::requiredFlagList(const StringPiece& name, const StringPiece& description,
40 std::vector<std::string>* value) {
41 auto func = [value](const StringPiece& arg) -> bool {
42 value->push_back(arg.toString());
43 return true;
44 };
45
46 mFlags.push_back(Flag{ name.toString(), description.toString(), func, true, 1, false });
47 return *this;
48}
49
50Flags& Flags::optionalFlag(const StringPiece& name, const StringPiece& description,
51 Maybe<std::string>* value) {
52 auto func = [value](const StringPiece& arg) -> bool {
53 *value = arg.toString();
54 return true;
55 };
56
57 mFlags.push_back(Flag{ name.toString(), description.toString(), func, false, 1, false });
58 return *this;
59}
60
61Flags& Flags::optionalFlagList(const StringPiece& name, const StringPiece& description,
62 std::vector<std::string>* value) {
63 auto func = [value](const StringPiece& arg) -> bool {
64 value->push_back(arg.toString());
65 return true;
66 };
67
68 mFlags.push_back(Flag{ name.toString(), description.toString(), func, false, 1, false });
69 return *this;
70}
71
Adam Lesinski9756dec2016-08-08 12:35:04 -070072Flags& Flags::optionalFlagList(const StringPiece& name, const StringPiece& description,
73 std::unordered_set<std::string>* value) {
74 auto func = [value](const StringPiece& arg) -> bool {
75 value->insert(arg.toString());
76 return true;
77 };
78
79 mFlags.push_back(Flag{ name.toString(), description.toString(), func, false, 1, false });
80 return *this;
81}
82
Adam Lesinski1ab598f2015-08-14 14:26:04 -070083Flags& Flags::optionalSwitch(const StringPiece& name, const StringPiece& description,
84 bool* value) {
85 auto func = [value](const StringPiece& arg) -> bool {
86 *value = true;
87 return true;
88 };
89
90 mFlags.push_back(Flag{ name.toString(), description.toString(), func, false, 0, false });
91 return *this;
92}
93
94void Flags::usage(const StringPiece& command, std::ostream* out) {
Adam Lesinski52364f72016-01-11 13:10:24 -080095 constexpr size_t kWidth = 50;
96
Adam Lesinski1ab598f2015-08-14 14:26:04 -070097 *out << command << " [options]";
98 for (const Flag& flag : mFlags) {
99 if (flag.required) {
100 *out << " " << flag.name << " arg";
101 }
102 }
103
104 *out << " files...\n\nOptions:\n";
105
106 for (const Flag& flag : mFlags) {
107 std::string argLine = flag.name;
108 if (flag.numArgs > 0) {
109 argLine += " arg";
110 }
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800111
112 // Split the description by newlines and write out the argument (which is empty after
113 // the first line) followed by the description line. This will make sure that multiline
114 // descriptions are still right justified and aligned.
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700115 for (StringPiece line : util::tokenize(flag.description, '\n')) {
Adam Lesinski52364f72016-01-11 13:10:24 -0800116 *out << " " << std::setw(kWidth) << std::left << argLine << line << "\n";
Adam Lesinski2ae4a872015-11-02 16:10:55 -0800117 argLine = " ";
118 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700119 }
Adam Lesinski52364f72016-01-11 13:10:24 -0800120 *out << " " << std::setw(kWidth) << std::left << "-h" << "Displays this help menu\n";
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700121 out->flush();
122}
123
124bool Flags::parse(const StringPiece& command, const std::vector<StringPiece>& args,
125 std::ostream* outError) {
126 for (size_t i = 0; i < args.size(); i++) {
127 StringPiece arg = args[i];
128 if (*(arg.data()) != '-') {
129 mArgs.push_back(arg.toString());
130 continue;
131 }
132
133 if (arg == "-h" || arg == "--help") {
134 usage(command, outError);
135 return false;
136 }
137
138 bool match = false;
139 for (Flag& flag : mFlags) {
140 if (arg == flag.name) {
141 if (flag.numArgs > 0) {
142 i++;
143 if (i >= args.size()) {
144 *outError << flag.name << " missing argument.\n\n";
145 usage(command, outError);
146 return false;
147 }
148 flag.action(args[i]);
149 } else {
150 flag.action({});
151 }
152 flag.parsed = true;
153 match = true;
154 break;
155 }
156 }
157
158 if (!match) {
159 *outError << "unknown option '" << arg << "'.\n\n";
160 usage(command, outError);
161 return false;
162 }
163 }
164
165 for (const Flag& flag : mFlags) {
166 if (flag.required && !flag.parsed) {
167 *outError << "missing required flag " << flag.name << "\n\n";
168 usage(command, outError);
169 return false;
170 }
171 }
172 return true;
173}
174
175const std::vector<std::string>& Flags::getArgs() {
176 return mArgs;
177}
178
179} // namespace aapt