From 3d64ed05652cbe2a399e35963ca0b8236214ca71 Mon Sep 17 00:00:00 2001 From: zhaoyrr Date: Wed, 13 Aug 2025 17:21:47 +0800 Subject: [PATCH] =?UTF-8?q?aa=E5=91=BD=E4=BB=A4=E9=97=AE=E9=A2=98=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhaoyrr --- tools/aa/include/ability_command.h | 4 +- tools/aa/src/ability_command.cpp | 177 ++++++++++++++++++++++++++++- 2 files changed, 178 insertions(+), 3 deletions(-) diff --git a/tools/aa/include/ability_command.h b/tools/aa/include/ability_command.h index d709ed7d35c..6e0ec5866b0 100644 --- a/tools/aa/include/ability_command.h +++ b/tools/aa/include/ability_command.h @@ -63,7 +63,8 @@ const std::string HELP_MSG_START = "usage: aa start \n" "options list:\n" " -h, --help list available commands\n" - " [-d ] [-a -b ] [-m ] [-p ] [-D] [-E] [-S] [-N]" + " [-d ] [-a -b ] [-m ] [-p ] " + " [-C] [-D] [-E] [-S] [-N] " " [-R] [-c] [--ps ] " " [--pi ] " " [--pb ] " @@ -272,6 +273,7 @@ private: sptr GetAbilityManagerService(); ErrCode MakeWantFromCmd(Want& want, std::string& windowMode); + ErrCode MakeWantFromCmdForStopService(Want& want); ErrCode MakeWantForProcess(Want& want); ErrCode RunAsTestCommand(); ErrCode TestCommandError(const std::string& info); diff --git a/tools/aa/src/ability_command.cpp b/tools/aa/src/ability_command.cpp index 790f68e371c..03da0442e11 100644 --- a/tools/aa/src/ability_command.cpp +++ b/tools/aa/src/ability_command.cpp @@ -492,8 +492,7 @@ ErrCode AbilityManagerShellCommand::RunAsStopService() ErrCode result = OHOS::ERR_OK; Want want; - std::string windowMode; - result = MakeWantFromCmd(want, windowMode); + result = MakeWantFromCmdForStopService(want); if (result == OHOS::ERR_OK) { result = AbilityManagerClient::GetInstance()->StopServiceAbility(want); if (result == OHOS::ERR_OK) { @@ -2194,6 +2193,180 @@ ErrCode AbilityManagerShellCommand::MakeWantFromCmd(Want& want, std::string& win return result; } +ErrCode AbilityManagerShellCommand::MakeWantFromCmdForStopService(Want& want) +{ + int result = OHOS::ERR_OK; + int option = -1; + int counter = 0; + std::string deviceId = ""; + std::string bundleName = ""; + std::string abilityName = ""; + std::string moduleName; + + while (true) { + counter++; + + option = getopt_long(argc_, argv_, SHORT_OPTIONS.c_str(), LONG_OPTIONS, nullptr); + + TAG_LOGI( + AAFwkTag::AA_TOOL, "option: %{public}d, optopt: %{public}d, optind: %{public}d", option, optopt, optind); + + if (optind < 0 || optind > argc_) { + return OHOS::ERR_INVALID_VALUE; + } + + if (option == -1) { + // When scanning the first argument + if (counter == 1 && strcmp(argv_[optind], cmd_.c_str()) == 0) { + // 'aa stop-service' with no option: aa stop-service + // 'aa stop-service' with a wrong argument: aa stop-service xxx + TAG_LOGI(AAFwkTag::AA_TOOL, "'aa %{public}s' %{public}s", HELP_MSG_NO_OPTION.c_str(), cmd_.c_str()); + + resultReceiver_.append(HELP_MSG_NO_OPTION + "\n"); + result = OHOS::ERR_INVALID_VALUE; + } + break; + } + + if (option == '?') { + switch (optopt) { + case 'h': { + // 'aa stop-service -h' + result = OHOS::ERR_INVALID_VALUE; + break; + } + case 'd': { + // 'aa stop-service -d' with no argument + TAG_LOGI(AAFwkTag::AA_TOOL, "'aa %{public}s -d' no arg", cmd_.c_str()); + + resultReceiver_.append("error: option "); + resultReceiver_.append("requires a value.\n"); + + result = OHOS::ERR_INVALID_VALUE; + break; + } + case 'a': { + // 'aa stop-service -a' with no argument + TAG_LOGI(AAFwkTag::AA_TOOL, "'aa %{public}s -a' no arg", cmd_.c_str()); + + resultReceiver_.append("error: option "); + resultReceiver_.append("requires a value.\n"); + + result = OHOS::ERR_INVALID_VALUE; + break; + } + case 'b': { + // 'aa stop-service -b' with no argument + TAG_LOGI(AAFwkTag::AA_TOOL, "'aa %{public}s -b' no arg", cmd_.c_str()); + + resultReceiver_.append("error: option "); + resultReceiver_.append("requires a value.\n"); + + result = OHOS::ERR_INVALID_VALUE; + break; + } + case 'm': { + // 'aa stop-service -m' with no argument + TAG_LOGI(AAFwkTag::AA_TOOL, "'aa %{public}s -m' no arg", cmd_.c_str()); + + resultReceiver_.append("error: option "); + resultReceiver_.append("requires a value.\n"); + + result = OHOS::ERR_INVALID_VALUE; + break; + } + case 0: { + // 'aa stop-service' with an unknown option: aa stop-service --x + // 'aa stop-service' with an unknown option: aa stop-service --xxx + std::string unknownOption = ""; + std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption); + + TAG_LOGI(AAFwkTag::AA_TOOL, "'aa %{public}s' opt unknown", cmd_.c_str()); + + resultReceiver_.append(unknownOptionMsg); + result = OHOS::ERR_INVALID_VALUE; + break; + } + default: { + // 'aa stop-service' with an unknown option: aa stop-service -x + // 'aa stop-service' with an unknown option: aa stop-service -xxx + std::string unknownOption = ""; + std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption); + + TAG_LOGI(AAFwkTag::AA_TOOL, "'aa %{public}s' opt unknown", cmd_.c_str()); + + resultReceiver_.append(unknownOptionMsg); + result = OHOS::ERR_INVALID_VALUE; + break; + } + } + break; + } + + switch (option) { + case 'h': { + // 'aa stop-service -h' + // 'aa stop-service --help' + result = OHOS::ERR_INVALID_VALUE; + break; + } + case 'd': { + // 'aa stop-service -d xxx' + + // save device ID + if (optarg != nullptr) { + deviceId = optarg; + } + break; + } + case 'a': { + // 'aa stop-service -a xxx' + + // save ability name + abilityName = optarg; + break; + } + case 'b': { + // 'aa stop-service -b xxx' + + // save bundle name + bundleName = optarg; + break; + } + case 'm': { + // 'aa stop-service -m xxx' + + // save module name + moduleName = optarg; + break; + } + case 0: { + break; + } + default: { + break; + } + } + } + + if (result == OHOS::ERR_OK) { + if (!abilityName.empty() && bundleName.empty()) { + // explicitly start ability must have both ability and bundle names + + // 'aa stop-service [-d ] -a -b ' + TAG_LOGI(AAFwkTag::AA_TOOL, "'aa %{public}s' without enough options", cmd_.c_str()); + + resultReceiver_.append(HELP_MSG_NO_BUNDLE_NAME_OPTION + "\n"); + result = OHOS::ERR_INVALID_VALUE; + } else { + ElementName element(deviceId, bundleName, abilityName, moduleName); + want.SetElement(element); + } + } + + return result; +} + ErrCode AbilityManagerShellCommand::RunAsTestCommand() { TAG_LOGD(AAFwkTag::AA_TOOL, "enter"); -- Gitee