blob: d21571d530d2697a1cc41b1cd1756cdd3fb73de8 [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>
21#include <ostream>
22#include <string>
23#include <unordered_set>
24#include <vector>
25
26#include "androidfw/StringPiece.h"
27
28#include "util/Maybe.h"
29
30namespace aapt {
31
32class Command {
33 public:
34 explicit Command(const android::StringPiece& name) : name_(name.to_string()),
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080035 short_name_(""),
36 full_subcommand_name_(name.to_string()) {}
37
Ryan Mitchell833a1a62018-07-10 13:51:36 -070038 explicit Command(const android::StringPiece& name, const android::StringPiece& short_name)
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080039 : name_(name.to_string()), short_name_(short_name.to_string()),
40 full_subcommand_name_(name.to_string()) {}
41
Ryan Mitchell833a1a62018-07-10 13:51:36 -070042 virtual ~Command() = default;
43
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080044 // Behavior flags used with the following functions that change how the command flags are parsed
45 // displayed.
46 enum : uint32_t {
47 // Indicates the arguments are file or folder paths. On Windows, paths that exceed the maximum
48 // path length will be converted to use the extended path prefix '//?/'. Without this
49 // conversion, files with long paths cannot be opened.
50 kPath = 1 << 0,
51 };
52
Ryan Mitchell833a1a62018-07-10 13:51:36 -070053 void AddRequiredFlag(const android::StringPiece& name, const android::StringPiece& description,
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080054 std::string* value, uint32_t flags = 0);
55
56 void AddRequiredFlagList(const android::StringPiece& name,
57 const android::StringPiece& description, std::vector<std::string>* value,
58 uint32_t flags = 0);
59
Ryan Mitchell833a1a62018-07-10 13:51:36 -070060 void AddOptionalFlag(const android::StringPiece& name, const android::StringPiece& description,
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080061 Maybe<std::string>* value, uint32_t flags = 0);
62
Ryan Mitchell833a1a62018-07-10 13:51:36 -070063 void AddOptionalFlagList(const android::StringPiece& name,
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080064 const android::StringPiece& description, std::vector<std::string>* value,
65 uint32_t flags = 0);
66
Ryan Mitchell833a1a62018-07-10 13:51:36 -070067 void AddOptionalFlagList(const android::StringPiece& name,
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080068 const android::StringPiece& description,
69 std::unordered_set<std::string>* value);
70
Ryan Mitchell833a1a62018-07-10 13:51:36 -070071 void AddOptionalSwitch(const android::StringPiece& name, const android::StringPiece& description,
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080072 bool* value);
73
Ryan Mitchell214846d2018-09-19 16:57:01 -070074 void AddOptionalSubcommand(std::unique_ptr<Command>&& subcommand, bool experimental = false);
Ryan Mitchell833a1a62018-07-10 13:51:36 -070075
76 void SetDescription(const android::StringPiece& name);
77
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080078 // Prints the help menu of the command.
Ryan Mitchell833a1a62018-07-10 13:51:36 -070079 void Usage(std::ostream* out);
80
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080081 // Parses the command line arguments, sets the flag variable values, and runs the action of
82 // the command. If the arguments fail to parse to the command and its subcommands, then the action
83 // will not be run and the usage will be printed instead.
Ryan Mitchell833a1a62018-07-10 13:51:36 -070084 int Execute(const std::vector<android::StringPiece>& args, std::ostream* outError);
85
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080086 // The action to preform when the command is executed.
Ryan Mitchell833a1a62018-07-10 13:51:36 -070087 virtual int Action(const std::vector<std::string>& args) = 0;
88
89 private:
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080090 DISALLOW_COPY_AND_ASSIGN(Command);
Ryan Mitchell833a1a62018-07-10 13:51:36 -070091
Ryan Mitchell2c8fc862018-12-13 16:56:07 -080092 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 Mitchell833a1a62018-07-10 13:51:36 -0700105 };
106
Ryan Mitchell2c8fc862018-12-13 16:56:07 -0800107 const std::string name_;
108 const std::string short_name_;
109 std::string description_ = "";
110 std::string full_subcommand_name_;
111
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700112 std::vector<Flag> flags_;
113 std::vector<std::unique_ptr<Command>> subcommands_;
Ryan Mitchell214846d2018-09-19 16:57:01 -0700114 std::vector<std::unique_ptr<Command>> experimental_subcommands_;
Ryan Mitchell833a1a62018-07-10 13:51:36 -0700115};
116
117} // namespace aapt
118
119#endif // AAPT_COMMAND_H