* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef HIPERF_OPTION_H_
#define HIPERF_OPTION_H_
#include <functional>
#include <map>
#include <memory>
#include <string>
#include <vector>
#include "debug_logger.h"
#include "utilities.h"
using argsVector = std::vector<std::string>;
namespace OHOS {
namespace Developtools {
namespace HiPerf {
namespace Option {
struct MainOption {
std::string help;
std::function<bool(std::vector<std::string> &)> callBackFunction;
};
bool RegisterMainOption(const std::string &optionName, const std::string &help,
std::function<bool(std::vector<std::string> &)> callBackFunction);
void ClearMainOptions();
bool CheckOptionFormat(const std::string &optionName);
argsVector::iterator FindOption(argsVector &args, const std::string &optionName);
bool GetValueFromString(const std::string &optionValue, const std::string &optionName, bool &value);
bool GetValueFromString(const std::string &optionValue, const std::string &optionName, int &value);
bool GetValueFromString(const std::string &optionValue, const std::string &optionName, float &value);
bool GetValueFromString(const std::string &optionValue, const std::string &optionName, uint64_t &value);
bool GetValueFromString(const std::string &optionValue, const std::string &optionName, std::string &value);
bool GetValueFromString(const std::string &optionValue, const std::string &optionName, std::vector<int> &values);
bool GetValueFromString(const std::string &optionValue, const std::string &optionName,
std::vector<std::string> &values);
bool GetOptionTrackedCommand(argsVector &args, std::vector<std::string> &trackedCommand);
Return false to indicate that the parameter is illegal
The program should exit with an error.
Return true, indicating that the parameter is legal (but the user does not necessarily enter the
parameter)
*/
template<class T>
bool GetValue(argsVector &args, const std::string &optionName, T &value, T &localValues)
{
if (!CheckOptionFormat(optionName)) {
if (optionName.empty()) {
printf("unable to use empty option name!\n");
} else {
printf("format error. must use '-' at the begin of option '%s'!\n", optionName.c_str());
}
return false;
}
auto it = FindOption(args, optionName);
if (it == args.end()) {
HLOGV("not found option, return default value");
return true;
} else {
it = args.erase(it);
if constexpr (std::is_same<T, bool>::value) {
GetValueFromString(optionName, optionName, value);
return true;
} else if (it == args.end()) {
printf("option %s value missed\n", optionName.c_str());
return false;
} else if (GetValueFromString(*it, optionName, localValues)) {
value = localValues;
args.erase(it);
return true;
} else {
printf("incorrect option value '%s' for option '%s'. View the usage with the --help option.\n",
(*it).c_str(), optionName.c_str());
return false;
}
}
return true;
}
template<class T>
bool GetOptionValue(argsVector &args, const std::string optionName, T &value)
{
T localValues = {};
if constexpr (std::is_same<T, std::vector<std::vector<std::string>>>::value) {
while (true) {
if (!GetOptionValue(args, optionName, localValues.emplace_back())) {
printf("incorrect option %s\n", optionName.c_str());
return false;
} else if (localValues.back().size() == 0) {
localValues.pop_back();
break;
}
}
if (localValues.size() > 0) {
value = localValues;
}
return true;
} else {
return GetValue(args, optionName, value, localValues);
}
}
const MainOption *FindMainOption(const std::string &argName);
const std::map<std::string, std::unique_ptr<MainOption>> &GetMainOptions();
}
}
}
}
#endif