• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 **
3 ** Copyright 2013, The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 **     http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17 
18 package com.android.internal.os;
19 
20 import android.compat.annotation.UnsupportedAppUsage;
21 import android.os.Build;
22 
23 import com.android.modules.utils.BasicShellCommandHandler;
24 
25 import java.io.PrintStream;
26 
27 public abstract class BaseCommand {
28 
29     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
30     final protected BasicShellCommandHandler mArgs = new BasicShellCommandHandler() {
31         @Override public int onCommand(String cmd) {
32             return 0;
33         }
34         @Override public void onHelp() {
35         }
36     };
37 
38     // These are magic strings understood by the Eclipse plugin.
39     public static final String FATAL_ERROR_CODE = "Error type 1";
40     public static final String NO_SYSTEM_ERROR_CODE = "Error type 2";
41     public static final String NO_CLASS_ERROR_CODE = "Error type 3";
42 
43     private String[] mRawArgs;
44 
45     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
BaseCommand()46     public BaseCommand() {
47     }
48 
49     /**
50      * Call to run the command.
51      */
run(String[] args)52     public void run(String[] args) {
53         if (args.length < 1) {
54             onShowUsage(System.out);
55             return;
56         }
57 
58         mRawArgs = args;
59         mArgs.init(null, null, null, null, args, 0);
60 
61         int status = 1;
62         try {
63             onRun();
64             status = 0;
65         } catch (IllegalArgumentException e) {
66             onShowUsage(System.err);
67             System.err.println();
68             System.err.println("Error: " + e.getMessage());
69             status = 0;
70         } catch (Exception e) {
71             e.printStackTrace(System.err);
72         } finally {
73             System.out.flush();
74             System.err.flush();
75         }
76         if (status != 0) {
77             System.exit(status);
78         }
79     }
80 
81     /**
82      * Convenience to show usage information to error output.
83      */
showUsage()84     public void showUsage() {
85         onShowUsage(System.err);
86     }
87 
88     /**
89      * Convenience to show usage information to error output along
90      * with an error message.
91      */
showError(String message)92     public void showError(String message) {
93         onShowUsage(System.err);
94         System.err.println();
95         System.err.println(message);
96     }
97 
98     /**
99      * Implement the command.
100      */
onRun()101     public abstract void onRun() throws Exception;
102 
103     /**
104      * Print help text for the command.
105      */
onShowUsage(PrintStream out)106     public abstract void onShowUsage(PrintStream out);
107 
108     /**
109      * Return the next option on the command line -- that is an argument that
110      * starts with '-'.  If the next argument is not an option, null is returned.
111      */
nextOption()112     public String nextOption() {
113         return mArgs.getNextOption();
114     }
115 
116     /**
117      * Return the next argument on the command line, whatever it is; if there are
118      * no arguments left, return null.
119      */
nextArg()120     public String nextArg() {
121         return mArgs.getNextArg();
122     }
123 
124     /**
125      * Peek the next argument on the command line, whatever it is; if there are
126      * no arguments left, return null.
127      */
peekNextArg()128     public String peekNextArg() {
129         return mArgs.peekNextArg();
130     }
131 
132     /**
133      * Return the next argument on the command line, whatever it is; if there are
134      * no arguments left, throws an IllegalArgumentException to report this to the user.
135      */
nextArgRequired()136     public String nextArgRequired() {
137         return mArgs.getNextArgRequired();
138     }
139 
140     /**
141      * Return the original raw argument list supplied to the command.
142      */
getRawArgs()143     public String[] getRawArgs() {
144         return mRawArgs;
145     }
146 }
147