Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | #ifndef AAPT_COMMAND_H |
| 18 | #define AAPT_COMMAND_H |
| 19 | |
| 20 | #include <functional> |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 21 | #include <optional> |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 22 | #include <ostream> |
| 23 | #include <string> |
| 24 | #include <unordered_set> |
| 25 | #include <vector> |
| 26 | |
| 27 | #include "androidfw/StringPiece.h" |
| 28 | |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 29 | namespace aapt { |
| 30 | |
| 31 | class Command { |
| 32 | public: |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 33 | explicit Command(const android::StringPiece& name) |
| 34 | : name_(name.to_string()), full_subcommand_name_(name.to_string()){}; |
Ryan Mitchell | 2c8fc86 | 2018-12-13 16:56:07 -0800 | [diff] [blame] | 35 | |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 36 | explicit Command(const android::StringPiece& name, const android::StringPiece& short_name) |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 37 | : name_(name.to_string()), |
| 38 | short_name_(short_name.to_string()), |
| 39 | full_subcommand_name_(name.to_string()){}; |
| 40 | |
| 41 | Command(Command&&) = default; |
| 42 | Command& operator=(Command&&) = default; |
Ryan Mitchell | 2c8fc86 | 2018-12-13 16:56:07 -0800 | [diff] [blame] | 43 | |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 44 | virtual ~Command() = default; |
| 45 | |
Ryan Mitchell | 2c8fc86 | 2018-12-13 16:56:07 -0800 | [diff] [blame] | 46 | // Behavior flags used with the following functions that change how the command flags are parsed |
| 47 | // displayed. |
| 48 | enum : uint32_t { |
| 49 | // Indicates the arguments are file or folder paths. On Windows, paths that exceed the maximum |
| 50 | // path length will be converted to use the extended path prefix '//?/'. Without this |
| 51 | // conversion, files with long paths cannot be opened. |
| 52 | kPath = 1 << 0, |
| 53 | }; |
| 54 | |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 55 | void AddRequiredFlag(const android::StringPiece& name, const android::StringPiece& description, |
Ryan Mitchell | 2c8fc86 | 2018-12-13 16:56:07 -0800 | [diff] [blame] | 56 | std::string* value, uint32_t flags = 0); |
| 57 | |
| 58 | void AddRequiredFlagList(const android::StringPiece& name, |
| 59 | const android::StringPiece& description, std::vector<std::string>* value, |
| 60 | uint32_t flags = 0); |
| 61 | |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 62 | void AddOptionalFlag(const android::StringPiece& name, const android::StringPiece& description, |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 63 | std::optional<std::string>* value, uint32_t flags = 0); |
Ryan Mitchell | 2c8fc86 | 2018-12-13 16:56:07 -0800 | [diff] [blame] | 64 | |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 65 | void AddOptionalFlagList(const android::StringPiece& name, |
Ryan Mitchell | 2c8fc86 | 2018-12-13 16:56:07 -0800 | [diff] [blame] | 66 | const android::StringPiece& description, std::vector<std::string>* value, |
| 67 | uint32_t flags = 0); |
| 68 | |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 69 | void AddOptionalFlagList(const android::StringPiece& name, |
Ryan Mitchell | 2c8fc86 | 2018-12-13 16:56:07 -0800 | [diff] [blame] | 70 | const android::StringPiece& description, |
| 71 | std::unordered_set<std::string>* value); |
| 72 | |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 73 | void AddOptionalSwitch(const android::StringPiece& name, const android::StringPiece& description, |
Ryan Mitchell | 2c8fc86 | 2018-12-13 16:56:07 -0800 | [diff] [blame] | 74 | bool* value); |
| 75 | |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 76 | void AddOptionalSubcommand(std::unique_ptr<Command>&& subcommand, bool experimental = false); |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 77 | |
| 78 | void SetDescription(const android::StringPiece& name); |
| 79 | |
Ryan Mitchell | 2c8fc86 | 2018-12-13 16:56:07 -0800 | [diff] [blame] | 80 | // Prints the help menu of the command. |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 81 | void Usage(std::ostream* out); |
| 82 | |
Ryan Mitchell | 2c8fc86 | 2018-12-13 16:56:07 -0800 | [diff] [blame] | 83 | // Parses the command line arguments, sets the flag variable values, and runs the action of |
| 84 | // the command. If the arguments fail to parse to the command and its subcommands, then the action |
| 85 | // will not be run and the usage will be printed instead. |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 86 | int Execute(const std::vector<android::StringPiece>& args, std::ostream* outError); |
| 87 | |
Ryan Mitchell | 2c8fc86 | 2018-12-13 16:56:07 -0800 | [diff] [blame] | 88 | // The action to preform when the command is executed. |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 89 | virtual int Action(const std::vector<std::string>& args) = 0; |
| 90 | |
| 91 | private: |
Ryan Mitchell | 2c8fc86 | 2018-12-13 16:56:07 -0800 | [diff] [blame] | 92 | struct Flag { |
| 93 | explicit Flag(const android::StringPiece& name, const android::StringPiece& description, |
| 94 | const bool is_required, const size_t num_args, |
| 95 | std::function<bool(const android::StringPiece& value)>&& action) |
| 96 | : name(name.to_string()), description(description.to_string()), is_required(is_required), |
| 97 | num_args(num_args), action(std::move(action)) {} |
| 98 | |
| 99 | const std::string name; |
| 100 | const std::string description; |
| 101 | const bool is_required; |
| 102 | const size_t num_args; |
| 103 | const std::function<bool(const android::StringPiece& value)> action; |
| 104 | bool found = false; |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 105 | }; |
| 106 | |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 107 | std::string name_; |
| 108 | std::string short_name_; |
Ryan Mitchell | 2c8fc86 | 2018-12-13 16:56:07 -0800 | [diff] [blame] | 109 | std::string description_ = ""; |
| 110 | std::string full_subcommand_name_; |
| 111 | |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 112 | std::vector<Flag> flags_; |
| 113 | std::vector<std::unique_ptr<Command>> subcommands_; |
Ryan Mitchell | 214846d | 2018-09-19 16:57:01 -0700 | [diff] [blame] | 114 | std::vector<std::unique_ptr<Command>> experimental_subcommands_; |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | } // namespace aapt |
| 118 | |
| 119 | #endif // AAPT_COMMAND_H |