1 /*
2  * Copyright (c) 2022-2023 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 #include <iostream>
17 #include <sstream>
18 
19 #include "tools_op.h"
20 #include "tools_op_help.h"
21 
22 namespace OHOS::FileManagement::Backup {
23 using namespace std;
24 
GenHelpMsg()25 static string GenHelpMsg()
26 {
27     return "\t\tThis operation helps to dump the help messages.";
28 }
29 
Exec(map<string,vector<string>> & mapArgToVal)30 static int Exec(map<string, vector<string>> &mapArgToVal)
31 {
32     stringstream ss;
33     auto &&allOps = ToolsOp::GetAllOperations();
34     ss << "Usage: backup_tool <SubCommand> [OPTION]... [ARG]..." << endl;
35     for (size_t i = 0; i < allOps.size(); ++i) {
36         auto desc = allOps[i].GetDescriptor();
37 
38         // echo: <op seqs>\n
39         ss << allOps[i].GetName();
40 
41         // echo: help msgs\n\n
42         if (desc.funcGenHelpMsg) {
43             ss << desc.funcGenHelpMsg() << endl;
44         }
45         if (i != allOps.size() - 1) {
46             ss << endl;
47         }
48     }
49     printf("%s", ss.str().c_str());
50     return 0;
51 }
52 
HelpRegister()53 bool HelpRegister()
54 {
55     return ToolsOp::Register(ToolsOp{ ToolsOp::Descriptor {
56         .opName = {"help"},
57         .funcGenHelpMsg = GenHelpMsg,
58         .funcExec = Exec,
59     } });
60 }
61 } // namespace OHOS::FileManagement::Backup