• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "ability_command.h"
17 
18 #include <getopt.h>
19 #include "ability_manager_client.h"
20 #include "hilog_wrapper.h"
21 
22 using namespace OHOS::AppExecFwk;
23 
24 namespace OHOS {
25 namespace AAFwk {
26 namespace {
27 const std::string SHORT_OPTIONS = "hd:a:b:p:s:";
28 const struct option LONG_OPTIONS[] = {
29     {"help", no_argument, nullptr, 'h'},
30     {"device", required_argument, nullptr, 'd'},
31     {"ability", required_argument, nullptr, 'a'},
32     {"bundle", required_argument, nullptr, 'b'},
33     {"power", required_argument, nullptr, 'p'},
34     {"setting", required_argument, nullptr, 's'},
35 };
36 
37 const std::string SHORT_OPTIONS_DUMP = "has:m:lud::e::";
38 const struct option LONG_OPTIONS_DUMP[] = {
39     {"help", no_argument, nullptr, 'h'},
40     {"all", no_argument, nullptr, 'a'},
41     {"stack", required_argument, nullptr, 's'},
42     {"mission", required_argument, nullptr, 'm'},
43     {"stack-list", no_argument, nullptr, 'l'},
44     {"ui", no_argument, nullptr, 'u'},
45     {"data", no_argument, nullptr, 'd'},
46     {"serv", no_argument, nullptr, 'e'},
47 };
48 }  // namespace
49 
AbilityManagerShellCommand(int argc,char * argv[])50 AbilityManagerShellCommand::AbilityManagerShellCommand(int argc, char *argv[]) : ShellCommand(argc, argv, TOOL_NAME)
51 {}
52 
CreateCommandMap()53 ErrCode AbilityManagerShellCommand::CreateCommandMap()
54 {
55     commandMap_ = {
56         {"help", std::bind(&AbilityManagerShellCommand::RunAsHelpCommand, this)},
57         {"screen", std::bind(&AbilityManagerShellCommand::RunAsScreenCommand, this)},
58         {"start", std::bind(&AbilityManagerShellCommand::RunAsStartAbility, this)},
59         {"stop-service", std::bind(&AbilityManagerShellCommand::RunAsStopService, this)},
60         {"dump", std::bind(&AbilityManagerShellCommand::RunAsDumpCommand, this)},
61     };
62 
63     return OHOS::ERR_OK;
64 }
65 
CreateMessageMap()66 ErrCode AbilityManagerShellCommand::CreateMessageMap()
67 {
68     messageMap_ = {
69         //  code + message
70         {
71             RESOLVE_ABILITY_ERR,
72             "error: resolve ability err.",
73         },
74         {
75             GET_ABILITY_SERVICE_FAILED,
76             "error: get ability service failed.",
77         },
78         {
79             ABILITY_SERVICE_NOT_CONNECTED,
80             "error: ability service not connected.",
81         },
82         {
83             RESOLVE_APP_ERR,
84             "error: resolve app err.",
85         },
86         {
87             STACK_MANAGER_NOT_EXIST,
88             "error: stack manager not exist.",
89         },
90         {
91             ABILITY_EXISTED,
92             "error: ability existed.",
93         },
94         {
95             CREATE_MISSION_STACK_FAILED,
96             "error: create mission stack failed.",
97         },
98         {
99             CREATE_MISSION_RECORD_FAILED,
100             "error: create mission record failed.",
101         },
102         {
103             CREATE_ABILITY_RECORD_FAILED,
104             "error: create ability record failed.",
105         },
106         {
107             START_ABILITY_WAITING,
108             "error: start ability waiting.",
109         },
110         {
111             TERMINATE_LAUNCHER_DENIED,
112             "error: terminate launcher denied.",
113         },
114         {
115             CONNECTION_NOT_EXIST,
116             "error: connection not exist.",
117         },
118         {
119             INVALID_CONNECTION_STATE,
120             "error: invalid connection state.",
121         },
122         {
123             LOAD_ABILITY_TIMEOUT,
124             "error: load ability timeout.",
125         },
126         {
127             CONNECTION_TIMEOUT,
128             "error: connection timeout.",
129         },
130         {
131             GET_BUNDLE_MANAGER_SERVICE_FAILED,
132             "error: get bundle manager service failed.",
133         },
134         {
135             REMOVE_MISSION_ID_NOT_EXIST,
136             "error: remove mission id not exist.",
137         },
138         {
139             REMOVE_MISSION_LAUNCHER_DENIED,
140             "error: remove mission launcher denied.",
141         },
142         {
143             REMOVE_MISSION_ACTIVE_DENIED,
144             "error: remove mission active denied.",
145         },
146         {
147             REMOVE_MISSION_FAILED,
148             "error: remove mission failed.",
149         },
150         {
151             INNER_ERR,
152             "error: inner err.",
153         },
154         {
155             GET_RECENT_MISSIONS_FAILED,
156             "error: get recent missions failed.",
157         },
158         {
159             REMOVE_STACK_LAUNCHER_DENIED,
160             "error: remove stack launcher denied.",
161         },
162         {
163             REMOVE_STACK_FAILED,
164             "error: remove stack failed.",
165         },
166         {
167             MISSION_STACK_LIST_IS_EMPTY,
168             "error: mission stack list is empty.",
169         },
170         {
171             REMOVE_STACK_ID_NOT_EXIST,
172             "error: remove stack id not exist.",
173         },
174         {
175             TARGET_ABILITY_NOT_SERVICE,
176             "error: target ability not service.",
177         },
178         {
179             TERMINATE_SERVICE_IS_CONNECTED,
180             "error: terminate service is connected.",
181         },
182         {
183             START_SERVICE_ABILITY_ACTIVING,
184             "error: start service ability activing.",
185         },
186         {
187             MOVE_MISSION_FAILED,
188             "error: move mission failed.",
189         },
190         {
191             KILL_PROCESS_FAILED,
192             "error: kill process failed.",
193         },
194         {
195             UNINSTALL_APP_FAILED,
196             "error: uninstall app failed.",
197         },
198         {
199             TERMINATE_ABILITY_RESULT_FAILED,
200             "error: terminate ability result failed.",
201         },
202         {
203             CHECK_PERMISSION_FAILED,
204             "error: check permission failed.",
205         },
206         {
207             POWER_OFF_WAITING,
208             "error: power off waiting.",
209         },
210         {
211             POWER_OFF_FAILED,
212             "error: power off failed.",
213         },
214         {
215             POWER_ON_FAILED,
216             "error: power on failed.",
217         },
218         {
219             NO_FIRST_IN_MISSION,
220             "error: no first in mission.",
221         },
222         {
223             LOCK_MISSION_DENY_FAILED,
224             "error: lock mission deny failed.",
225         },
226         {
227             UNLOCK_MISSION_DENY_FAILED,
228             "error: unlock mission deny failed.",
229         },
230         {
231             SET_MISSION_INFO_FAILED,
232             "error: set mission info failed.",
233         },
234         {
235             LOCK_MISSION_STATE_DENY_REQUEST,
236             "error: lock mission state deny request.",
237         },
238         {
239             MOVE_MISSION_TO_STACK_OUT_OF_SIZE,
240             "error: move mission to stack out of size.",
241         },
242         {
243             MOVE_MISSION_TO_STACK_NOT_SAME_WIN_MODE,
244             "error: move mission to stack not same win mode.",
245         },
246         {
247             MOVE_MISSION_TO_STACK_NOT_EXIST_MISSION,
248             "error: move mission to stack not exist mission.",
249         },
250         {
251             MOVE_MISSION_TO_STACK_NOT_SUPPORT_MULTI_WIN,
252             "error: move mission to stack not support multi win.",
253         },
254         {
255             MOVE_MISSION_TO_STACK_TARGET_STACK_OVERFLOW,
256             "error: move mission to stack target stack overflow.",
257         },
258         {
259             MOVE_MISSION_TO_STACK_MOVING_DENIED,
260             "error: move mission to stack moving denied.",
261         },
262         {
263             MINIMIZE_MULTI_WINDOW_FAILED,
264             "error: minimize multi window failed.",
265         },
266         {
267             MAXIMIZE_MULTIWINDOW_NOT_EXIST,
268             "error: maximize multiwindow not exist.",
269         },
270         {
271             MAXIMIZE_MULTIWINDOW_FAILED,
272             "error: maximize multiwindow failed.",
273         },
274         {
275             CHANGE_FOCUS_ABILITY_FAILED,
276             "error: change focus ability failed.",
277         },
278         {
279             GET_FLOATING_STACK_FAILED,
280             "error: get floating stack failed.",
281         },
282         {
283             CLOSE_MULTI_WINDOW_FAILED,
284             "error: close multi window failed.",
285         },
286         {
287             START_ABILITY_SETTING_FAILED,
288             "error: start ability setting failed.",
289         },
290         {
291             START_ABILITY_SETTING_NOT_SUPPORT_MULTI_WIN,
292             "error: start ability setting not support multi win.",
293         },
294         {
295             NO_FOUND_ABILITY_BY_CALLER,
296             "error: no found ability by caller.",
297         },
298         {
299             ABILITY_VISIBLE_FALSE_DENY_REQUEST,
300             "error: ability visible false deny request.",
301         },
302     };
303 
304     return OHOS::ERR_OK;
305 }
306 
init()307 ErrCode AbilityManagerShellCommand::init()
308 {
309     ErrCode result = AbilityManagerClient::GetInstance()->Connect();
310 
311     return result;
312 }
313 
RunAsHelpCommand()314 ErrCode AbilityManagerShellCommand::RunAsHelpCommand()
315 {
316     resultReceiver_.append(HELP_MSG);
317 
318     return OHOS::ERR_OK;
319 }
320 
RunAsScreenCommand()321 ErrCode AbilityManagerShellCommand::RunAsScreenCommand()
322 {
323     HILOG_INFO("enter");
324 
325     int result = OHOS::ERR_OK;
326 
327     int option = -1;
328     int counter = 0;
329 
330     std::string powerState = "";
331 
332     while (true) {
333         counter++;
334 
335         option = getopt_long(argc_, argv_, SHORT_OPTIONS.c_str(), LONG_OPTIONS, nullptr);
336 
337         HILOG_INFO("option: %{public}d, optopt: %{public}d, optind: %{public}d", option, optopt, optind);
338 
339         if (optind < 0 || optind > argc_) {
340             return OHOS::ERR_INVALID_VALUE;
341         }
342 
343         for (int i = 0; i < argc_; i++) {
344             HILOG_INFO("argv_[%{public}d]: %{public}s", i, argv_[i]);
345         }
346 
347         if (option == -1) {
348             if (counter == 1) {
349                 // When scanning the first argument
350                 if (strcmp(argv_[optind], cmd_.c_str()) == 0) {
351                     // 'aa screen' with no option: aa screen
352                     // 'aa screen' with a wrong argument: aa screen xxx
353                     HILOG_INFO("'aa %{public}s' %{public}s", HELP_MSG_NO_OPTION.c_str(), cmd_.c_str());
354 
355                     resultReceiver_.append(HELP_MSG_NO_OPTION + "\n");
356                     result = OHOS::ERR_INVALID_VALUE;
357                 }
358             }
359             break;
360         }
361 
362         if (option == '?') {
363             switch (optopt) {
364                 case 'p': {
365                     // 'aa screen -p' with no argument
366                     HILOG_INFO("'aa %{public}s -p' with no argument.", cmd_.c_str());
367 
368                     resultReceiver_.append("error: option ");
369                     resultReceiver_.append("requires a value.\n");
370 
371                     result = OHOS::ERR_INVALID_VALUE;
372                     break;
373                 }
374                 case 0: {
375                     // 'aa screen' with an unknown option: aa screen --x
376                     // 'aa screen' with an unknown option: aa screen --xxx
377                     std::string unknownOption = "";
378                     std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption);
379 
380                     HILOG_INFO("'aa screen' with an unknown option.");
381 
382                     resultReceiver_.append(unknownOptionMsg);
383                     result = OHOS::ERR_INVALID_VALUE;
384                     break;
385                 }
386                 default: {
387                     // 'aa screen' with an unknown option: aa screen -x
388                     // 'aa screen' with an unknown option: aa screen -xxx
389                     std::string unknownOption = "";
390                     std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption);
391 
392                     HILOG_INFO("'aa screen' with an unknown option.");
393 
394                     resultReceiver_.append(unknownOptionMsg);
395                     result = OHOS::ERR_INVALID_VALUE;
396                     break;
397                 }
398             }
399             break;
400         }
401 
402         switch (option) {
403             case 'h': {
404                 // 'aa screen -h'
405                 // 'aa screen --help'
406                 result = OHOS::ERR_INVALID_VALUE;
407                 break;
408             }
409             case 'p': {
410                 // 'aa screen -p xxx'
411 
412                 // save powerState
413                 if (optarg != nullptr) {
414                     powerState = optarg;
415                 }
416                 break;
417             }
418             case 0: {
419                 break;
420             }
421             default: {
422                 break;
423             }
424         }
425     }
426 
427     if (result == OHOS::ERR_OK) {
428         HILOG_INFO("powerState: %{public}s", powerState.c_str());
429 
430         if (powerState == STRING_SCREEN_POWER_ON) {
431             result = AbilityManagerClient::GetInstance()->PowerOn();
432             if (result == OHOS::ERR_OK) {
433                 HILOG_INFO("%{public}s", STRING_SCREEN_POWER_ON_OK.c_str());
434                 resultReceiver_ = STRING_SCREEN_POWER_ON_OK + "\n";
435             } else {
436                 HILOG_INFO("%{public}s result = %{public}d", STRING_SCREEN_POWER_ON_NG.c_str(), result);
437                 resultReceiver_ = STRING_SCREEN_POWER_ON_NG + "\n";
438 
439                 resultReceiver_.append(GetMessageFromCode(result));
440             }
441         } else {
442             result = AbilityManagerClient::GetInstance()->PowerOff();
443             if (result == OHOS::ERR_OK) {
444                 HILOG_INFO("%{public}s", STRING_SCREEN_POWER_OFF_OK.c_str());
445                 resultReceiver_ = STRING_SCREEN_POWER_OFF_OK + "\n";
446             } else {
447                 HILOG_INFO("%{public}s result = %{public}d", STRING_SCREEN_POWER_OFF_NG.c_str(), result);
448                 resultReceiver_ = STRING_SCREEN_POWER_OFF_NG + "\n";
449 
450                 resultReceiver_.append(GetMessageFromCode(result));
451             }
452         }
453     } else {
454         resultReceiver_.append(HELP_MSG_SCREEN);
455         result = OHOS::ERR_INVALID_VALUE;
456     }
457 
458     return result;
459 }
460 
RunAsStartAbility()461 ErrCode AbilityManagerShellCommand::RunAsStartAbility()
462 {
463     Want want;
464     std::string windowMode;
465     ErrCode result = MakeWantFromCmd(want, windowMode);
466     if (result == OHOS::ERR_OK) {
467         int windowModeKey = std::atoi(windowMode.c_str());
468         if (windowModeKey > 0) {
469             auto setting = AbilityStartSetting::GetEmptySetting();
470             if (setting != nullptr) {
471                 setting->AddProperty(AbilityStartSetting::WINDOW_MODE_KEY, windowMode);
472                 result = AbilityManagerClient::GetInstance()->StartAbility(want, *(setting.get()), nullptr, -1);
473             }
474         } else {
475             result = AbilityManagerClient::GetInstance()->StartAbility(want);
476         }
477         if (result == OHOS::ERR_OK) {
478             HILOG_INFO("%{public}s", STRING_START_ABILITY_OK.c_str());
479             resultReceiver_ = STRING_START_ABILITY_OK + "\n";
480         } else {
481             HILOG_INFO("%{public}s result = %{public}d", STRING_START_ABILITY_NG.c_str(), result);
482             resultReceiver_ = STRING_START_ABILITY_NG + "\n";
483 
484             resultReceiver_.append(GetMessageFromCode(result));
485         }
486     } else {
487         resultReceiver_.append(HELP_MSG_START);
488         result = OHOS::ERR_INVALID_VALUE;
489     }
490 
491     return result;
492 }
493 
RunAsStopService()494 ErrCode AbilityManagerShellCommand::RunAsStopService()
495 {
496     ErrCode result = OHOS::ERR_OK;
497 
498     Want want;
499     std::string windowMode;
500     result = MakeWantFromCmd(want, windowMode);
501     if (result == OHOS::ERR_OK) {
502         result = AbilityManagerClient::GetInstance()->StopServiceAbility(want);
503         if (result == OHOS::ERR_OK) {
504             HILOG_INFO("%{public}s", STRING_STOP_SERVICE_ABILITY_OK.c_str());
505             resultReceiver_ = STRING_STOP_SERVICE_ABILITY_OK + "\n";
506         } else {
507             HILOG_INFO("%{public}s result = %{public}d", STRING_STOP_SERVICE_ABILITY_NG.c_str(), result);
508             resultReceiver_ = STRING_STOP_SERVICE_ABILITY_NG + "\n";
509 
510             resultReceiver_.append(GetMessageFromCode(result));
511         }
512     } else {
513         resultReceiver_.append(HELP_MSG_STOP_SERVICE);
514         result = OHOS::ERR_INVALID_VALUE;
515     }
516 
517     return result;
518 }
519 
RunAsDumpCommand()520 ErrCode AbilityManagerShellCommand::RunAsDumpCommand()
521 {
522     ErrCode result = OHOS::ERR_OK;
523 
524     std::string args;
525     for (auto arg : argList_) {
526         args += arg;
527         args += " ";
528     }
529 
530     int option = getopt_long(argc_, argv_, SHORT_OPTIONS_DUMP.c_str(), LONG_OPTIONS_DUMP, nullptr);
531 
532     HILOG_INFO("option: %{public}d, optopt: %{public}d, optind: %{public}d", option, optopt, optind);
533 
534     if (optind < 0 || optind > argc_) {
535         return OHOS::ERR_INVALID_VALUE;
536     }
537 
538     for (int i = 0; i < argc_; i++) {
539         HILOG_INFO("argv_[%{public}d]: %{public}s", i, argv_[i]);
540     }
541 
542     switch (option) {
543         case 'h': {
544             // 'aa dump -h'
545             // 'aa dump --help'
546             result = OHOS::ERR_INVALID_VALUE;
547             break;
548         }
549         case 'a': {
550             // 'aa dump -a'
551             // 'aa dump --all'
552             break;
553         }
554         case 's': {
555             // 'aa dump -s xxx'
556             // 'aa dump --stack xxx'
557             break;
558         }
559         case 'm': {
560             // 'aa dump -m xxx'
561             // 'aa dump --mission xxx'
562             break;
563         }
564         case 'l': {
565             // 'aa dump -l'
566             // 'aa dump --stack-list'
567             break;
568         }
569         case 'u': {
570             // 'aa dump -u'
571             // 'aa dump --ui'
572             break;
573         }
574         case 'd': {
575             // 'aa dump -d'
576             // 'aa dump --data'
577             break;
578         }
579         case 'e': {
580             // 'aa dump -e'
581             // 'aa dump --serv'
582             break;
583         }
584         case '?': {
585             result = RunAsDumpCommandOptopt();
586             break;
587         }
588         default: {
589             if (strcmp(argv_[optind], cmd_.c_str()) == 0) {
590                 // 'aa dump' with no option: aa dump
591                 // 'aa dump' with a wrong argument: aa dump xxx
592                 HILOG_INFO("'aa dump' with no option.");
593 
594                 resultReceiver_.append(HELP_MSG_NO_OPTION + "\n");
595                 result = OHOS::ERR_INVALID_VALUE;
596             }
597             break;
598         }
599     }
600 
601     if (result != OHOS::ERR_OK) {
602         resultReceiver_.append(HELP_MSG_DUMP);
603     } else {
604         std::vector<std::string> dumpResults;
605         result = AbilityManagerClient::GetInstance()->DumpState(args, dumpResults);
606         if (result == OHOS::ERR_OK) {
607             for (auto it : dumpResults) {
608                 resultReceiver_ += it + "\n";
609             }
610         } else {
611             HILOG_INFO("failed to dump state.");
612         }
613     }
614 
615     return result;
616 }
617 
RunAsDumpCommandOptopt()618 ErrCode AbilityManagerShellCommand::RunAsDumpCommandOptopt()
619 {
620     ErrCode result = OHOS::ERR_OK;
621 
622     switch (optopt) {
623         case 's': {
624             // 'aa dump -s' with no argument: aa dump -s
625             // 'aa dump --stack' with no argument: aa dump --stack
626             HILOG_INFO("'aa dump -s' with no argument.");
627 
628             resultReceiver_.append("error: option ");
629             resultReceiver_.append("requires a value.\n");
630             result = OHOS::ERR_INVALID_VALUE;
631             break;
632         }
633         case 'm': {
634             // 'aa dump -m' with no argument: aa dump -m
635             // 'aa dump --mission' with no argument: aa dump --mission
636             HILOG_INFO("'aa dump -m' with no argument.");
637 
638             resultReceiver_.append("error: option ");
639             resultReceiver_.append("requires a value.\n");
640             result = OHOS::ERR_INVALID_VALUE;
641             break;
642         }
643         case 0: {
644             // 'aa dump' with an unknown option: aa dump --x
645             // 'aa dump' with an unknown option: aa dump --xxx
646             std::string unknownOption = "";
647             std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption);
648 
649             HILOG_INFO("'aa dump' with an unknown option.");
650 
651             resultReceiver_.append(unknownOptionMsg);
652             result = OHOS::ERR_INVALID_VALUE;
653             break;
654         }
655         default: {
656             // 'aa dump' with an unknown option: aa dump -x
657             // 'aa dump' with an unknown option: aa dump -xxx
658             std::string unknownOption = "";
659             std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption);
660 
661             HILOG_INFO("'aa dump' with an unknown option.");
662 
663             resultReceiver_.append(unknownOptionMsg);
664             result = OHOS::ERR_INVALID_VALUE;
665             break;
666         }
667     }
668 
669     return result;
670 }
671 
MakeWantFromCmd(Want & want,std::string & windowMode)672 ErrCode AbilityManagerShellCommand::MakeWantFromCmd(Want &want, std::string &windowMode)
673 {
674     int result = OHOS::ERR_OK;
675 
676     int option = -1;
677     int counter = 0;
678 
679     std::string deviceId = "";
680     std::string bundleName = "";
681     std::string abilityName = "";
682 
683     while (true) {
684         counter++;
685 
686         option = getopt_long(argc_, argv_, SHORT_OPTIONS.c_str(), LONG_OPTIONS, nullptr);
687 
688         HILOG_INFO("option: %{public}d, optopt: %{public}d, optind: %{public}d", option, optopt, optind);
689 
690         if (optind < 0 || optind > argc_) {
691             return OHOS::ERR_INVALID_VALUE;
692         }
693 
694         for (int i = 0; i < argc_; i++) {
695             HILOG_INFO("argv_[%{public}d]: %{public}s", i, argv_[i]);
696         }
697 
698         if (option == -1) {
699             if (counter == 1) {
700                 // When scanning the first argument
701                 if (strcmp(argv_[optind], cmd_.c_str()) == 0) {
702                     // 'aa start' with no option: aa start
703                     // 'aa start' with a wrong argument: aa start xxx
704                     // 'aa stop-service' with no option: aa stop-service
705                     // 'aa stop-service' with a wrong argument: aa stop-service xxx
706                     HILOG_INFO("'aa %{public}s' %{public}s", HELP_MSG_NO_OPTION.c_str(), cmd_.c_str());
707 
708                     resultReceiver_.append(HELP_MSG_NO_OPTION + "\n");
709                     result = OHOS::ERR_INVALID_VALUE;
710                 }
711             }
712             break;
713         }
714 
715         if (option == '?') {
716             switch (optopt) {
717                 case 'd': {
718                     // 'aa start -d' with no argument
719                     // 'aa stop-service -d' with no argument
720                     HILOG_INFO("'aa %{public}s -d' with no argument.", cmd_.c_str());
721 
722                     resultReceiver_.append("error: option ");
723                     resultReceiver_.append("requires a value.\n");
724 
725                     result = OHOS::ERR_INVALID_VALUE;
726                     break;
727                 }
728                 case 'a': {
729                     // 'aa start -a' with no argument
730                     // 'aa stop-service -a' with no argument
731                     HILOG_INFO("'aa %{public}s -a' with no argument.", cmd_.c_str());
732 
733                     resultReceiver_.append("error: option ");
734                     resultReceiver_.append("requires a value.\n");
735 
736                     result = OHOS::ERR_INVALID_VALUE;
737                     break;
738                 }
739                 case 'b': {
740                     // 'aa start -b' with no argument
741                     // 'aa stop-service -b' with no argument
742                     HILOG_INFO("'aa %{public}s -b' with no argument.", cmd_.c_str());
743 
744                     resultReceiver_.append("error: option ");
745                     resultReceiver_.append("requires a value.\n");
746 
747                     result = OHOS::ERR_INVALID_VALUE;
748                     break;
749                 }
750                 case 's': {
751                     // 'aa start -s' with no argument
752                     // 'aa stop-service -s' with no argument
753                     HILOG_INFO("'aa %{public}s -s' with no argument.", cmd_.c_str());
754 
755                     resultReceiver_.append("error: option ");
756                     resultReceiver_.append(argv_[optind - 1]);
757                     resultReceiver_.append("' requires a value.\n");
758 
759                     result = OHOS::ERR_INVALID_VALUE;
760                     break;
761                 }
762                 case 0: {
763                     // 'aa start' with an unknown option: aa start --x
764                     // 'aa start' with an unknown option: aa start --xxx
765                     // 'aa stop-service' with an unknown option: aa stop-service --x
766                     // 'aa stop-service' with an unknown option: aa stop-service --xxx
767                     std::string unknownOption = "";
768                     std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption);
769 
770                     HILOG_INFO("'aa %{public}s' with an unknown option.", cmd_.c_str());
771 
772                     resultReceiver_.append(unknownOptionMsg);
773                     result = OHOS::ERR_INVALID_VALUE;
774                     break;
775                 }
776                 default: {
777                     // 'aa start' with an unknown option: aa start -x
778                     // 'aa start' with an unknown option: aa start -xxx
779                     // 'aa stop-service' with an unknown option: aa stop-service -x
780                     // 'aa stop-service' with an unknown option: aa stop-service -xxx
781                     std::string unknownOption = "";
782                     std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption);
783 
784                     HILOG_INFO("'aa %{public}s' with an unknown option.", cmd_.c_str());
785 
786                     resultReceiver_.append(unknownOptionMsg);
787                     result = OHOS::ERR_INVALID_VALUE;
788                     break;
789                 }
790             }
791             break;
792         }
793 
794         switch (option) {
795             case 'h': {
796                 // 'aa start -h'
797                 // 'aa start --help'
798                 // 'aa stop-service -h'
799                 // 'aa stop-service --help'
800                 result = OHOS::ERR_INVALID_VALUE;
801                 break;
802             }
803             case 'd': {
804                 // 'aa start -d xxx'
805                 // 'aa stop-service -d xxx'
806 
807                 // save device ID
808                 if (optarg != nullptr) {
809                     deviceId = optarg;
810                 }
811                 break;
812             }
813             case 'a': {
814                 // 'aa start -a xxx'
815                 // 'aa stop-service -a xxx'
816 
817                 // save ability name
818                 abilityName = optarg;
819                 break;
820             }
821             case 'b': {
822                 // 'aa start -b xxx'
823                 // 'aa stop-service -b xxx'
824 
825                 // save bundle name
826                 bundleName = optarg;
827                 break;
828             }
829             case 's': {
830                 // 'aa start -s xxx'
831                 // save windowMode
832                 windowMode = optarg;
833                 break;
834             }
835             case 0: {
836                 break;
837             }
838             default: {
839                 break;
840             }
841         }
842     }
843 
844     if (result == OHOS::ERR_OK) {
845         if (abilityName.size() == 0 || bundleName.size() == 0) {
846             // 'aa start [-d <device-id>] -a <ability-name> -b <bundle-name>'
847             // 'aa stop-service [-d <device-id>] -a <ability-name> -b <bundle-name>'
848             HILOG_INFO("'aa %{public}s' without enough options.", cmd_.c_str());
849 
850             if (abilityName.size() == 0) {
851                 resultReceiver_.append(HELP_MSG_NO_ABILITY_NAME_OPTION + "\n");
852             }
853 
854             if (bundleName.size() == 0) {
855                 resultReceiver_.append(HELP_MSG_NO_BUNDLE_NAME_OPTION + "\n");
856             }
857 
858             result = OHOS::ERR_INVALID_VALUE;
859         } else {
860             ElementName element(deviceId, bundleName, abilityName);
861             want.SetElement(element);
862         }
863     }
864 
865     return result;
866 }
867 
868 }  // namespace AAFwk
869 }  // namespace OHOS
870