blob: 6ae5af7bb92a5d0e2391e7ec1af05f55925816f4 [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"
19
20#include <iomanip>
21#include <iostream>
22#include <string>
23#include <vector>
24
25namespace aapt {
26
27Flags& Flags::requiredFlag(const StringPiece& name, const StringPiece& description,
28 std::string* value) {
29 auto func = [value](const StringPiece& arg) -> bool {
30 *value = arg.toString();
31 return true;
32 };
33
34 mFlags.push_back(Flag{ name.toString(), description.toString(), func, true, 1, false});
35 return *this;
36}
37
38Flags& Flags::requiredFlagList(const StringPiece& name, const StringPiece& description,
39 std::vector<std::string>* value) {
40 auto func = [value](const StringPiece& arg) -> bool {
41 value->push_back(arg.toString());
42 return true;
43 };
44
45 mFlags.push_back(Flag{ name.toString(), description.toString(), func, true, 1, false });
46 return *this;
47}
48
49Flags& Flags::optionalFlag(const StringPiece& name, const StringPiece& description,
50 Maybe<std::string>* value) {
51 auto func = [value](const StringPiece& arg) -> bool {
52 *value = arg.toString();
53 return true;
54 };
55
56 mFlags.push_back(Flag{ name.toString(), description.toString(), func, false, 1, false });
57 return *this;
58}
59
60Flags& Flags::optionalFlagList(const StringPiece& name, const StringPiece& description,
61 std::vector<std::string>* value) {
62 auto func = [value](const StringPiece& arg) -> bool {
63 value->push_back(arg.toString());
64 return true;
65 };
66
67 mFlags.push_back(Flag{ name.toString(), description.toString(), func, false, 1, false });
68 return *this;
69}
70
71Flags& Flags::optionalSwitch(const StringPiece& name, const StringPiece& description,
72 bool* value) {
73 auto func = [value](const StringPiece& arg) -> bool {
74 *value = true;
75 return true;
76 };
77
78 mFlags.push_back(Flag{ name.toString(), description.toString(), func, false, 0, false });
79 return *this;
80}
81
82void Flags::usage(const StringPiece& command, std::ostream* out) {
83 *out << command << " [options]";
84 for (const Flag& flag : mFlags) {
85 if (flag.required) {
86 *out << " " << flag.name << " arg";
87 }
88 }
89
90 *out << " files...\n\nOptions:\n";
91
92 for (const Flag& flag : mFlags) {
93 std::string argLine = flag.name;
94 if (flag.numArgs > 0) {
95 argLine += " arg";
96 }
97 *out << " " << std::setw(30) << std::left << argLine << flag.description << "\n";
98 }
99 *out << " " << std::setw(30) << std::left << "-h" << "Displays this help menu\n";
100 out->flush();
101}
102
103bool Flags::parse(const StringPiece& command, const std::vector<StringPiece>& args,
104 std::ostream* outError) {
105 for (size_t i = 0; i < args.size(); i++) {
106 StringPiece arg = args[i];
107 if (*(arg.data()) != '-') {
108 mArgs.push_back(arg.toString());
109 continue;
110 }
111
112 if (arg == "-h" || arg == "--help") {
113 usage(command, outError);
114 return false;
115 }
116
117 bool match = false;
118 for (Flag& flag : mFlags) {
119 if (arg == flag.name) {
120 if (flag.numArgs > 0) {
121 i++;
122 if (i >= args.size()) {
123 *outError << flag.name << " missing argument.\n\n";
124 usage(command, outError);
125 return false;
126 }
127 flag.action(args[i]);
128 } else {
129 flag.action({});
130 }
131 flag.parsed = true;
132 match = true;
133 break;
134 }
135 }
136
137 if (!match) {
138 *outError << "unknown option '" << arg << "'.\n\n";
139 usage(command, outError);
140 return false;
141 }
142 }
143
144 for (const Flag& flag : mFlags) {
145 if (flag.required && !flag.parsed) {
146 *outError << "missing required flag " << flag.name << "\n\n";
147 usage(command, outError);
148 return false;
149 }
150 }
151 return true;
152}
153
154const std::vector<std::string>& Flags::getArgs() {
155 return mArgs;
156}
157
158} // namespace aapt