hunting/test/application/VersionReleaseTool/src/ArgvAnalysis.cpp
2024-05-17 19:57:41 +08:00

78 lines
2.3 KiB
C++

/*
* Copyright (c) 2023 Fancy Code.
* 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 "ArgvAnalysis.h"
#include "ILog.h"
const char *V_SOURCE_FILE = "v_source_file";
const char *V_OUTPUT_FILE = "v_output_file";
std::shared_ptr<ArgvAnalysis> &ArgvAnalysis::GetInstance(std::shared_ptr<ArgvAnalysis> *impl)
{
static auto instance = std::make_shared<ArgvAnalysis>();
if (impl) {
if (instance.use_count() == 1) {
LogInfo("Instance changed succeed.\n");
instance = *impl;
}
else {
LogError("Can't changing the instance becase of using by some one.\n");
}
}
return instance;
}
void ArgvAnalysis::Analyze(int argc, char *argv[])
{
LogInfo("argc: %d\n", argc);
for (int i = 1; i < argc; ++i) {
std::string arg = argv[i];
size_t pos = arg.find('=');
if (pos != std::string::npos) {
std::string key = arg.substr(0, pos);
std::string value = arg.substr(pos + 1);
// remove'--'
if (key.substr(0, 2) == "--") {
key = key.substr(2);
}
mOptions[key] = value;
}
else {
LogError("Invalid argument format: %s\n", arg.c_str());
return;
}
}
for (const auto &pair : mOptions) {
LogInfo("Key: %s, Value: %s\n", pair.first.c_str(), pair.second.c_str());
}
}
std::string ArgvAnalysis::GetSourceFile(void)
{
auto it = mOptions.find(V_SOURCE_FILE);
if (it != mOptions.end()) {
return it->second;
}
LogWarning("Can't find the source file.\n");
return "";
}
std::string ArgvAnalysis::GetOutputFile(void)
{
auto it = mOptions.find(V_OUTPUT_FILE);
if (it != mOptions.end()) {
return it->second;
}
LogWarning("Can't find the source file.\n");
return "";
}