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 | |
| 28 | Flags& 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 | |
| 39 | Flags& 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 | |
| 50 | Flags& 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 | |
| 61 | Flags& 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 Lesinski | 9756dec | 2016-08-08 12:35:04 -0700 | [diff] [blame^] | 72 | Flags& 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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 83 | Flags& 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 | |
| 94 | void Flags::usage(const StringPiece& command, std::ostream* out) { |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 95 | constexpr size_t kWidth = 50; |
| 96 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 97 | *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 Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 111 | |
| 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 Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 115 | for (StringPiece line : util::tokenize(flag.description, '\n')) { |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 116 | *out << " " << std::setw(kWidth) << std::left << argLine << line << "\n"; |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 117 | argLine = " "; |
| 118 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 119 | } |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 120 | *out << " " << std::setw(kWidth) << std::left << "-h" << "Displays this help menu\n"; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 121 | out->flush(); |
| 122 | } |
| 123 | |
| 124 | bool 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 | |
| 175 | const std::vector<std::string>& Flags::getArgs() { |
| 176 | return mArgs; |
| 177 | } |
| 178 | |
| 179 | } // namespace aapt |