Debian Codegraph @

tools/aapt2/cmd/Command.h

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