1 /*
2  * Copyright (c) 2020 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 <cstdio>
17 #include <cstdlib>
18 #include <getopt.h>
19 
20 #include "ability_tool.h"
21 
22 using OHOS::AbilityTool;
23 
PrintUsage()24 static void PrintUsage()
25 {
26     printf("Usage:\n");
27     printf("aa start -p bundlename -n ability_name\n");
28     printf("aa stopability -p bundlename -n ability_name\n");
29     printf("aa terminate -p bundlename\n");
30     printf("aa dump -p bundlename -n ability_name -e extra_option\n");
31     printf("aa dump -a\n");
32     printf("\n");
33     printf("Options:\n");
34     printf(" -h (--help)                Show the help information.             [eg: aa -h]\n");
35     printf(" -p (--bundlename)          Appoint the bundlename name.           [eg: -p com.huawei]\n");
36     printf(" -n (--abilityname)         Appoint the ability name.              [eg: -n MyAbility]\n");
37     printf(" -a (--all)                 [Unnecessary]dump all ability info.    [eg: -a]\n");
38     printf(" -e (--extra)               [Unnecessary]extra info when dump.     [eg: -e]\n");
39     printf("\n");
40     printf("Commands:\n");
41     printf("aa start                    Start the target ability.\n");
42     printf("aa stopability              Stop the target service ability.\n");
43     printf("aa terminate                Terminate the target app.\n");
44     printf("aa dump                     Dump ability\n");
45 }
46 
SetOptions(int argc,char * argv[],const option * options,AbilityTool & tool)47 static void SetOptions(int argc, char *argv[], const option *options, AbilityTool &tool)
48 {
49     const char *command = argv[1];
50     int index = 0;
51     const char *optStr = "hap:n:e:";
52     int para = 0;
53     while ((para = getopt_long(argc, argv, optStr, options, &index)) != -1) {
54         switch (para) {
55             case 'h': {
56                 PrintUsage();
57                 exit(0);
58             }
59             case 'a': {
60                 tool.SetDumpAll();
61                 break;
62             }
63             case 'p': {
64                 tool.SetBundleName(optarg);
65                 break;
66             }
67             case 'n': {
68                 tool.SetAbilityName(optarg);
69                 break;
70             }
71             case 'e': {
72                 tool.SetExtra(optarg);
73                 break;
74             }
75             default:
76                 printf("Try 'aa -h' for more information.\n");
77                 exit(-1);
78         }
79     }
80     if (!tool.SetCommand(command)) {
81         printf("Unsupported this command. Try 'aa -h' for more information.\n");
82         exit(-1);
83     }
84 }
85 
main(int argc,char * argv[])86 int main(int argc, char *argv[])
87 {
88     struct option options[] = {
89         {"help",        no_argument,       nullptr, 'h'},
90         {"bundlename",  required_argument, nullptr, 'p'},
91         {"abilityname", required_argument, nullptr, 'n'},
92         {"all",         no_argument,       nullptr, 'a'},
93         {"extra",       required_argument, nullptr, 'e'},
94         {nullptr,       no_argument,       nullptr, 0},
95     };
96 
97     if (argc == 1) {
98         PrintUsage();
99         exit(0);
100     }
101     AbilityTool tool = AbilityTool();
102     SetOptions(argc, argv, options, tool);
103     bool ret = tool.RunCommand();
104     if (!ret) {
105         printf("Failed to run the command. \n");
106     }
107     exit(0);
108 }
109