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