Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1 | /* |
| 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 Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 19 | #include "util/Util.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 20 | |
| 21 | #include <iomanip> |
| 22 | #include <iostream> |
| 23 | #include <string> |
| 24 | #include <vector> |
| 25 | |
| 26 | namespace aapt { |
| 27 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 28 | Flags& 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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 34 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 35 | mFlags.push_back( |
| 36 | Flag{name.toString(), description.toString(), func, true, 1, false}); |
| 37 | return *this; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 40 | Flags& Flags::requiredFlagList(const StringPiece& name, |
| 41 | const StringPiece& description, |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 42 | std::vector<std::string>* value) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 43 | auto func = [value](const StringPiece& arg) -> bool { |
| 44 | value->push_back(arg.toString()); |
| 45 | return true; |
| 46 | }; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 47 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 48 | mFlags.push_back( |
| 49 | Flag{name.toString(), description.toString(), func, true, 1, false}); |
| 50 | return *this; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 53 | Flags& Flags::optionalFlag(const StringPiece& name, |
| 54 | const StringPiece& description, |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 55 | Maybe<std::string>* value) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 56 | auto func = [value](const StringPiece& arg) -> bool { |
| 57 | *value = arg.toString(); |
| 58 | return true; |
| 59 | }; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 60 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 61 | mFlags.push_back( |
| 62 | Flag{name.toString(), description.toString(), func, false, 1, false}); |
| 63 | return *this; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 66 | Flags& Flags::optionalFlagList(const StringPiece& name, |
| 67 | const StringPiece& description, |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 68 | std::vector<std::string>* value) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 69 | auto func = [value](const StringPiece& arg) -> bool { |
| 70 | value->push_back(arg.toString()); |
| 71 | return true; |
| 72 | }; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 73 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 74 | mFlags.push_back( |
| 75 | Flag{name.toString(), description.toString(), func, false, 1, false}); |
| 76 | return *this; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 79 | Flags& Flags::optionalFlagList(const StringPiece& name, |
| 80 | const StringPiece& description, |
Adam Lesinski | 9756dec | 2016-08-08 12:35:04 -0700 | [diff] [blame] | 81 | std::unordered_set<std::string>* value) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 82 | auto func = [value](const StringPiece& arg) -> bool { |
| 83 | value->insert(arg.toString()); |
| 84 | return true; |
| 85 | }; |
Adam Lesinski | 9756dec | 2016-08-08 12:35:04 -0700 | [diff] [blame] | 86 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 87 | mFlags.push_back( |
| 88 | Flag{name.toString(), description.toString(), func, false, 1, false}); |
| 89 | return *this; |
Adam Lesinski | 9756dec | 2016-08-08 12:35:04 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 92 | Flags& 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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 98 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 99 | mFlags.push_back( |
| 100 | Flag{name.toString(), description.toString(), func, false, 0, false}); |
| 101 | return *this; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | void Flags::usage(const StringPiece& command, std::ostream* out) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 105 | constexpr size_t kWidth = 50; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 106 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 107 | *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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 122 | // 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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 130 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 131 | } |
| 132 | *out << " " << std::setw(kWidth) << std::left << "-h" |
| 133 | << "Displays this help menu\n"; |
| 134 | out->flush(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 137 | bool Flags::parse(const StringPiece& command, |
| 138 | const std::vector<StringPiece>& args, |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 139 | std::ostream* outError) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 140 | 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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 147 | 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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 159 | usage(command, outError); |
| 160 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 161 | } |
| 162 | flag.action(args[i]); |
| 163 | } else { |
| 164 | flag.action({}); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 165 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 166 | flag.parsed = true; |
| 167 | match = true; |
| 168 | break; |
| 169 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 170 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 171 | |
| 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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 189 | const std::vector<std::string>& Flags::getArgs() { return mArgs; } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 190 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 191 | } // namespace aapt |