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_OPTION_H_
16 #define HIPERF_OPTION_H_
17
18 #include <functional>
19 #include <map>
20 #include <memory>
21 #include <string>
22 #include <vector>
23 #include "debug_logger.h"
24 #include "utilities.h"
25
26 using argsVector = std::vector<std::string>;
27 namespace OHOS {
28 namespace Developtools {
29 namespace HiPerf {
30 namespace Option {
31 struct MainOption {
32 std::string help;
33 std::function<bool(std::vector<std::string> &)> callBackFunction;
34 };
35
36 // called from main
37 bool RegisterMainOption(const std::string &, const std::string &,
38 std::function<bool(std::vector<std::string> &)>);
39
40 void ClearMainOptions();
41
42 bool CheckOptionFormat(const std::string &optionName);
43
44 argsVector::iterator FindOption(argsVector &args, const std::string &optionName);
45
46 // some option function
47 bool GetValueFromString(const std::string &optionValue, const std::string &optionName, bool &value);
48 bool GetValueFromString(const std::string &optionValue, const std::string &optionName, int &);
49 bool GetValueFromString(const std::string &optionValue, const std::string &optionName,
50 float &value);
51 bool GetValueFromString(const std::string &optionValue, const std::string &optionName,
52 std::string &value);
53 bool GetValueFromString(const std::string &optionValue, const std::string &optionName,
54 std::vector<int> &value);
55 bool GetValueFromString(const std::string &optionValue, const std::string &optionName,
56 std::vector<std::string> &value);
57
58 bool GetOptionTrackedCommand(argsVector &args, std::vector<std::string> &trackedCommand);
59
60 /*
61 Return false to indicate that the parameter is illegal
62 The program should exit with an error.
63
64 Return true, indicating that the parameter is legal (but the user does not necessarily enter the
65 parameter)
66 */
67 template<class T>
GetOptionValue(argsVector & args,std::string optionName,T & value)68 bool GetOptionValue(argsVector &args, std::string optionName, T &value)
69 {
70 // we need keep the ref if we got failed
71 // so we use a local value first.
72 T localValues = {};
73 if constexpr (std::is_same<T, std::vector<std::vector<std::string>>>::value) {
74 // try unitl failed.
75 while (true) {
76 if (!GetOptionValue(args, optionName, localValues.emplace_back())) {
77 printf("incorrect option %s\n", optionName.c_str());
78 return false; // format error
79 } else if (localValues.back().size() == 0) {
80 // if the last one we request is empty , we remove it
81 localValues.pop_back();
82 // nothing more needed
83 // we don't allow empty value
84 break;
85 }
86 }
87 if (localValues.size() > 0) {
88 value = localValues;
89 }
90 return true;
91 } else {
92 if (!CheckOptionFormat(optionName)) {
93 if (optionName.empty()) {
94 printf("unable to use empty option name!\n");
95 } else {
96 printf("format error. must use '-' at the begin of option '%s'!\n",
97 optionName.c_str());
98 }
99 return false; // something wrong
100 }
101 auto it = FindOption(args, optionName);
102 if (it == args.end()) {
103 HLOGV("not found option, return default value");
104 return true; // not found but also not error
105 } else {
106 it = args.erase(it);
107 // some special case
108 if constexpr (std::is_same<T, bool>::value) {
109 // for bool we don't need get value.
110 // this always return true
111 GetValueFromString(optionName, optionName, value);
112 return true;
113 } else if (it == args.end()) {
114 // no value means failed
115 printf("option %s value missed\n", optionName.c_str());
116 return false;
117 } else if (GetValueFromString(*it, optionName, localValues)) {
118 // got some value
119 value = localValues;
120 args.erase(it);
121 return true;
122 } else {
123 // have value but convert failed.
124 printf("incorrect option value '%s'. View the usage with the --help option.\n",
125 (*it).c_str());
126 return false;
127 }
128 }
129 }
130 }
131
132 const MainOption *FindMainOption(std::string);
133
134 const std::map<std::string, std::unique_ptr<MainOption>> &GetMainOptions();
135 } // namespace Option
136 } // namespace HiPerf
137 } // namespace Developtools
138 } // namespace OHOS
139 #endif // HIPERF_OPTION_H_
140