blob: 1416e980ed192f6f474dde18ac3f3c908f208ad2 [file] [log] [blame]
Ryan Mitchell833a1a62018-07-10 13:51:36 -07001/*
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 Mitchell4382e442021-07-14 12:53:01 -070021#include <optional>
Ryan Mitchell833a1a62018-07-10 13:51:36 -070022#include <ostream>
23#include <string>
24#include <unordered_set>
25#include <vector>
26
27#include "androidfw/StringPiece.h"
28
Ryan Mitchell833a1a62018-07-10 13:51:36 -070029namespace aapt {
30
31class Command {
32 public:
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070033 explicit Command(android::StringPiece name) : name_(name), full_subcommand_name_(name){};
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080034
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070035 explicit Command(android::StringPiece name, android::StringPiece short_name)
36 : name_(name), short_name_(short_name), full_subcommand_name_(name){};
Ryan Mitchell4382e442021-07-14 12:53:01 -070037
38 Command(Command&&) = default;
39 Command& operator=(Command&&) = default;
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080040
Ryan Mitchell833a1a62018-07-10 13:51:36 -070041 virtual ~Command() = default;
42
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080043 // Behavior flags used with the following functions that change how the command flags are parsed
44 // displayed.
45 enum : uint32_t {
46 // Indicates the arguments are file or folder paths. On Windows, paths that exceed the maximum
47 // path length will be converted to use the extended path prefix '//?/'. Without this
48 // conversion, files with long paths cannot be opened.
49 kPath = 1 << 0,
50 };
51
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070052 void AddRequiredFlag(android::StringPiece name, android::StringPiece description,
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080053 std::string* value, uint32_t flags = 0);
54
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070055 void AddRequiredFlagList(android::StringPiece name, android::StringPiece description,
56 std::vector<std::string>* value, uint32_t flags = 0);
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080057
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070058 void AddOptionalFlag(android::StringPiece name, android::StringPiece description,
Ryan Mitchell4382e442021-07-14 12:53:01 -070059 std::optional<std::string>* value, uint32_t flags = 0);
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080060
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070061 void AddOptionalFlagList(android::StringPiece name, android::StringPiece description,
62 std::vector<std::string>* value, uint32_t flags = 0);
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080063
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070064 void AddOptionalFlagList(android::StringPiece name, android::StringPiece description,
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080065 std::unordered_set<std::string>* value);
66
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070067 void AddOptionalSwitch(android::StringPiece name, android::StringPiece description, bool* value);
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080068
Ryan Mitchell214846d2018-09-19 16:57:01 -070069 void AddOptionalSubcommand(std::unique_ptr<Command>&& subcommand, bool experimental = false);
Ryan Mitchell833a1a62018-07-10 13:51:36 -070070
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070071 void SetDescription(android::StringPiece name);
Ryan Mitchell833a1a62018-07-10 13:51:36 -070072
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080073 // Prints the help menu of the command.
Ryan Mitchell833a1a62018-07-10 13:51:36 -070074 void Usage(std::ostream* out);
75
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080076 // Parses the command line arguments, sets the flag variable values, and runs the action of
77 // the command. If the arguments fail to parse to the command and its subcommands, then the action
78 // will not be run and the usage will be printed instead.
Ryan Mitchell833a1a62018-07-10 13:51:36 -070079 int Execute(const std::vector<android::StringPiece>& args, std::ostream* outError);
80
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080081 // The action to preform when the command is executed.
Ryan Mitchell833a1a62018-07-10 13:51:36 -070082 virtual int Action(const std::vector<std::string>& args) = 0;
83
84 private:
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080085 struct Flag {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070086 explicit Flag(android::StringPiece name, android::StringPiece description,
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080087 const bool is_required, const size_t num_args,
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070088 std::function<bool(android::StringPiece value)>&& action)
89 : name(name),
90 description(description),
91 is_required(is_required),
92 num_args(num_args),
93 action(std::move(action)) {
94 }
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080095
96 const std::string name;
97 const std::string description;
98 const bool is_required;
99 const size_t num_args;
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700100 const std::function<bool(android::StringPiece value)> action;
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800101 bool found = false;
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700102 };
103
Ryan Mitchell4382e442021-07-14 12:53:01 -0700104 std::string name_;
105 std::string short_name_;
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800106 std::string description_ = "";
107 std::string full_subcommand_name_;
108
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700109 std::vector<Flag> flags_;
110 std::vector<std::unique_ptr<Command>> subcommands_;
Ryan Mitchell214846d2018-09-19 16:57:01 -0700111 std::vector<std::unique_ptr<Command>> experimental_subcommands_;
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700112};
113
114} // namespace aapt
115
116#endif // AAPT_COMMAND_H