blob: 71dc6fe48da12f5991a1d3e9e22e43b7f1ff86fe [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()),
35 fullname_(name.to_string()) { }
36 explicit Command(const android::StringPiece& name, const android::StringPiece& short_name)
37 : name_(name.to_string()), short_name_(short_name.to_string()), fullname_(name.to_string()) {}
38 virtual ~Command() = default;
39
40 void AddRequiredFlag(const android::StringPiece& name, const android::StringPiece& description,
41 std::string* value);
42 void AddRequiredFlagList(const android::StringPiece& name, const android::StringPiece&
43 description, std::vector<std::string>* value);
44 void AddOptionalFlag(const android::StringPiece& name, const android::StringPiece& description,
45 Maybe<std::string>* value);
46 void AddOptionalFlagList(const android::StringPiece& name,
47 const android::StringPiece& description, std::vector<std::string>* value);
48 void AddOptionalFlagList(const android::StringPiece& name,
49 const android::StringPiece& description, std::unordered_set<std::string>* value);
50 void AddOptionalSwitch(const android::StringPiece& name, const android::StringPiece& description,
51 bool* value);
52 void AddOptionalSubcommand(std::unique_ptr<Command>&& subcommand);
53
54 void SetDescription(const android::StringPiece& name);
55
56 /** Prints the help menu of the command. */
57 void Usage(std::ostream* out);
58
59 /**
60 * Parses the command line arguments, sets the flag variable values, and runs the action of
61 * the command. If the arguments fail to parse to the command and its subcommands, then the action
62 * will not be run and the usage will be printed instead.
63 **/
64 int Execute(const std::vector<android::StringPiece>& args, std::ostream* outError);
65
66 /** The action to preform when the command is executed. */
67 virtual int Action(const std::vector<std::string>& args) = 0;
68
69 private:
70 struct Flag {
71 std::string name;
72 std::string description;
73 std::function<bool(const android::StringPiece& value)> action;
74 bool required;
75 size_t num_args;
76
77 bool parsed;
78 };
79
80 std::string description_;
81 std::string name_;
82 std::string short_name_;
83 std::string fullname_;
84 std::vector<Flag> flags_;
85 std::vector<std::unique_ptr<Command>> subcommands_;
86};
87
88} // namespace aapt
89
90#endif // AAPT_COMMAND_H