* Copyright (c) 2021 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.
*/
#include "command.h"
#include "debug_logger.h"
#if defined(is_ohos) && is_ohos
#include "hiperf_hilog.h"
#endif
#include "option.h"
#include "subcommand.h"
#include "utilities.h"
namespace OHOS {
namespace Developtools {
namespace HiPerf {
std::string Command::fullArgument = "";
bool Command::DispatchSubCommands(std::vector<std::string> &arguments, CommandReporter &reporter)
{
auto subCommand = SubCommand::FindSubCommand(arguments.front());
if (subCommand != nullptr) {
reporter.mainCommand_ = arguments.front();
arguments.erase(arguments.begin());
HLOGD("OnSubCommandOptions -> %s", subCommand->Name().c_str());
if (subCommand->OnSubCommandOptions(arguments)) {
subCommand->AddReportArgs(reporter);
if (subCommand->OnPreSubCommand()) {
return true;
}
HLOGD("OnSubCommand -> %s", subCommand->Name().c_str());
if (HiperfError err = subCommand->OnSubCommand(arguments); err != HiperfError::NO_ERR) {
printf("subcommand '%s' failed\n", subCommand->Name().c_str());
#if defined(is_ohos) && is_ohos
HIPERF_HILOGE(MODULE_DEFAULT, "subcommand '%{public}s' failed", subCommand->Name().c_str());
#endif
reporter.errorCode_ = err;
return false;
} else {
HLOGD("OnSubCommand successed");
return true;
}
} else {
reporter.errorCode_ = HiperfError::SUBCOMMAND_OPTIONS_ERROR;
HLOGD("OnSubCommandOptions interrupt the process.");
return false;
}
} else {
printf("unknown args: %s\n", arguments.front().c_str());
return false;
}
}
bool Command::DispatchCommands(std::vector<std::string> arguments)
{
fullArgument.clear();
for (std::string &arg : arguments) {
fullArgument.append(" ");
fullArgument.append(arg);
}
CommandReporter reporter(fullArgument);
HLOGD("args:%s", VectorToString(arguments).c_str());
while (!arguments.empty()) {
auto commandOption = Option::FindMainOption(arguments.front());
if (commandOption != nullptr) {
arguments.erase(arguments.begin());
if (!commandOption->callBackFunction(arguments)) {
printf("unknown options: %s\nUse the help command to view help.\n", arguments.front().c_str());
return false;
}
continue;
} else {
return DispatchSubCommands(arguments, reporter);
}
}
return false;
}
bool Command::DispatchCommand(std::string argument)
{
return Command::DispatchCommands(StringSplit(argument, " "));
}
}
}
}