blob: 2311f33adf67b59096f1c042398a6065f3027add [file] [log] [blame]
Yabin Cui67d3abd2015-04-16 15:26:31 -07001/*
2 * Copyright (C) 2015 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 SIMPLE_PERF_COMMAND_H_
18#define SIMPLE_PERF_COMMAND_H_
19
Yabin Cuif79f07e2015-06-01 11:21:37 -070020#include <functional>
21#include <memory>
Yabin Cui67d3abd2015-04-16 15:26:31 -070022#include <string>
23#include <vector>
24
Elliott Hughes66dd09e2015-12-04 14:00:57 -080025#include <android-base/macros.h>
Yabin Cui67d3abd2015-04-16 15:26:31 -070026
27class Command {
28 public:
29 Command(const std::string& name, const std::string& short_help_string,
30 const std::string& long_help_string)
31 : name_(name), short_help_string_(short_help_string), long_help_string_(long_help_string) {
Yabin Cui67d3abd2015-04-16 15:26:31 -070032 }
33
34 virtual ~Command() {
35 }
36
37 const std::string& Name() const {
38 return name_;
39 }
40
41 const std::string& ShortHelpString() const {
42 return short_help_string_;
43 }
44
45 const std::string LongHelpString() const {
46 return long_help_string_;
47 }
48
49 virtual bool Run(const std::vector<std::string>& args) = 0;
50
Yabin Cuif79f07e2015-06-01 11:21:37 -070051 protected:
52 bool NextArgumentOrError(const std::vector<std::string>& args, size_t* pi);
53 void ReportUnknownOption(const std::vector<std::string>& args, size_t i);
Yabin Cui67d3abd2015-04-16 15:26:31 -070054
55 private:
56 const std::string name_;
57 const std::string short_help_string_;
58 const std::string long_help_string_;
59
Yabin Cui67d3abd2015-04-16 15:26:31 -070060 DISALLOW_COPY_AND_ASSIGN(Command);
61};
62
Yabin Cuif79f07e2015-06-01 11:21:37 -070063void RegisterCommand(const std::string& cmd_name,
Yabin Cui3e4c5952016-07-26 15:03:27 -070064 const std::function<std::unique_ptr<Command>(void)>& callback);
Yabin Cuif79f07e2015-06-01 11:21:37 -070065void UnRegisterCommand(const std::string& cmd_name);
66std::unique_ptr<Command> CreateCommandInstance(const std::string& cmd_name);
67const std::vector<std::string> GetAllCommandNames();
68
Yabin Cui67d3abd2015-04-16 15:26:31 -070069#endif // SIMPLE_PERF_COMMAND_H_