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