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
17 #include "cmd/cmd_parser.h"
18 #include <cstdint>
19 #include <cstring>
20 #include <memory>
21 #include "cmd/dump_parser.h"
22 #include "cmd/package_parser.h"
23 #include "restool_errors.h"
24
25 namespace OHOS {
26 namespace Global {
27 namespace Restool {
CmdParserBase(const std::string & name)28 CmdParserBase::CmdParserBase(const std::string &name) : name_(name)
29 {}
30
Parse(int argc,char * argv[],int currentIndex)31 uint32_t CmdParserBase::Parse(int argc, char *argv[], int currentIndex)
32 {
33 if (currentIndex < argc) {
34 for (const auto &subcommand : subcommands_) {
35 if (subcommand->name_ == argv[currentIndex]) {
36 return subcommand->Parse(argc, argv, currentIndex + 1);
37 };
38 }
39 if (std::strcmp(argv[currentIndex], "-h") == 0) {
40 ShowUseage();
41 return RESTOOL_SUCCESS;
42 }
43 }
44 uint32_t errorCode = ParseOption(argc, argv, currentIndex);
45 if (errorCode != RESTOOL_SUCCESS) {
46 if (showUseage_) {
47 ShowUseage();
48 }
49 return errorCode;
50 }
51 return ExecCommand();
52 }
53
ParseOption(int argc,char * argv[],int currentIndex)54 uint32_t CmdParserBase::ParseOption(int argc, char *argv[], int currentIndex)
55 {
56 return RESTOOL_SUCCESS;
57 }
58
CmdParser()59 CmdParser::CmdParser() : CmdParserBase("")
60 {
61 subcommands_.emplace_back(std::make_unique<DumpParser>());
62 }
63
ParseOption(int argc,char * argv[],int currentIndex)64 uint32_t CmdParser::ParseOption(int argc, char *argv[], int currentIndex)
65 {
66 if (currentIndex >= argc) {
67 return RESTOOL_ERROR;
68 }
69 return packageParser_.Parse(argc, argv);
70 }
71
ExecCommand()72 uint32_t CmdParser::ExecCommand()
73 {
74 return packageParser_.ExecCommand();
75 }
76
GetPackageParser()77 PackageParser &CmdParser::GetPackageParser()
78 {
79 return packageParser_;
80 }
81
ShowUseage()82 void CmdParser::ShowUseage()
83 {
84 std::cout << "This is an OHOS Packaging Tool.\n";
85 std::cout << "Usage:\n";
86 std::cout << TOOL_NAME << " [subcommand] files...\n";
87 std::cout << TOOL_NAME << " [option1] [option2] [options] files...\n";
88 std::cout << "[subcommands]:\n";
89 std::cout << " dump Print the contents of the resource in the hap."
90 "For details about the usage of dump, see '-h'.\n";
91 std::cout << "\n";
92 std::cout << "[options]:\n";
93 std::cout << " -i/--inputPath Input resource path, can add multiple.\n";
94 std::cout << " -p/--packageName Package name.\n";
95 std::cout << " -o/--outputPath Output path.\n";
96 std::cout << " -r/--resHeader Resource header file path(like ./ResourceTable.js, ./ResrouceTable.h).\n";
97 std::cout << " -f/--forceWrite If output path exists,force delete it.\n";
98 std::cout << " -v/--version Print tool version.\n";
99 std::cout << " -m/--modules Module names, can add multiple, split by ','(like entry1,entry2,...).\n";
100 std::cout << " -j/--json Path of module.json(in Stage model) or onfig.json(in FA model).\n";
101 std::cout << " -e/--startId Start id mask, e.g 0x01000000,";
102 std::cout << " in the scope [0x01000000, 0x06FFFFFF),[0x08000000, 0xFFFFFFFF).\n";
103 std::cout << " -x/--append Resources folder path.\n";
104 std::cout << " -z/--combine Flag for incremental compilation.\n";
105 std::cout << " -h/--help Displays this help menu.\n";
106 std::cout << " -l/--fileList Input json file of the option set, e.g resConfig.json.";
107 std::cout << " For details, see the developer documentation.\n";
108 std::cout << " --ids Save id_defined.json direcory.\n";
109 std::cout << " --defined-ids Input id_defined.json path.\n";
110 std::cout << " --dependEntry Build result directory of the specified entry module when the feature";
111 std::cout << " module resources are independently built in the FA model.\n";
112 std::cout << " --icon-check Enable the PNG image verification function for icons and startwindows.\n";
113 std::cout << " --target-config When used with '-i', selective compilation is supported.\n";
114 std::cout << " --compressed-config Path of opt-compression.json.\n";
115 std::cout << " --thread Subthreads count.\n";
116 std::cout << " --ignored-file Regular patterns of ignored files, split by ':'(like \\.git:\\.svn).\n";
117 }
118 }
119 }
120 }