• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef HIPERF_SUBCOMMAND_H_
16 #define HIPERF_SUBCOMMAND_H_
17 #include <string>
18 #include "utilities.h"
19 
20 namespace OHOS {
21 namespace Developtools {
22 namespace HiPerf {
23 class SubCommand {
24 public:
SubCommand(const std::string & name,const std::string & brief,const std::string & help)25     SubCommand(const std::string &name, const std::string &brief, const std::string &help)
26         : name_(name), brief_(brief), help_(help)
27     {
28     }
29 
~SubCommand()30     virtual ~SubCommand() {}
31 
Name()32     const std::string &Name() const
33     {
34         return name_;
35     }
Brief()36     const std::string &Brief() const
37     {
38         return brief_;
39     }
Help()40     const std::string &Help() const
41     {
42         return help_;
43     }
44 
45     // parse option first
46     bool OnSubCommandOptions(std::vector<std::string> args);
47 
48     // some help cmd
OnPreSubCommand()49     bool OnPreSubCommand()
50     {
51         if (showHelp_) {
52             printf("%s\n", Help().c_str());
53             return true;
54         }
55         return false;
56     };
57 
DumpOptions()58     virtual void DumpOptions() const {}
59 
60     // args should be empty after all the args processed
ParseOption(std::vector<std::string> & args)61     virtual bool ParseOption(std::vector<std::string> &args)
62     {
63         args.clear(); // all the args is processed
64         return true;
65     }
66 
67     // return false means cmd failed
68     virtual bool OnSubCommand(std::vector<std::string> &args) = 0;
69     // some test code will use this for simple
OnSubCommand(std::string stringArgs)70     bool OnSubCommand(std::string stringArgs)
71     {
72         auto args = StringSplit(stringArgs, " ");
73         return OnSubCommand(args);
74     };
75 
76     // called from main
77     static bool RegisterSubCommand(std::string, std::unique_ptr<SubCommand>);
78 
79     // get some cmd
80     static const std::map<std::string, std::unique_ptr<SubCommand>> &GetSubCommands();
81     static SubCommand *FindSubCommand(std::string);
82 
83     // for test code
84     static void ClearSubCommands();
85 
86 protected:
87     const std::string name_;
88     const std::string brief_;
89     std::string help_;
90     bool dumpOptions_ = false;
91     bool showHelp_ = false;
92 };
93 } // namespace HiPerf
94 } // namespace Developtools
95 } // namespace OHOS
96 #endif // HIPERF_SUBCOMMAND_H_
97