1 /* 2 * Copyright (c) 2024-2024 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 16 #ifndef OHOS_RESTOOL_CMD_PARSER_H 17 #define OHOS_RESTOOL_CMD_PARSER_H 18 19 #include <cstdint> 20 #include <memory> 21 #include <iostream> 22 #include <string> 23 #include <type_traits> 24 #include <vector> 25 #include "cmd/package_parser.h" 26 #include "singleton.h" 27 #include "restool_errors.h" 28 29 namespace OHOS { 30 namespace Global { 31 namespace Restool { 32 33 class CmdParserBase { 34 public: 35 CmdParserBase(const std::string &name); 36 virtual ~CmdParserBase() = default; 37 virtual uint32_t Parse(int argc, char *argv[], int currentIndex); 38 virtual uint32_t ParseOption(int argc, char *argv[], int currentIndex); 39 virtual uint32_t ExecCommand() = 0; 40 virtual void ShowUseage() = 0; 41 protected: 42 std::vector<std::unique_ptr<CmdParserBase>> subcommands_; 43 const std::string name_; 44 bool showUseage_{true}; 45 }; 46 47 class CmdParser : public CmdParserBase, public Singleton<CmdParser> { 48 public: 49 CmdParser(); 50 uint32_t ParseOption(int argc, char *argv[], int currentIndex) override; 51 uint32_t ExecCommand() override; 52 void ShowUseage() override; 53 PackageParser &GetPackageParser(); 54 55 private: 56 PackageParser packageParser_; 57 }; 58 } 59 } 60 } 61 #endif