/** * Copyright 2020 Huawei Technologies 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. */ #include "core/util/option_parser.h" #include #include #include #include #include #include "include/infer_log.h" namespace mindspore { namespace serving { bool StartWith(const std::string &str, const std::string &expected) { return expected.empty() || (str.size() >= expected.size() && memcmp(str.data(), expected.data(), expected.size()) == 0); } bool RemovePrefix(std::string *const str, const std::string &prefix) { if (!StartWith(*str, prefix)) return false; str->replace(str->begin(), str->begin() + prefix.size(), ""); return true; } bool Option::ParseInt32(std::string *const arg) { if (RemovePrefix(arg, "--") && RemovePrefix(arg, name_) && RemovePrefix(arg, "=")) { int32_t parsed_value; try { parsed_value = std::stoi(arg->data()); } catch (std::invalid_argument) { std::cout << "Parse " << name_ << " Error for option " << *arg << std::endl; return false; } *int32_default_ = parsed_value; return true; } return false; } bool Option::ParseBool(std::string *const arg) { if (RemovePrefix(arg, "--") && RemovePrefix(arg, name_) && RemovePrefix(arg, "=")) { if (*arg == "true") { *bool_default_ = true; } else if (*arg == "false") { *bool_default_ = false; } else { std::cout << "Parse " << name_ << " Error for option " << *arg << std::endl; return false; } return true; } return false; } bool Option::ParseString(std::string *const arg) { if (RemovePrefix(arg, "--") && RemovePrefix(arg, name_) && RemovePrefix(arg, "=")) { *string_default_ = *arg; return true; } return false; } bool Option::ParseFloat(std::string *const arg) { if (RemovePrefix(arg, "--") && RemovePrefix(arg, name_) && RemovePrefix(arg, "=")) { float parsed_value; try { parsed_value = std::stof(arg->data()); } catch (std::invalid_argument) { std::cout << "Parse " << name_ << " Error for option " << *arg << std::endl; return false; } *float_default_ = parsed_value; return true; } return false; } Option::Option(const std::string &name, int32_t *const default_point, const std::string &usage) : name_(name), type_(MS_TYPE_INT32), int32_default_(default_point), bool_default_(nullptr), string_default_(nullptr), float_default_(nullptr), usage_(usage) {} Option::Option(const std::string &name, bool *const default_point, const std::string &usage) : name_(name), type_(MS_TYPE_BOOL), int32_default_(nullptr), bool_default_(default_point), string_default_(nullptr), float_default_(nullptr), usage_(usage) {} Option::Option(const std::string &name, std::string *const default_point, const std::string &usage) : name_(name), type_(MS_TYPE_STRING), int32_default_(nullptr), bool_default_(nullptr), string_default_(default_point), float_default_(nullptr), usage_(usage) {} Option::Option(const std::string &name, float *const default_point, const std::string &usage) : name_(name), type_(MS_TYPE_FLOAT), int32_default_(nullptr), bool_default_(nullptr), string_default_(nullptr), float_default_(default_point), usage_(usage) {} bool Option::Parse(std::string *const arg) { bool result = false; switch (type_) { case MS_TYPE_BOOL: result = ParseBool(arg); break; case MS_TYPE_FLOAT: result = ParseFloat(arg); break; case MS_TYPE_INT32: result = ParseInt32(arg); break; case MS_TYPE_STRING: result = ParseString(arg); break; default: break; } return result; } std::shared_ptr Options::inst_ = nullptr; Options &Options::Instance() { static Options instance; return instance; } Options::Options() : args_(nullptr) { CreateOptions(); } void Options::CreateOptions() { args_ = std::make_shared(); std::vector