• 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  
18  #include <cstdlib>
19  #include <string>
20  #include "utilities.h"
21  
22  namespace OHOS {
23  namespace Developtools {
24  namespace HiPerf {
25  class SubCommand {
26  public:
SubCommand(const std::string & name,const std::string & brief,const std::string & help)27      SubCommand(const std::string &name, const std::string &brief, const std::string &help)
28          : name_(name), brief_(brief), help_(help)
29      {
30      }
31  
~SubCommand()32      virtual ~SubCommand() {}
33  
Name()34      const std::string &Name() const
35      {
36          return name_;
37      }
Brief()38      const std::string &Brief() const
39      {
40          return brief_;
41      }
Help()42      const std::string &Help() const
43      {
44          return help_;
45      }
46  
47      // parse option first
48      bool OnSubCommandOptions(std::vector<std::string> args);
49  
50      // some help cmd
OnPreSubCommand()51      bool OnPreSubCommand()
52      {
53          if (showHelp_) {
54              printf("%s\n", Help().c_str());
55              return true;
56          }
57          return false;
58      };
59  
DumpOptions()60      virtual void DumpOptions() const {};
61  
62      // args should be empty after all the args processed
ParseOption(std::vector<std::string> & args)63      virtual bool ParseOption(std::vector<std::string> &args)
64      {
65          args.clear(); // all the args is processed
66          return true;
67      }
68  
69      // return false means cmd failed
70      virtual bool OnSubCommand(std::vector<std::string> &args) = 0;
71      // some test code will use this for simple
OnSubCommand(std::string stringArgs)72      bool OnSubCommand(std::string stringArgs)
73      {
74          auto args = StringSplit(stringArgs, " ");
75          return OnSubCommand(args);
76      };
77  
78      // called from main
79      static bool RegisterSubCommand(std::string, std::unique_ptr<SubCommand>);
80  
81      // get some cmd
82      static const std::map<std::string, std::unique_ptr<SubCommand>> &GetSubCommands();
83      static SubCommand *FindSubCommand(std::string);
84  
85      // for test code
86      static void ClearSubCommands();
87  
88  protected:
89      const std::string name_;
90      const std::string brief_;
91      std::string help_;
92      bool dumpOptions_ = false;
93      bool showHelp_ = false;
94  };
95  } // namespace HiPerf
96  } // namespace Developtools
97  } // namespace OHOS
98  #endif