From ba74695aa001329684ce9ca132b1a1b4d98fcac3 Mon Sep 17 00:00:00 2001 From: yangxuguang-huawei Date: Wed, 10 Jan 2024 16:37:32 +0800 Subject: [PATCH 01/20] feat: add key value option Signed-off-by: yangxuguang-huawei --- tools/aa/src/ability_command.cpp | 76 +++++++++++++++++++++++++++++++- 1 file changed, 75 insertions(+), 1 deletion(-) diff --git a/tools/aa/src/ability_command.cpp b/tools/aa/src/ability_command.cpp index 8ba932729ab..311515f2544 100644 --- a/tools/aa/src/ability_command.cpp +++ b/tools/aa/src/ability_command.cpp @@ -35,7 +35,7 @@ namespace AAFwk { namespace { constexpr size_t PARAM_LENGTH = 1024; -const std::string SHORT_OPTIONS = "ch:d:a:b:p:s:m:CDSN"; +const std::string SHORT_OPTIONS = "ch:d:a:b:p:s:m:k:CDSN"; constexpr struct option LONG_OPTIONS[] = { {"help", no_argument, nullptr, 'h'}, {"device", required_argument, nullptr, 'd'}, @@ -807,6 +807,42 @@ bool AbilityManagerShellCommand::CheckPerfCmdString( return true; } +std::vector Split(const std::string& str, char delim) +{ + std::stringstream ss(str); + std::string item; + std::vector elems; + while (std::getline(ss, item, delim)) + { + if (!item.empty()) + elems.push_back(item); + } + return elems; +} + +void ParseToKeyValuePairs(const std::string& str, std::map& m) +{ + auto vs = Split(str, ';'); + for (auto s : vs) + { + auto v = Split(s, ':'); + if (v.size == 2) m.insert(v[0], v[1]); + } +} + +void PrintMapMessage(std::string& str, const std::map& m) +{ + std::map::iterator it; + for (it = m.begin(); it != m.end(); it++) + { + str.append("received key: "); + str.append(it->first); + str.append(" value: "); + str.append(it->second); + str.append("\n"); + } +} + ErrCode AbilityManagerShellCommand::MakeWantForProcess(Want& want) { int result = OHOS::ERR_OK; @@ -818,6 +854,7 @@ ErrCode AbilityManagerShellCommand::MakeWantForProcess(Want& want) std::string moduleName = ""; std::string perfCmd = ""; std::string debugCmd = ""; + std::map mapKeyValues; bool isPerf = false; bool isSandboxApp = false; @@ -887,6 +924,15 @@ ErrCode AbilityManagerShellCommand::MakeWantForProcess(Want& want) result = OHOS::ERR_INVALID_VALUE; break; } + case 'k': { + // 'aa process -k' with no argument + HILOG_INFO("'aa %{public}s -k' with no argument.", cmd_.c_str()); + resultReceiver_.append("error: option "); + resultReceiver_.append("requires a value.\n"); + + result = OHOS::ERR_INVALID_VALUE; + break; + } case 'D': { // 'aa process -D' with no argument HILOG_INFO("'aa %{public}s -D' with no argument.", cmd_.c_str()); @@ -959,6 +1005,15 @@ ErrCode AbilityManagerShellCommand::MakeWantForProcess(Want& want) } break; } + case 'k': { + // 'aa process -k k1:v1;k2:v2' + // save key value maps + ParseToKeyValuePairs(optarg, mapKeyValues); + // print the map + PrintMapMessage(resultReceiver_, mapKeyValues); + + break; + } case 'D': { // 'aa process -D xxx' // save debug cmd @@ -1114,6 +1169,7 @@ ErrCode AbilityManagerShellCommand::MakeWantFromCmd(Want& want, std::string& win std::string abilityName = ""; std::string moduleName; std::string perfCmd; + std::map mapKeyValues; bool isColdStart = false; bool isDebugApp = false; bool isContinuation = false; @@ -1221,6 +1277,17 @@ ErrCode AbilityManagerShellCommand::MakeWantFromCmd(Want& want, std::string& win result = OHOS::ERR_INVALID_VALUE; break; } + case 'k': { + // 'aa start -k' with no argumnet + // 'aa stop-service -k' with no argument + HILOG_INFO("'aa %{public}s -k' with no argument.", cmd_.c_str()); + + resultReceiver_.append("error: option "); + resultReceiver_.append("requires a value.\n"); + + result = OHOS::ERR_INVALID_VALUE; + break; + } case 0: { // 'aa start' with an unknown option: aa start --x // 'aa start' with an unknown option: aa start --xxx @@ -1313,6 +1380,13 @@ ErrCode AbilityManagerShellCommand::MakeWantFromCmd(Want& want, std::string& win } break; } + case 'k': { + ParseToKeyValuePairs(optarg, mapKeyValues); + // print map + PrintMapMessage(resultReceiver_, mapKeyValues); + + break; + } case 'C': { // 'aa start -C' // cold start app -- Gitee From c7436fe0ef3dd0563d3b53cb5e8c25d706e6fa89 Mon Sep 17 00:00:00 2001 From: yangxuguang-huawei Date: Thu, 11 Jan 2024 09:59:27 +0800 Subject: [PATCH 02/20] feat: add a long option 'parameters' to start command Signed-off-by: yangxuguang-huawei --- tools/aa/include/ability_command.h | 1 + tools/aa/src/ability_command.cpp | 67 ++++++++++-------------------- 2 files changed, 23 insertions(+), 45 deletions(-) diff --git a/tools/aa/include/ability_command.h b/tools/aa/include/ability_command.h index c347b5a95ce..c1eb9474f99 100644 --- a/tools/aa/include/ability_command.h +++ b/tools/aa/include/ability_command.h @@ -63,6 +63,7 @@ const std::string HELP_MSG_START = "options list:\n" " -h, --help list available commands\n" " [-d ] -a -b [-m ] [-p ] [-D] [-S] [-N] " + " [-P key1:value1;key2:value2]" " start ability with an element name\n"; const std::string HELP_MSG_STOP_SERVICE = diff --git a/tools/aa/src/ability_command.cpp b/tools/aa/src/ability_command.cpp index 311515f2544..da74f56b839 100644 --- a/tools/aa/src/ability_command.cpp +++ b/tools/aa/src/ability_command.cpp @@ -35,7 +35,7 @@ namespace AAFwk { namespace { constexpr size_t PARAM_LENGTH = 1024; -const std::string SHORT_OPTIONS = "ch:d:a:b:p:s:m:k:CDSN"; +const std::string SHORT_OPTIONS = "ch:d:a:b:p:s:m:P:CDSN"; constexpr struct option LONG_OPTIONS[] = { {"help", no_argument, nullptr, 'h'}, {"device", required_argument, nullptr, 'd'}, @@ -47,6 +47,7 @@ constexpr struct option LONG_OPTIONS[] = { {"cold-start", no_argument, nullptr, 'C'}, {"debug", no_argument, nullptr, 'D'}, {"native-debug", no_argument, nullptr, 'N'}, + {"parameters", required_argument, nullptr, 'P'}, {nullptr, 0, nullptr, 0}, }; const std::string SHORT_OPTIONS_APPLICATION_NOT_RESPONDING = "hp:"; @@ -830,19 +831,6 @@ void ParseToKeyValuePairs(const std::string& str, std::map& m) -{ - std::map::iterator it; - for (it = m.begin(); it != m.end(); it++) - { - str.append("received key: "); - str.append(it->first); - str.append(" value: "); - str.append(it->second); - str.append("\n"); - } -} - ErrCode AbilityManagerShellCommand::MakeWantForProcess(Want& want) { int result = OHOS::ERR_OK; @@ -854,7 +842,6 @@ ErrCode AbilityManagerShellCommand::MakeWantForProcess(Want& want) std::string moduleName = ""; std::string perfCmd = ""; std::string debugCmd = ""; - std::map mapKeyValues; bool isPerf = false; bool isSandboxApp = false; @@ -924,15 +911,6 @@ ErrCode AbilityManagerShellCommand::MakeWantForProcess(Want& want) result = OHOS::ERR_INVALID_VALUE; break; } - case 'k': { - // 'aa process -k' with no argument - HILOG_INFO("'aa %{public}s -k' with no argument.", cmd_.c_str()); - resultReceiver_.append("error: option "); - resultReceiver_.append("requires a value.\n"); - - result = OHOS::ERR_INVALID_VALUE; - break; - } case 'D': { // 'aa process -D' with no argument HILOG_INFO("'aa %{public}s -D' with no argument.", cmd_.c_str()); @@ -1005,15 +983,6 @@ ErrCode AbilityManagerShellCommand::MakeWantForProcess(Want& want) } break; } - case 'k': { - // 'aa process -k k1:v1;k2:v2' - // save key value maps - ParseToKeyValuePairs(optarg, mapKeyValues); - // print the map - PrintMapMessage(resultReceiver_, mapKeyValues); - - break; - } case 'D': { // 'aa process -D xxx' // save debug cmd @@ -1169,7 +1138,7 @@ ErrCode AbilityManagerShellCommand::MakeWantFromCmd(Want& want, std::string& win std::string abilityName = ""; std::string moduleName; std::string perfCmd; - std::map mapKeyValues; + std::map parameters; bool isColdStart = false; bool isDebugApp = false; bool isContinuation = false; @@ -1277,15 +1246,17 @@ ErrCode AbilityManagerShellCommand::MakeWantFromCmd(Want& want, std::string& win result = OHOS::ERR_INVALID_VALUE; break; } - case 'k': { - // 'aa start -k' with no argumnet - // 'aa stop-service -k' with no argument - HILOG_INFO("'aa %{public}s -k' with no argument.", cmd_.c_str()); + case 'P': { + if (strcmp(cmd_.c_str(), "start") == 0) { + // parameters are only valid for the start command + // 'aa start -P' with no argumnet + HILOG_INFO("'aa %{public}s -k' with no argument.", cmd_.c_str()); - resultReceiver_.append("error: option "); - resultReceiver_.append("requires a value.\n"); + resultReceiver_.append("error: option "); + resultReceiver_.append("requires a value.\n"); - result = OHOS::ERR_INVALID_VALUE; + result = OHOS::ERR_INVALID_VALUE; + } break; } case 0: { @@ -1380,10 +1351,11 @@ ErrCode AbilityManagerShellCommand::MakeWantFromCmd(Want& want, std::string& win } break; } - case 'k': { - ParseToKeyValuePairs(optarg, mapKeyValues); - // print map - PrintMapMessage(resultReceiver_, mapKeyValues); + case 'P': { + // 'aa start -P xxx' + + // parse option arguments into a key-value map + ParseToKeyValuePairs(optarg, parameters); break; } @@ -1463,6 +1435,11 @@ ErrCode AbilityManagerShellCommand::MakeWantFromCmd(Want& want, std::string& win if (isNativeDebug) { want.SetParam("nativeDebug", isNativeDebug); } + if (!parameters.empty()) { + for (std::map::iterator it = parameters.begin(); it != parameters.end{}. it++) { + want.SetParam(it->first, it->second); + } + } } } -- Gitee From 3558363308fe403253fa678ee1a1cae955061ada Mon Sep 17 00:00:00 2001 From: yangxuguang-huawei Date: Thu, 11 Jan 2024 14:55:14 +0800 Subject: [PATCH 03/20] fix: add () Signed-off-by: yangxuguang-huawei --- tools/aa/src/ability_command.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/aa/src/ability_command.cpp b/tools/aa/src/ability_command.cpp index da74f56b839..5b72daa49a7 100644 --- a/tools/aa/src/ability_command.cpp +++ b/tools/aa/src/ability_command.cpp @@ -827,7 +827,7 @@ void ParseToKeyValuePairs(const std::string& str, std::map Date: Thu, 11 Jan 2024 14:56:02 +0800 Subject: [PATCH 04/20] fix: {} -> () Signed-off-by: yangxuguang-huawei --- tools/aa/src/ability_command.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/aa/src/ability_command.cpp b/tools/aa/src/ability_command.cpp index 5b72daa49a7..ff6e3dc4242 100644 --- a/tools/aa/src/ability_command.cpp +++ b/tools/aa/src/ability_command.cpp @@ -1436,7 +1436,7 @@ ErrCode AbilityManagerShellCommand::MakeWantFromCmd(Want& want, std::string& win want.SetParam("nativeDebug", isNativeDebug); } if (!parameters.empty()) { - for (std::map::iterator it = parameters.begin(); it != parameters.end{}. it++) { + for (std::map::iterator it = parameters.begin(); it != parameters.end(). it++) { want.SetParam(it->first, it->second); } } -- Gitee From 8b5e8c8b570c5c64853c4cdddabdd938bd95a51a Mon Sep 17 00:00:00 2001 From: yangxuguang-huawei Date: Thu, 11 Jan 2024 16:27:16 +0800 Subject: [PATCH 05/20] fix: fix compile errors Signed-off-by: yangxuguang-huawei --- tools/aa/src/ability_command.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/aa/src/ability_command.cpp b/tools/aa/src/ability_command.cpp index ff6e3dc4242..ebc15714ebe 100644 --- a/tools/aa/src/ability_command.cpp +++ b/tools/aa/src/ability_command.cpp @@ -827,7 +827,7 @@ void ParseToKeyValuePairs(const std::string& str, std::map::iterator it = parameters.begin(); it != parameters.end(). it++) { + for (std::map::iterator it = parameters.begin(); it != parameters.end(); it++) { want.SetParam(it->first, it->second); } } -- Gitee From c7d2c6cbfbcf494c511944401e27af40e4a2615c Mon Sep 17 00:00:00 2001 From: yangxuguang-huawei Date: Thu, 11 Jan 2024 16:53:22 +0800 Subject: [PATCH 06/20] =?UTF-8?q?fix:=20=EF=BC=9B->=20;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yangxuguang-huawei --- tools/aa/src/ability_command.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/aa/src/ability_command.cpp b/tools/aa/src/ability_command.cpp index ebc15714ebe..f1d5d6774a0 100644 --- a/tools/aa/src/ability_command.cpp +++ b/tools/aa/src/ability_command.cpp @@ -1436,7 +1436,7 @@ ErrCode AbilityManagerShellCommand::MakeWantFromCmd(Want& want, std::string& win want.SetParam("nativeDebug", isNativeDebug); } if (!parameters.empty()) { - for (std::map::iterator it = parameters.begin(); it != parameters.end(); it++) { + for (std::map::iterator it = parameters.begin(); it != parameters.end(); it++) { want.SetParam(it->first, it->second); } } -- Gitee From 54e3f2e8a053b61ddda81d3c6710aef0c5b9b5bf Mon Sep 17 00:00:00 2001 From: yangxuguang-huawei Date: Fri, 12 Jan 2024 10:17:01 +0800 Subject: [PATCH 07/20] feat: add action and uri options Signed-off-by: yangxuguang-huawei --- tools/aa/include/ability_command.h | 4 ++- tools/aa/src/ability_command.cpp | 54 ++++++++++++++++++++++++++++-- 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/tools/aa/include/ability_command.h b/tools/aa/include/ability_command.h index c1eb9474f99..efac6495189 100644 --- a/tools/aa/include/ability_command.h +++ b/tools/aa/include/ability_command.h @@ -63,7 +63,9 @@ const std::string HELP_MSG_START = "options list:\n" " -h, --help list available commands\n" " [-d ] -a -b [-m ] [-p ] [-D] [-S] [-N] " - " [-P key1:value1;key2:value2]" + " [-P ] " + " [-A ] " + " [-U ] " " start ability with an element name\n"; const std::string HELP_MSG_STOP_SERVICE = diff --git a/tools/aa/src/ability_command.cpp b/tools/aa/src/ability_command.cpp index f1d5d6774a0..bf5adaf266a 100644 --- a/tools/aa/src/ability_command.cpp +++ b/tools/aa/src/ability_command.cpp @@ -35,7 +35,7 @@ namespace AAFwk { namespace { constexpr size_t PARAM_LENGTH = 1024; -const std::string SHORT_OPTIONS = "ch:d:a:b:p:s:m:P:CDSN"; +const std::string SHORT_OPTIONS = "ch:d:a:b:p:s:m:P:A:U:CDSN"; constexpr struct option LONG_OPTIONS[] = { {"help", no_argument, nullptr, 'h'}, {"device", required_argument, nullptr, 'd'}, @@ -48,6 +48,8 @@ constexpr struct option LONG_OPTIONS[] = { {"debug", no_argument, nullptr, 'D'}, {"native-debug", no_argument, nullptr, 'N'}, {"parameters", required_argument, nullptr, 'P'}, + {"action", required_argument, nullptr, "A"}, + {"URI", required_argument, nullptr, "U"}, {nullptr, 0, nullptr, 0}, }; const std::string SHORT_OPTIONS_APPLICATION_NOT_RESPONDING = "hp:"; @@ -1139,6 +1141,8 @@ ErrCode AbilityManagerShellCommand::MakeWantFromCmd(Want& want, std::string& win std::string moduleName; std::string perfCmd; std::map parameters; + std::string uri; + std::string action; bool isColdStart = false; bool isDebugApp = false; bool isContinuation = false; @@ -1250,7 +1254,33 @@ ErrCode AbilityManagerShellCommand::MakeWantFromCmd(Want& want, std::string& win if (strcmp(cmd_.c_str(), "start") == 0) { // parameters are only valid for the start command // 'aa start -P' with no argumnet - HILOG_INFO("'aa %{public}s -k' with no argument.", cmd_.c_str()); + HILOG_INFO("'aa %{public}s -P' with no argument.", cmd_.c_str()); + + resultReceiver_.append("error: option "); + resultReceiver_.append("requires a value.\n"); + + result = OHOS::ERR_INVALID_VALUE; + } + break; + } + case 'A': { + if (strcmp(cmd_.c_str(), "start") == 0) { + // action is only valid for the start command + // 'aa start -A' with no argumnet + HILOG_INFO("'aa %{public}s -A' with no argument.", cmd_.c_str()); + + resultReceiver_.append("error: option "); + resultReceiver_.append("requires a value.\n"); + + result = OHOS::ERR_INVALID_VALUE; + } + break; + } + case 'U': { + if (strcmp(cmd_.c_str(), "start") == 0) { + // URI is only valid for the start command + // 'aa start -U' with no argumnet + HILOG_INFO("'aa %{public}s -U' with no argument.", cmd_.c_str()); resultReceiver_.append("error: option "); resultReceiver_.append("requires a value.\n"); @@ -1359,6 +1389,20 @@ ErrCode AbilityManagerShellCommand::MakeWantFromCmd(Want& want, std::string& win break; } + case 'U': { + // 'aa start -U xxx' + + // save URI + uri = optarg; + break; + } + case 'A': { + // 'aa start -A xxx' + + // save action + action = optarg; + break; + } case 'C': { // 'aa start -C' // cold start app @@ -1440,6 +1484,12 @@ ErrCode AbilityManagerShellCommand::MakeWantFromCmd(Want& want, std::string& win want.SetParam(it->first, it->second); } } + if (!action.empty()) { + want.SetAction(action); + } + if (!uri.empty()) { + want.SetUri(uri); + } } } -- Gitee From a271c98992ff637ab9886e9f15eba7d037cfb388 Mon Sep 17 00:00:00 2001 From: yangxuguang-huawei Date: Fri, 12 Jan 2024 10:38:38 +0800 Subject: [PATCH 08/20] fix: " -> ' Signed-off-by: yangxuguang-huawei --- tools/aa/src/ability_command.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/aa/src/ability_command.cpp b/tools/aa/src/ability_command.cpp index bf5adaf266a..e4a0a43b93e 100644 --- a/tools/aa/src/ability_command.cpp +++ b/tools/aa/src/ability_command.cpp @@ -48,8 +48,8 @@ constexpr struct option LONG_OPTIONS[] = { {"debug", no_argument, nullptr, 'D'}, {"native-debug", no_argument, nullptr, 'N'}, {"parameters", required_argument, nullptr, 'P'}, - {"action", required_argument, nullptr, "A"}, - {"URI", required_argument, nullptr, "U"}, + {"action", required_argument, nullptr, 'A'}, + {"URI", required_argument, nullptr, 'U'}, {nullptr, 0, nullptr, 0}, }; const std::string SHORT_OPTIONS_APPLICATION_NOT_RESPONDING = "hp:"; -- Gitee From 7e8a320a411cd9db44eafb5f1d44e81671da26a1 Mon Sep 17 00:00:00 2001 From: yangxuguang-huawei Date: Sat, 13 Jan 2024 10:18:08 +0800 Subject: [PATCH 09/20] refactor: per static check Signed-off-by: yangxuguang-huawei --- tools/aa/src/ability_command.cpp | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/tools/aa/src/ability_command.cpp b/tools/aa/src/ability_command.cpp index e4a0a43b93e..873607322d0 100644 --- a/tools/aa/src/ability_command.cpp +++ b/tools/aa/src/ability_command.cpp @@ -34,6 +34,7 @@ namespace OHOS { namespace AAFwk { namespace { constexpr size_t PARAM_LENGTH = 1024; +constexpr size_t VALID_KV_VECTOR_SIZE = 2; const std::string SHORT_OPTIONS = "ch:d:a:b:p:s:m:P:A:U:CDSN"; constexpr struct option LONG_OPTIONS[] = { @@ -815,8 +816,7 @@ std::vector Split(const std::string& str, char delim) std::stringstream ss(str); std::string item; std::vector elems; - while (std::getline(ss, item, delim)) - { + while (std::getline(ss, item, delim)) { if (!item.empty()) elems.push_back(item); } @@ -826,10 +826,18 @@ std::vector Split(const std::string& str, char delim) void ParseToKeyValuePairs(const std::string& str, std::map& m) { auto vs = Split(str, ';'); - for (auto s : vs) - { + for (auto s : vs) { auto v = Split(s, ':'); - if (v.size() == 2) m[v[0]] = v[1]; + if (v.size() == VALID_KV_VECTOR_SIZE) + m[v[0]] = v[1]; + } +} + +void SetParamWithMap(const std::map& m, Want& want) +{ + std::map::iterator it; + for (it = m.begin(); it != m.end(); it++) { + want.SetParam(it->first, it->second); } } @@ -1251,7 +1259,7 @@ ErrCode AbilityManagerShellCommand::MakeWantFromCmd(Want& want, std::string& win break; } case 'P': { - if (strcmp(cmd_.c_str(), "start") == 0) { + if (cmd_ == "start") { // parameters are only valid for the start command // 'aa start -P' with no argumnet HILOG_INFO("'aa %{public}s -P' with no argument.", cmd_.c_str()); @@ -1264,7 +1272,7 @@ ErrCode AbilityManagerShellCommand::MakeWantFromCmd(Want& want, std::string& win break; } case 'A': { - if (strcmp(cmd_.c_str(), "start") == 0) { + if (cmd_ == "start") { // action is only valid for the start command // 'aa start -A' with no argumnet HILOG_INFO("'aa %{public}s -A' with no argument.", cmd_.c_str()); @@ -1277,7 +1285,7 @@ ErrCode AbilityManagerShellCommand::MakeWantFromCmd(Want& want, std::string& win break; } case 'U': { - if (strcmp(cmd_.c_str(), "start") == 0) { + if (cmd_ == "start") { // URI is only valid for the start command // 'aa start -U' with no argumnet HILOG_INFO("'aa %{public}s -U' with no argument.", cmd_.c_str()); @@ -1480,9 +1488,7 @@ ErrCode AbilityManagerShellCommand::MakeWantFromCmd(Want& want, std::string& win want.SetParam("nativeDebug", isNativeDebug); } if (!parameters.empty()) { - for (std::map::iterator it = parameters.begin(); it != parameters.end(); it++) { - want.SetParam(it->first, it->second); - } + SetParamWithMap(parameters, want); } if (!action.empty()) { want.SetAction(action); -- Gitee From 74a503b562bd0d6d1b157a8bf9ef61b3f0630e86 Mon Sep 17 00:00:00 2001 From: yangxuguang-huawei Date: Sat, 13 Jan 2024 10:33:19 +0800 Subject: [PATCH 10/20] fix: per compile error Signed-off-by: yangxuguang-huawei --- tools/aa/src/ability_command.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tools/aa/src/ability_command.cpp b/tools/aa/src/ability_command.cpp index 873607322d0..0e4ac37481b 100644 --- a/tools/aa/src/ability_command.cpp +++ b/tools/aa/src/ability_command.cpp @@ -835,8 +835,7 @@ void ParseToKeyValuePairs(const std::string& str, std::map& m, Want& want) { - std::map::iterator it; - for (it = m.begin(); it != m.end(); it++) { + for (auto it = m.begin(); it != m.end(); it++) { want.SetParam(it->first, it->second); } } -- Gitee From 4cfbe362688db6fc9e7813e32675a20b07851599 Mon Sep 17 00:00:00 2001 From: yangxuguang-huawei Date: Sat, 13 Jan 2024 11:16:01 +0800 Subject: [PATCH 11/20] fix: per static check errors Signed-off-by: yangxuguang-huawei --- tools/aa/src/ability_command.cpp | 42 ++++++++++++++------------------ 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/tools/aa/src/ability_command.cpp b/tools/aa/src/ability_command.cpp index 0e4ac37481b..84f431fa45d 100644 --- a/tools/aa/src/ability_command.cpp +++ b/tools/aa/src/ability_command.cpp @@ -1258,42 +1258,36 @@ ErrCode AbilityManagerShellCommand::MakeWantFromCmd(Want& want, std::string& win break; } case 'P': { - if (cmd_ == "start") { - // parameters are only valid for the start command - // 'aa start -P' with no argumnet - HILOG_INFO("'aa %{public}s -P' with no argument.", cmd_.c_str()); + // 'aa start -P' with no argumnet + HILOG_INFO("'aa %{public}s -P' with no argument.", cmd_.c_str()); - resultReceiver_.append("error: option "); - resultReceiver_.append("requires a value.\n"); + resultReceiver_.append("error: option "); + resultReceiver_.append("requires a value.\n"); + + result = OHOS::ERR_INVALID_VALUE; - result = OHOS::ERR_INVALID_VALUE; - } break; } case 'A': { - if (cmd_ == "start") { - // action is only valid for the start command - // 'aa start -A' with no argumnet - HILOG_INFO("'aa %{public}s -A' with no argument.", cmd_.c_str()); + // 'aa start -A' with no argumnet + HILOG_INFO("'aa %{public}s -A' with no argument.", cmd_.c_str()); - resultReceiver_.append("error: option "); - resultReceiver_.append("requires a value.\n"); + resultReceiver_.append("error: option "); + resultReceiver_.append("requires a value.\n"); + + result = OHOS::ERR_INVALID_VALUE; - result = OHOS::ERR_INVALID_VALUE; - } break; } case 'U': { - if (cmd_ == "start") { - // URI is only valid for the start command - // 'aa start -U' with no argumnet - HILOG_INFO("'aa %{public}s -U' with no argument.", cmd_.c_str()); + // 'aa start -U' with no argumnet + HILOG_INFO("'aa %{public}s -U' with no argument.", cmd_.c_str()); - resultReceiver_.append("error: option "); - resultReceiver_.append("requires a value.\n"); + resultReceiver_.append("error: option "); + resultReceiver_.append("requires a value.\n"); + + result = OHOS::ERR_INVALID_VALUE; - result = OHOS::ERR_INVALID_VALUE; - } break; } case 0: { -- Gitee From 9da827cf9f6d6ac797c3f5fc8ab824a0c1cfaa83 Mon Sep 17 00:00:00 2001 From: yangxuguang-huawei Date: Thu, 18 Jan 2024 15:58:01 +0800 Subject: [PATCH 12/20] feat: add entity and type; support parameters of more types Signed-off-by: yangxuguang-huawei --- tools/aa/include/ability_command.h | 17 ++- tools/aa/src/ability_command.cpp | 224 +++++++++++++++++++++++++---- 2 files changed, 216 insertions(+), 25 deletions(-) diff --git a/tools/aa/include/ability_command.h b/tools/aa/include/ability_command.h index efac6495189..f94e7de9ed6 100644 --- a/tools/aa/include/ability_command.h +++ b/tools/aa/include/ability_command.h @@ -24,6 +24,10 @@ namespace OHOS { namespace AAFwk { namespace { +using ParametersInteger = std::map; +using ParametersString = std::map; +using ParametersBool = std::map; + const std::string TOOL_NAME = "aa"; const std::string HELP_MSG = "usage: aa \n" @@ -63,7 +67,10 @@ const std::string HELP_MSG_START = "options list:\n" " -h, --help list available commands\n" " [-d ] -a -b [-m ] [-p ] [-D] [-S] [-N] " - " [-P ] " + " [-Ps ] " + " [-Pi ] " + " [-Pb ] " + " [-Psn] " " [-A ] " " [-U ] " " start ability with an element name\n"; @@ -212,6 +219,14 @@ private: ErrCode RunAsProcessCommand(); ErrCode RunAsAttachDebugCommand(); ErrCode RunAsDetachDebugCommand(); + bool CheckParameters(int target); + ErrCode ParseParam(ParametersInteger& pi); + ErrCode ParseParam(ParametersString& ps, bool isNull); + ErrCode ParseParam(ParametersBool& pb); + ErrCode ParseParam(ParametersInteger& pi, ParametersBool& pb, ParametersString& ps); + void SetParams(const ParametersInteger& pi, Want& want); + void SetParams(const ParametersString& ps, Want& want); + void SetParams(const ParametersBool& pb, Want& want); #ifdef ABILITY_COMMAND_FOR_TEST ErrCode RunForceTimeoutForTest(); ErrCode RunAsSendAppNotRespondingProcessID(); diff --git a/tools/aa/src/ability_command.cpp b/tools/aa/src/ability_command.cpp index 84f431fa45d..2dc2bf10bb5 100644 --- a/tools/aa/src/ability_command.cpp +++ b/tools/aa/src/ability_command.cpp @@ -34,9 +34,16 @@ namespace OHOS { namespace AAFwk { namespace { constexpr size_t PARAM_LENGTH = 1024; -constexpr size_t VALID_KV_VECTOR_SIZE = 2; - -const std::string SHORT_OPTIONS = "ch:d:a:b:p:s:m:P:A:U:CDSN"; +constexpr size_t PARAMETER_VALUE_OFFSET = 1; +constexpr size_t NEXT_OPTION_OFFSET = 2; +constexpr int VALID_KEY_VALUE_PAIR_SIZE = 2; +constexpr size_t VALID_NULL_STRING_KEY_SIZE = 1; +const std::string OPTION_PARAMETERS_INTERGER = "i"; +const std::string OPTION_PARAMETERS_STRING = "s"; +const std::string OPTION_PARAMETERS_STRING_NULL = "sn"; +const std::string OPTION_PARAMETERS_BOOL = "b"; + +const std::string SHORT_OPTIONS = "ch:d:a:b:e:t:p:s:m:P::A:U:CDSN"; constexpr struct option LONG_OPTIONS[] = { {"help", no_argument, nullptr, 'h'}, {"device", required_argument, nullptr, 'd'}, @@ -51,6 +58,8 @@ constexpr struct option LONG_OPTIONS[] = { {"parameters", required_argument, nullptr, 'P'}, {"action", required_argument, nullptr, 'A'}, {"URI", required_argument, nullptr, 'U'}, + {"entity", required_argument, nullptr, 'e'}, + {"type", required_argument, nullptr, 't'}, {nullptr, 0, nullptr, 0}, }; const std::string SHORT_OPTIONS_APPLICATION_NOT_RESPONDING = "hp:"; @@ -811,35 +820,141 @@ bool AbilityManagerShellCommand::CheckPerfCmdString( return true; } -std::vector Split(const std::string& str, char delim) +bool IsNum(const std::string& s) { - std::stringstream ss(str); - std::string item; - std::vector elems; - while (std::getline(ss, item, delim)) { - if (!item.empty()) - elems.push_back(item); - } - return elems; + for (auto c : s) + if (!std::isdigit(c)) + return false; + return true; } -void ParseToKeyValuePairs(const std::string& str, std::map& m) +// parse integer parameters +ErrCode AbilityManagerShellCommand::ParseParam(ParametersInteger& pi) { - auto vs = Split(str, ';'); - for (auto s : vs) { - auto v = Split(s, ':'); - if (v.size() == VALID_KV_VECTOR_SIZE) - m[v[0]] = v[1]; + std::string key = argv_[optind]; + std::string intString = argv_[optind+1]; + if (!IsNum(intString)) { + resultReceiver_.append("invalid parameter "); + resultReceiver_.append(intString); + resultReceiver_.append(" for integer option\n"); + + return OHOS::ERR_INVALID_VALUE; } + pi[key] = atoi(argv_[optind+1]); + return OHOS::ERR_OK; +} + +// parse string parameters +ErrCode AbilityManagerShellCommand::ParseParam(ParametersString& ps, bool isNull = false) +{ + std::string key = argv_[optind]; + std::string value = ""; + if (!isNull) + value = argv_[optind + PARAMETER_VALUE_OFFSET]; + + ps[key] = value; + + return OHOS::ERR_OK; } -void SetParamWithMap(const std::map& m, Want& want) +// parse bool parameters +ErrCode AbilityManagerShellCommand::ParseParam(ParametersBool& pb) { - for (auto it = m.begin(); it != m.end(); it++) { + std::string key = argv_[optind]; + std::string boolString = argv_[optind+1]; + std::transform(boolString.begin(), boolString.end(), boolString.begin(), ::tolower); + bool value; + if (boolString == "true" || boolString == "t"){ + value = true; + } else if (boolString == "false" || boolString == "f") { + value = false; + } else { + resultReceiver_.append("invalid parameter "); + resultReceiver_.append(argv_[optind+1]); + resultReceiver_.append(" for bool option\n"); + + return OHOS::ERR_INVALID_VALUE; + } + + pb[key] = value; + + return OHOS::ERR_OK; +} + +ErrCode AbilityManagerShellCommand::ParseParam(ParametersInteger& pi, ParametersBool& pb, ParametersString& ps) { + int result = OHOS::ERR_OK; + if (std::strcmp(optarg, OPTION_PARAMETERS_INTERGER.c_str()) == 0) { + if (!CheckParameters(VALID_KEY_VALUE_PAIR_SIZE)) { + resultReceiver_.append("invalid number of parameters for option -pi\n"); + return OHOS::ERR_INVALID_VALUE; + } + result = ParseParam(pi); + optind += NEXT_OPTION_OFFSET; + return result; + } + + if (std::strcmp(optarg, OPTION_PARAMETERS_STRING.c_str()) == 0) { + if (!CheckParameters(VALID_KEY_VALUE_PAIR_SIZE)) { + resultReceiver_.append("invalid number of parameters for option -ps\n"); + return OHOS::ERR_INVALID_VALUE; + } + result = ParseParam(ps, true); + optind += NEXT_OPTION_OFFSET; + return result; + } + + if (std::strcmp(optarg, OPTION_PARAMETERS_STRING_NULL.c_str()) == 0) { + if (!CheckParameters(VALID_NULL_STRING_KEY_SIZE)) { + resultReceiver_.append("invalid number of parameters for option -psn\n"); + return OHOS::ERR_INVALID_VALUE; + } + result = ParseParam(ps); + optind++; + return result; + } + + if (std::strcmp(optarg, OPTION_PARAMETERS_BOOL.c_str()) == 0) { + if (!CheckParameters(VALID_KEY_VALUE_PAIR_SIZE)) { + resultReceiver_.append("invalid number of parameters for option -pb\n"); + return OHOS::ERR_INVALID_VALUE; + } + result = ParseParam(pb); + optind += NEXT_OPTION_OFFSET; + return result; + } + + resultReceiver_.append("invlid option: "); + resultReceiver_.append(optarg); + resultReceiver_.append("\n"); + return OHOS::ERR_INVALID_VALUE; +} + +void AbilityManagerShellCommand::SetParams(const ParametersInteger& pi, Want& want) +{ + for (auto it = pi.begin(); it != pi.end(); it++) { + want.SetParam(it->first, it->second); + } +} + +void AbilityManagerShellCommand::SetParams(const ParametersString& ps, Want& want) +{ + for (auto it = ps.begin(); it != ps.end(); it++) { + want.SetParam(it->first, it->second); + } +} + +void AbilityManagerShellCommand::SetParams(const ParametersBool& pb, Want& want) +{ + for (auto it = pb.begin(); it != pb.end(); it++) { want.SetParam(it->first, it->second); } } +void AddEntities(const std::vector& entities, Want& want) { + for (auto entity : entities) + want.AddEntity(entity); +} + ErrCode AbilityManagerShellCommand::MakeWantForProcess(Want& want) { int result = OHOS::ERR_OK; @@ -1135,6 +1250,18 @@ ErrCode AbilityManagerShellCommand::RunForceTimeoutForTest() } #endif +bool AbilityManagerShellCommand::CheckParameters(int target) +{ + if (optind + target > argc_) return false; + int index = optind; + int count = 0; + while (index < argc_ && argv_[index][0] != '-') { + count++; + index++; + } + return count == target; +} + ErrCode AbilityManagerShellCommand::MakeWantFromCmd(Want& want, std::string& windowMode) { int result = OHOS::ERR_OK; @@ -1147,9 +1274,13 @@ ErrCode AbilityManagerShellCommand::MakeWantFromCmd(Want& want, std::string& win std::string abilityName = ""; std::string moduleName; std::string perfCmd; - std::map parameters; + ParametersInteger parametersInteger; + ParametersString parametersString; + ParametersBool parametersBool; std::string uri; std::string action; + std::vector entities; + std::string typeVal; bool isColdStart = false; bool isDebugApp = false; bool isContinuation = false; @@ -1223,6 +1354,26 @@ ErrCode AbilityManagerShellCommand::MakeWantFromCmd(Want& want, std::string& win result = OHOS::ERR_INVALID_VALUE; break; } + case 'e': { + // 'aa start -e' with no argument + HILOG_INFO("'aa %{public}s -e with no argument.", cmd_.c_str()); + + resultReceiver_.append("error: option "); + resultReceiver_.append("requires a value.\n"); + + result = OHOS::ERR_INVALID_VALUE; + break; + } + case 't': { + // 'aa start -t' with no argument + HILOG_INFO("'aa %{public}s -t with no argument.", cmd_.c_str()); + + resultReceiver_.append("error: option "); + resultReceiver_.append("requires a value.\n"); + + result = OHOS::ERR_INVALID_VALUE; + break; + } case 's': { // 'aa start -s' with no argument // 'aa stop-service -s' with no argument @@ -1357,6 +1508,19 @@ ErrCode AbilityManagerShellCommand::MakeWantFromCmd(Want& want, std::string& win bundleName = optarg; break; } + case 'e': { + // 'aa start -e xxx' + + // save entity + entities.push_back(optarg); + break; + } + case 't': { + // 'aa start -t xxx' + + // save type + typeVal = optarg; + } case 's': { // 'aa start -s xxx' // save windowMode @@ -1386,7 +1550,7 @@ ErrCode AbilityManagerShellCommand::MakeWantFromCmd(Want& want, std::string& win // 'aa start -P xxx' // parse option arguments into a key-value map - ParseToKeyValuePairs(optarg, parameters); + result = ParseParam(parametersInteger, parametersBool, parametersString); break; } @@ -1480,8 +1644,14 @@ ErrCode AbilityManagerShellCommand::MakeWantFromCmd(Want& want, std::string& win if (isNativeDebug) { want.SetParam("nativeDebug", isNativeDebug); } - if (!parameters.empty()) { - SetParamWithMap(parameters, want); + if (!parametersInteger.empty()) { + SetParams(parametersInteger, want); + } + if (!parametersBool.empty()) { + SetParams(parametersBool, want); + } + if(!parametersString.empty()) { + SetParams(parametersString, want); } if (!action.empty()) { want.SetAction(action); @@ -1489,6 +1659,12 @@ ErrCode AbilityManagerShellCommand::MakeWantFromCmd(Want& want, std::string& win if (!uri.empty()) { want.SetUri(uri); } + if (!entities.empty()) { + AddEntities(entities, want); + } + if (!typeVal.empty()) { + want.SetType(typeVal); + } } } -- Gitee From edc9d0f951d7f6b88f035732138688a6a16763ed Mon Sep 17 00:00:00 2001 From: yangxuguang-huawei Date: Thu, 18 Jan 2024 16:01:08 +0800 Subject: [PATCH 13/20] refactor: add entity and type docstrings to help Signed-off-by: yangxuguang-huawei --- tools/aa/include/ability_command.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/aa/include/ability_command.h b/tools/aa/include/ability_command.h index f94e7de9ed6..cc1c0d166f3 100644 --- a/tools/aa/include/ability_command.h +++ b/tools/aa/include/ability_command.h @@ -73,6 +73,8 @@ const std::string HELP_MSG_START = " [-Psn] " " [-A ] " " [-U ] " + " [-e ] " + " [-t ] " " start ability with an element name\n"; const std::string HELP_MSG_STOP_SERVICE = -- Gitee From 8add187026005391b412b2cf3d4bcff1e09c346f Mon Sep 17 00:00:00 2001 From: yangxuguang-huawei Date: Thu, 18 Jan 2024 16:57:29 +0800 Subject: [PATCH 14/20] fix: per clean code check Signed-off-by: yangxuguang-huawei --- tools/aa/src/ability_command.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tools/aa/src/ability_command.cpp b/tools/aa/src/ability_command.cpp index 2dc2bf10bb5..288a58362e3 100644 --- a/tools/aa/src/ability_command.cpp +++ b/tools/aa/src/ability_command.cpp @@ -864,7 +864,7 @@ ErrCode AbilityManagerShellCommand::ParseParam(ParametersBool& pb) std::string boolString = argv_[optind+1]; std::transform(boolString.begin(), boolString.end(), boolString.begin(), ::tolower); bool value; - if (boolString == "true" || boolString == "t"){ + if (boolString == "true" || boolString == "t") { value = true; } else if (boolString == "false" || boolString == "f") { value = false; @@ -881,7 +881,8 @@ ErrCode AbilityManagerShellCommand::ParseParam(ParametersBool& pb) return OHOS::ERR_OK; } -ErrCode AbilityManagerShellCommand::ParseParam(ParametersInteger& pi, ParametersBool& pb, ParametersString& ps) { +ErrCode AbilityManagerShellCommand::ParseParam(ParametersInteger& pi, ParametersBool& pb, ParametersString& ps) +{ int result = OHOS::ERR_OK; if (std::strcmp(optarg, OPTION_PARAMETERS_INTERGER.c_str()) == 0) { if (!CheckParameters(VALID_KEY_VALUE_PAIR_SIZE)) { @@ -950,7 +951,8 @@ void AbilityManagerShellCommand::SetParams(const ParametersBool& pb, Want& want) } } -void AddEntities(const std::vector& entities, Want& want) { +void AddEntities(const std::vector& entities, Want& want) +{ for (auto entity : entities) want.AddEntity(entity); } @@ -1650,7 +1652,7 @@ ErrCode AbilityManagerShellCommand::MakeWantFromCmd(Want& want, std::string& win if (!parametersBool.empty()) { SetParams(parametersBool, want); } - if(!parametersString.empty()) { + if (!parametersString.empty()) { SetParams(parametersString, want); } if (!action.empty()) { -- Gitee From 1ecaee4bad7174aeaa5fb34f16508746814cd1fd Mon Sep 17 00:00:00 2001 From: yangxuguang-huawei Date: Thu, 18 Jan 2024 17:38:08 +0800 Subject: [PATCH 15/20] refactor: docstring for -Psn option Signed-off-by: yangxuguang-huawei --- tools/aa/include/ability_command.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/aa/include/ability_command.h b/tools/aa/include/ability_command.h index cc1c0d166f3..1bc830bbc8b 100644 --- a/tools/aa/include/ability_command.h +++ b/tools/aa/include/ability_command.h @@ -70,7 +70,7 @@ const std::string HELP_MSG_START = " [-Ps ] " " [-Pi ] " " [-Pb ] " - " [-Psn] " + " [-Psn ] " " [-A ] " " [-U ] " " [-e ] " -- Gitee From e0f9b68bcd169131fb96fd5e6bf196a706d88ab0 Mon Sep 17 00:00:00 2001 From: yangxuguang-huawei Date: Thu, 18 Jan 2024 17:41:37 +0800 Subject: [PATCH 16/20] refactor: error message for options Signed-off-by: yangxuguang-huawei --- tools/aa/src/ability_command.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tools/aa/src/ability_command.cpp b/tools/aa/src/ability_command.cpp index 288a58362e3..a14c278eeb1 100644 --- a/tools/aa/src/ability_command.cpp +++ b/tools/aa/src/ability_command.cpp @@ -886,7 +886,7 @@ ErrCode AbilityManagerShellCommand::ParseParam(ParametersInteger& pi, Parameters int result = OHOS::ERR_OK; if (std::strcmp(optarg, OPTION_PARAMETERS_INTERGER.c_str()) == 0) { if (!CheckParameters(VALID_KEY_VALUE_PAIR_SIZE)) { - resultReceiver_.append("invalid number of parameters for option -pi\n"); + resultReceiver_.append("invalid number of parameters for option -Pi\n"); return OHOS::ERR_INVALID_VALUE; } result = ParseParam(pi); @@ -896,7 +896,7 @@ ErrCode AbilityManagerShellCommand::ParseParam(ParametersInteger& pi, Parameters if (std::strcmp(optarg, OPTION_PARAMETERS_STRING.c_str()) == 0) { if (!CheckParameters(VALID_KEY_VALUE_PAIR_SIZE)) { - resultReceiver_.append("invalid number of parameters for option -ps\n"); + resultReceiver_.append("invalid number of parameters for option -Ps\n"); return OHOS::ERR_INVALID_VALUE; } result = ParseParam(ps, true); @@ -906,7 +906,7 @@ ErrCode AbilityManagerShellCommand::ParseParam(ParametersInteger& pi, Parameters if (std::strcmp(optarg, OPTION_PARAMETERS_STRING_NULL.c_str()) == 0) { if (!CheckParameters(VALID_NULL_STRING_KEY_SIZE)) { - resultReceiver_.append("invalid number of parameters for option -psn\n"); + resultReceiver_.append("invalid number of parameters for option -Psn\n"); return OHOS::ERR_INVALID_VALUE; } result = ParseParam(ps); @@ -916,7 +916,7 @@ ErrCode AbilityManagerShellCommand::ParseParam(ParametersInteger& pi, Parameters if (std::strcmp(optarg, OPTION_PARAMETERS_BOOL.c_str()) == 0) { if (!CheckParameters(VALID_KEY_VALUE_PAIR_SIZE)) { - resultReceiver_.append("invalid number of parameters for option -pb\n"); + resultReceiver_.append("invalid number of parameters for option -Pb\n"); return OHOS::ERR_INVALID_VALUE; } result = ParseParam(pb); -- Gitee From 3656cf8f01e2b20026b785f6d2e73063d84b4d14 Mon Sep 17 00:00:00 2001 From: yangxuguang-huawei Date: Thu, 18 Jan 2024 20:15:22 +0800 Subject: [PATCH 17/20] fix: add break; Signed-off-by: yangxuguang-huawei --- tools/aa/src/ability_command.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/aa/src/ability_command.cpp b/tools/aa/src/ability_command.cpp index a14c278eeb1..d69aedd494d 100644 --- a/tools/aa/src/ability_command.cpp +++ b/tools/aa/src/ability_command.cpp @@ -1522,6 +1522,7 @@ ErrCode AbilityManagerShellCommand::MakeWantFromCmd(Want& want, std::string& win // save type typeVal = optarg; + break; } case 's': { // 'aa start -s xxx' -- Gitee From cb9ce4b73153c4ba0e1c3ac78421ce57bffef1fb Mon Sep 17 00:00:00 2001 From: yangxuguang-huawei Date: Fri, 19 Jan 2024 10:24:27 +0800 Subject: [PATCH 18/20] refactor: help message Signed-off-by: yangxuguang-huawei --- tools/aa/include/ability_command.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/aa/include/ability_command.h b/tools/aa/include/ability_command.h index 1bc830bbc8b..35292612476 100644 --- a/tools/aa/include/ability_command.h +++ b/tools/aa/include/ability_command.h @@ -67,9 +67,9 @@ const std::string HELP_MSG_START = "options list:\n" " -h, --help list available commands\n" " [-d ] -a -b [-m ] [-p ] [-D] [-S] [-N] " - " [-Ps ] " - " [-Pi ] " - " [-Pb ] " + " [-Ps ] " + " [-Pi ] " + " [-Pb ] " " [-Psn ] " " [-A ] " " [-U ] " -- Gitee From 4799676df950e38ff13fc05da79c831a947032f6 Mon Sep 17 00:00:00 2001 From: yangxuguang-huawei Date: Fri, 19 Jan 2024 16:32:19 +0800 Subject: [PATCH 19/20] refactor based on the optind behaviour Signed-off-by: yangxuguang-huawei --- tools/aa/include/ability_command.h | 9 +- tools/aa/src/ability_command.cpp | 210 +++++++++++++++++------------ 2 files changed, 128 insertions(+), 91 deletions(-) diff --git a/tools/aa/include/ability_command.h b/tools/aa/include/ability_command.h index 35292612476..cc36af79b95 100644 --- a/tools/aa/include/ability_command.h +++ b/tools/aa/include/ability_command.h @@ -67,10 +67,10 @@ const std::string HELP_MSG_START = "options list:\n" " -h, --help list available commands\n" " [-d ] -a -b [-m ] [-p ] [-D] [-S] [-N] " - " [-Ps ] " - " [-Pi ] " - " [-Pb ] " - " [-Psn ] " + " [--ps ] " + " [--pi ] " + " [--pb ] " + " [--psn ] " " [-A ] " " [-U ] " " [-e ] " @@ -225,7 +225,6 @@ private: ErrCode ParseParam(ParametersInteger& pi); ErrCode ParseParam(ParametersString& ps, bool isNull); ErrCode ParseParam(ParametersBool& pb); - ErrCode ParseParam(ParametersInteger& pi, ParametersBool& pb, ParametersString& ps); void SetParams(const ParametersInteger& pi, Want& want); void SetParams(const ParametersString& ps, Want& want); void SetParams(const ParametersBool& pb, Want& want); diff --git a/tools/aa/src/ability_command.cpp b/tools/aa/src/ability_command.cpp index d69aedd494d..1588b8bb5bb 100644 --- a/tools/aa/src/ability_command.cpp +++ b/tools/aa/src/ability_command.cpp @@ -34,16 +34,16 @@ namespace OHOS { namespace AAFwk { namespace { constexpr size_t PARAM_LENGTH = 1024; -constexpr size_t PARAMETER_VALUE_OFFSET = 1; -constexpr size_t NEXT_OPTION_OFFSET = 2; -constexpr int VALID_KEY_VALUE_PAIR_SIZE = 2; -constexpr size_t VALID_NULL_STRING_KEY_SIZE = 1; -const std::string OPTION_PARAMETERS_INTERGER = "i"; -const std::string OPTION_PARAMETERS_STRING = "s"; -const std::string OPTION_PARAMETERS_STRING_NULL = "sn"; -const std::string OPTION_PARAMETERS_BOOL = "b"; - -const std::string SHORT_OPTIONS = "ch:d:a:b:e:t:p:s:m:P::A:U:CDSN"; +constexpr int EXTRA_ARGUMENTS_FOR_KEY_VALUE_PAIR = 1; +constexpr int EXTRA_ARGUMENTS_FOR_NULL_STRING = 0; +constexpr int OPTION_PARAMETER_VALUE_OFFSET = 1; + +constexpr int OPTION_PARAMETER_INTEGER = 257; +constexpr int OPTION_PARAMETER_STRING = 258; +constexpr int OPTION_PARAMETER_BOOL = 259; +constexpr int OPTION_PARAMETER_NULL_STRING = 260; + +const std::string SHORT_OPTIONS = "ch:d:a:b:e:t:p:s:m:A:U:CDSN"; constexpr struct option LONG_OPTIONS[] = { {"help", no_argument, nullptr, 'h'}, {"device", required_argument, nullptr, 'd'}, @@ -55,11 +55,14 @@ constexpr struct option LONG_OPTIONS[] = { {"cold-start", no_argument, nullptr, 'C'}, {"debug", no_argument, nullptr, 'D'}, {"native-debug", no_argument, nullptr, 'N'}, - {"parameters", required_argument, nullptr, 'P'}, {"action", required_argument, nullptr, 'A'}, {"URI", required_argument, nullptr, 'U'}, {"entity", required_argument, nullptr, 'e'}, {"type", required_argument, nullptr, 't'}, + {"pi", required_argument, nullptr, OPTION_PARAMETER_INTEGER}, + {"ps", required_argument, nullptr, OPTION_PARAMETER_STRING}, + {"pb", required_argument, nullptr, OPTION_PARAMETER_BOOL}, + {"psn", required_argument, nullptr, OPTION_PARAMETER_NULL_STRING}, {nullptr, 0, nullptr, 0}, }; const std::string SHORT_OPTIONS_APPLICATION_NOT_RESPONDING = "hp:"; @@ -828,11 +831,23 @@ bool IsNum(const std::string& s) return true; } +bool AbilityManagerShellCommand::CheckParameters(int extraArguments) +{ + if (optind + extraArguments >= argc_) return false; + int index = optind + 1; // optind is the index of 'start' which is right behind optarg + int count = 0; + while (index < argc_ && argv_[index][0] != '-') { + count++; + index++; + } + return count == extraArguments; +} + // parse integer parameters ErrCode AbilityManagerShellCommand::ParseParam(ParametersInteger& pi) { - std::string key = argv_[optind]; - std::string intString = argv_[optind+1]; + std::string key = optarg; + std::string intString = argv_[optind + OPTION_PARAMETER_VALUE_OFFSET]; if (!IsNum(intString)) { resultReceiver_.append("invalid parameter "); resultReceiver_.append(intString); @@ -840,17 +855,17 @@ ErrCode AbilityManagerShellCommand::ParseParam(ParametersInteger& pi) return OHOS::ERR_INVALID_VALUE; } - pi[key] = atoi(argv_[optind+1]); + pi[key] = atoi(argv_[optind + OPTION_PARAMETER_VALUE_OFFSET]); return OHOS::ERR_OK; } // parse string parameters ErrCode AbilityManagerShellCommand::ParseParam(ParametersString& ps, bool isNull = false) { - std::string key = argv_[optind]; + std::string key = optarg; std::string value = ""; if (!isNull) - value = argv_[optind + PARAMETER_VALUE_OFFSET]; + value = argv_[optind + OPTION_PARAMETER_VALUE_OFFSET]; ps[key] = value; @@ -860,8 +875,8 @@ ErrCode AbilityManagerShellCommand::ParseParam(ParametersString& ps, bool isNull // parse bool parameters ErrCode AbilityManagerShellCommand::ParseParam(ParametersBool& pb) { - std::string key = argv_[optind]; - std::string boolString = argv_[optind+1]; + std::string key = optarg; + std::string boolString = argv_[optind + OPTION_PARAMETER_VALUE_OFFSET]; std::transform(boolString.begin(), boolString.end(), boolString.begin(), ::tolower); bool value; if (boolString == "true" || boolString == "t") { @@ -870,7 +885,7 @@ ErrCode AbilityManagerShellCommand::ParseParam(ParametersBool& pb) value = false; } else { resultReceiver_.append("invalid parameter "); - resultReceiver_.append(argv_[optind+1]); + resultReceiver_.append(argv_[optind + OPTION_PARAMETER_VALUE_OFFSET]); resultReceiver_.append(" for bool option\n"); return OHOS::ERR_INVALID_VALUE; @@ -881,55 +896,6 @@ ErrCode AbilityManagerShellCommand::ParseParam(ParametersBool& pb) return OHOS::ERR_OK; } -ErrCode AbilityManagerShellCommand::ParseParam(ParametersInteger& pi, ParametersBool& pb, ParametersString& ps) -{ - int result = OHOS::ERR_OK; - if (std::strcmp(optarg, OPTION_PARAMETERS_INTERGER.c_str()) == 0) { - if (!CheckParameters(VALID_KEY_VALUE_PAIR_SIZE)) { - resultReceiver_.append("invalid number of parameters for option -Pi\n"); - return OHOS::ERR_INVALID_VALUE; - } - result = ParseParam(pi); - optind += NEXT_OPTION_OFFSET; - return result; - } - - if (std::strcmp(optarg, OPTION_PARAMETERS_STRING.c_str()) == 0) { - if (!CheckParameters(VALID_KEY_VALUE_PAIR_SIZE)) { - resultReceiver_.append("invalid number of parameters for option -Ps\n"); - return OHOS::ERR_INVALID_VALUE; - } - result = ParseParam(ps, true); - optind += NEXT_OPTION_OFFSET; - return result; - } - - if (std::strcmp(optarg, OPTION_PARAMETERS_STRING_NULL.c_str()) == 0) { - if (!CheckParameters(VALID_NULL_STRING_KEY_SIZE)) { - resultReceiver_.append("invalid number of parameters for option -Psn\n"); - return OHOS::ERR_INVALID_VALUE; - } - result = ParseParam(ps); - optind++; - return result; - } - - if (std::strcmp(optarg, OPTION_PARAMETERS_BOOL.c_str()) == 0) { - if (!CheckParameters(VALID_KEY_VALUE_PAIR_SIZE)) { - resultReceiver_.append("invalid number of parameters for option -Pb\n"); - return OHOS::ERR_INVALID_VALUE; - } - result = ParseParam(pb); - optind += NEXT_OPTION_OFFSET; - return result; - } - - resultReceiver_.append("invlid option: "); - resultReceiver_.append(optarg); - resultReceiver_.append("\n"); - return OHOS::ERR_INVALID_VALUE; -} - void AbilityManagerShellCommand::SetParams(const ParametersInteger& pi, Want& want) { for (auto it = pi.begin(); it != pi.end(); it++) { @@ -1252,18 +1218,6 @@ ErrCode AbilityManagerShellCommand::RunForceTimeoutForTest() } #endif -bool AbilityManagerShellCommand::CheckParameters(int target) -{ - if (optind + target > argc_) return false; - int index = optind; - int count = 0; - while (index < argc_ && argv_[index][0] != '-') { - count++; - index++; - } - return count == target; -} - ErrCode AbilityManagerShellCommand::MakeWantFromCmd(Want& want, std::string& windowMode) { int result = OHOS::ERR_OK; @@ -1410,9 +1364,20 @@ ErrCode AbilityManagerShellCommand::MakeWantFromCmd(Want& want, std::string& win result = OHOS::ERR_INVALID_VALUE; break; } - case 'P': { - // 'aa start -P' with no argumnet - HILOG_INFO("'aa %{public}s -P' with no argument.", cmd_.c_str()); + case OPTION_PARAMETER_INTEGER: { + // 'aa start --pi' with no argumnet + HILOG_INFO("'aa %{public}s --pi' with no argument.", cmd_.c_str()); + + resultReceiver_.append("error: option "); + resultReceiver_.append("requires a value.\n"); + + result = OHOS::ERR_INVALID_VALUE; + + break; + } + case OPTION_PARAMETER_STRING: { + // 'aa start --ps' with no argumnet + HILOG_INFO("'aa %{public}s --ps' with no argument.", cmd_.c_str()); resultReceiver_.append("error: option "); resultReceiver_.append("requires a value.\n"); @@ -1421,6 +1386,29 @@ ErrCode AbilityManagerShellCommand::MakeWantFromCmd(Want& want, std::string& win break; } + case OPTION_PARAMETER_BOOL: { + // 'aa start --pb' with no argumnet + HILOG_INFO("'aa %{public}s -pb' with no argument.", cmd_.c_str()); + + resultReceiver_.append("error: option "); + resultReceiver_.append("requires a value.\n"); + + result = OHOS::ERR_INVALID_VALUE; + + break; + } + case OPTION_PARAMETER_NULL_STRING: { + // 'aa start --psn' with no argumnet + HILOG_INFO("'aa %{public}s --psn' with no argument.", cmd_.c_str()); + + resultReceiver_.append("error: option "); + resultReceiver_.append("requires a value.\n"); + + result = OHOS::ERR_INVALID_VALUE; + + break; + } + case 'A': { // 'aa start -A' with no argumnet HILOG_INFO("'aa %{public}s -A' with no argument.", cmd_.c_str()); @@ -1549,11 +1537,61 @@ ErrCode AbilityManagerShellCommand::MakeWantFromCmd(Want& want, std::string& win } break; } - case 'P': { - // 'aa start -P xxx' + case OPTION_PARAMETER_INTEGER: { + // 'aa start --pi xxx' + if (!CheckParameters(EXTRA_ARGUMENTS_FOR_KEY_VALUE_PAIR)) { + resultReceiver_.append("invalid number of parameters for option --pi\n"); + result = OHOS::ERR_INVALID_VALUE; + break; + } + + // parse option arguments into a key-value map + result = ParseParam(parametersInteger); + + optind++; + + break; + } + case OPTION_PARAMETER_STRING: { + // 'aa start --ps xxx' + if (!CheckParameters(EXTRA_ARGUMENTS_FOR_KEY_VALUE_PAIR)) { + resultReceiver_.append("invalid number of parameters for option --ps\n"); + result = OHOS::ERR_INVALID_VALUE; + break; + } + + // parse option arguments into a key-value map + result = ParseParam(parametersString); + + optind++; + + break; + } + case OPTION_PARAMETER_BOOL: { + // 'aa start --pb xxx' + if (!CheckParameters(EXTRA_ARGUMENTS_FOR_KEY_VALUE_PAIR)) { + resultReceiver_.append("invalid number of parameters for option --pb\n"); + result = OHOS::ERR_INVALID_VALUE; + break; + } + + // parse option arguments into a key-value map + result = ParseParam(parametersBool); + + optind++; + + break; + } + case OPTION_PARAMETER_NULL_STRING: { + // 'aa start --psn xxx' + if (!CheckParameters(EXTRA_ARGUMENTS_FOR_NULL_STRING)) { + resultReceiver_.append("invalid number of parameters for option --psn\n"); + result = OHOS::ERR_INVALID_VALUE; + break; + } // parse option arguments into a key-value map - result = ParseParam(parametersInteger, parametersBool, parametersString); + result = ParseParam(parametersString, true); break; } -- Gitee From 9bb715c9886d803ee83eff85de956bc9091628e3 Mon Sep 17 00:00:00 2001 From: yangxuguang-huawei Date: Fri, 19 Jan 2024 19:03:01 +0800 Subject: [PATCH 20/20] refactor: remove space at the end of line Signed-off-by: yangxuguang-huawei --- tools/aa/src/ability_command.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/aa/src/ability_command.cpp b/tools/aa/src/ability_command.cpp index 1588b8bb5bb..c4979a43205 100644 --- a/tools/aa/src/ability_command.cpp +++ b/tools/aa/src/ability_command.cpp @@ -60,7 +60,7 @@ constexpr struct option LONG_OPTIONS[] = { {"entity", required_argument, nullptr, 'e'}, {"type", required_argument, nullptr, 't'}, {"pi", required_argument, nullptr, OPTION_PARAMETER_INTEGER}, - {"ps", required_argument, nullptr, OPTION_PARAMETER_STRING}, + {"ps", required_argument, nullptr, OPTION_PARAMETER_STRING}, {"pb", required_argument, nullptr, OPTION_PARAMETER_BOOL}, {"psn", required_argument, nullptr, OPTION_PARAMETER_NULL_STRING}, {nullptr, 0, nullptr, 0}, -- Gitee