148f512ceSopenharmony_ci/*
248f512ceSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
348f512ceSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
448f512ceSopenharmony_ci * you may not use this file except in compliance with the License.
548f512ceSopenharmony_ci * You may obtain a copy of the License at
648f512ceSopenharmony_ci *
748f512ceSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
848f512ceSopenharmony_ci *
948f512ceSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1048f512ceSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1148f512ceSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1248f512ceSopenharmony_ci * See the License for the specific language governing permissions and
1348f512ceSopenharmony_ci * limitations under the License.
1448f512ceSopenharmony_ci */
1548f512ceSopenharmony_ci
1648f512ceSopenharmony_ci#include "command.h"
1748f512ceSopenharmony_ci#include "debug_logger.h"
1848f512ceSopenharmony_ci#include "option.h"
1948f512ceSopenharmony_ci#include "subcommand.h"
2048f512ceSopenharmony_ci#include "utilities.h"
2148f512ceSopenharmony_cinamespace OHOS {
2248f512ceSopenharmony_cinamespace Developtools {
2348f512ceSopenharmony_cinamespace HiPerf {
2448f512ceSopenharmony_cistd::string Command::fullArgument;
2548f512ceSopenharmony_cibool Command::DispatchCommands(std::vector<std::string> arguments)
2648f512ceSopenharmony_ci{
2748f512ceSopenharmony_ci    for (std::string arg : arguments) {
2848f512ceSopenharmony_ci        fullArgument.append(" ");
2948f512ceSopenharmony_ci        fullArgument.append(arg);
3048f512ceSopenharmony_ci    }
3148f512ceSopenharmony_ci    HLOGD("args:%s", VectorToString(arguments).c_str());
3248f512ceSopenharmony_ci    while (!arguments.empty()) {
3348f512ceSopenharmony_ci        // we need found main command args first
3448f512ceSopenharmony_ci        auto commandOption = Option::FindMainOption(arguments.front());
3548f512ceSopenharmony_ci        if (commandOption != nullptr) {
3648f512ceSopenharmony_ci            // this is a arg which we support
3748f512ceSopenharmony_ci
3848f512ceSopenharmony_ci            // remove the arg name
3948f512ceSopenharmony_ci            arguments.erase(arguments.begin());
4048f512ceSopenharmony_ci
4148f512ceSopenharmony_ci            if (!commandOption->callBackFunction(arguments)) {
4248f512ceSopenharmony_ci                printf("unknown options: %s\nUse the help command to view help.\n", arguments.front().c_str());
4348f512ceSopenharmony_ci                return false;
4448f512ceSopenharmony_ci            }
4548f512ceSopenharmony_ci            // goto next args
4648f512ceSopenharmony_ci            continue;
4748f512ceSopenharmony_ci        } else {
4848f512ceSopenharmony_ci            // if it is an sub command
4948f512ceSopenharmony_ci            auto subCommand = SubCommand::FindSubCommand(arguments.front());
5048f512ceSopenharmony_ci            if (subCommand != nullptr) {
5148f512ceSopenharmony_ci                // this is an sub command which we support
5248f512ceSopenharmony_ci
5348f512ceSopenharmony_ci                // remove the subcmd name
5448f512ceSopenharmony_ci                arguments.erase(arguments.begin());
5548f512ceSopenharmony_ci
5648f512ceSopenharmony_ci                // if we found the sub command , after it processed , we will exit
5748f512ceSopenharmony_ci                HLOGD("OnSubCommandOptions -> %s", subCommand->Name().c_str());
5848f512ceSopenharmony_ci                if (subCommand->OnSubCommandOptions(arguments)) {
5948f512ceSopenharmony_ci                    // if some help cmd ?
6048f512ceSopenharmony_ci                    if (subCommand->OnPreSubCommand()) {
6148f512ceSopenharmony_ci                        return true;
6248f512ceSopenharmony_ci                    }
6348f512ceSopenharmony_ci
6448f512ceSopenharmony_ci                    HLOGD("OnSubCommand -> %s", subCommand->Name().c_str());
6548f512ceSopenharmony_ci                    if (!subCommand->OnSubCommand(arguments)) {
6648f512ceSopenharmony_ci                        printf("subcommand '%s' failed\n", subCommand->Name().c_str());
6748f512ceSopenharmony_ci                        return false;
6848f512ceSopenharmony_ci                    } else {
6948f512ceSopenharmony_ci                        HLOGD("OnSubCommand successed");
7048f512ceSopenharmony_ci                        return true;
7148f512ceSopenharmony_ci                    }
7248f512ceSopenharmony_ci                } else {
7348f512ceSopenharmony_ci                    HLOGD("OnSubCommandOptions interrupt the process.");
7448f512ceSopenharmony_ci                    return false;
7548f512ceSopenharmony_ci                }
7648f512ceSopenharmony_ci            } else {
7748f512ceSopenharmony_ci                // we don't support this command
7848f512ceSopenharmony_ci                printf("unknown args: %s\n", arguments.front().c_str());
7948f512ceSopenharmony_ci                return false;
8048f512ceSopenharmony_ci            }
8148f512ceSopenharmony_ci        }
8248f512ceSopenharmony_ci    }
8348f512ceSopenharmony_ci    return false;
8448f512ceSopenharmony_ci}
8548f512ceSopenharmony_ci
8648f512ceSopenharmony_cibool Command::DispatchCommand(std::string argument)
8748f512ceSopenharmony_ci{
8848f512ceSopenharmony_ci    return Command::DispatchCommands(StringSplit(argument, " "));
8948f512ceSopenharmony_ci}
9048f512ceSopenharmony_ci} // namespace HiPerf
9148f512ceSopenharmony_ci} // namespace Developtools
9248f512ceSopenharmony_ci} // namespace OHOS
93