blob: 9435396991df441c46056409783a68be5d5ba45f [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
72Flags& Flags::optionalSwitch(const StringPiece& name, const StringPiece& description,
73 bool* value) {
74 auto func = [value](const StringPiece& arg) -> bool {
75 *value = true;
76 return true;
77 };
78
79 mFlags.push_back(Flag{ name.toString(), description.toString(), func, false, 0, false });
80 return *this;
81}
82
83void Flags::usage(const StringPiece& command, std::ostream* out) {
84 *out << command << " [options]";
85 for (const Flag& flag : mFlags) {
86 if (flag.required) {
87 *out << " " << flag.name << " arg";
88 }
89 }
90
91 *out << " files...\n\nOptions:\n";
92
93 for (const Flag& flag : mFlags) {
94 std::string argLine = flag.name;
95 if (flag.numArgs > 0) {
96 argLine += " arg";
97 }
Adam Lesinski2ae4a872015-11-02 16:10:55 -080098
99 // Split the description by newlines and write out the argument (which is empty after
100 // the first line) followed by the description line. This will make sure that multiline
101 // descriptions are still right justified and aligned.
102 for (StringPiece line : util::tokenize<char>(flag.description, '\n')) {
103 *out << " " << std::setw(30) << std::left << argLine << line << "\n";
104 argLine = " ";
105 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700106 }
107 *out << " " << std::setw(30) << std::left << "-h" << "Displays this help menu\n";
108 out->flush();
109}
110
111bool Flags::parse(const StringPiece& command, const std::vector<StringPiece>& args,
112 std::ostream* outError) {
113 for (size_t i = 0; i < args.size(); i++) {
114 StringPiece arg = args[i];
115 if (*(arg.data()) != '-') {
116 mArgs.push_back(arg.toString());
117 continue;
118 }
119
120 if (arg == "-h" || arg == "--help") {
121 usage(command, outError);
122 return false;
123 }
124
125 bool match = false;
126 for (Flag& flag : mFlags) {
127 if (arg == flag.name) {
128 if (flag.numArgs > 0) {
129 i++;
130 if (i >= args.size()) {
131 *outError << flag.name << " missing argument.\n\n";
132 usage(command, outError);
133 return false;
134 }
135 flag.action(args[i]);
136 } else {
137 flag.action({});
138 }
139 flag.parsed = true;
140 match = true;
141 break;
142 }
143 }
144
145 if (!match) {
146 *outError << "unknown option '" << arg << "'.\n\n";
147 usage(command, outError);
148 return false;
149 }
150 }
151
152 for (const Flag& flag : mFlags) {
153 if (flag.required && !flag.parsed) {
154 *outError << "missing required flag " << flag.name << "\n\n";
155 usage(command, outError);
156 return false;
157 }
158 }
159 return true;
160}
161
162const std::vector<std::string>& Flags::getArgs() {
163 return mArgs;
164}
165
166} // namespace aapt