• 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.annotation.UnsupportedAppUsage;
21 import android.os.ShellCommand;
22 
23 import java.io.PrintStream;
24 
25 public abstract class BaseCommand {
26 
27     @UnsupportedAppUsage
28     final protected ShellCommand mArgs = new ShellCommand() {
29         @Override public int onCommand(String cmd) {
30             return 0;
31         }
32         @Override public void onHelp() {
33         }
34     };
35 
36     // These are magic strings understood by the Eclipse plugin.
37     public static final String FATAL_ERROR_CODE = "Error type 1";
38     public static final String NO_SYSTEM_ERROR_CODE = "Error type 2";
39     public static final String NO_CLASS_ERROR_CODE = "Error type 3";
40 
41     private String[] mRawArgs;
42 
43     /**
44      * Call to run the command.
45      */
run(String[] args)46     public void run(String[] args) {
47         if (args.length < 1) {
48             onShowUsage(System.out);
49             return;
50         }
51 
52         mRawArgs = args;
53         mArgs.init(null, null, null, null, args, null, 0);
54 
55         try {
56             onRun();
57         } catch (IllegalArgumentException e) {
58             onShowUsage(System.err);
59             System.err.println();
60             System.err.println("Error: " + e.getMessage());
61         } catch (Exception e) {
62             e.printStackTrace(System.err);
63             System.exit(1);
64         }
65     }
66 
67     /**
68      * Convenience to show usage information to error output.
69      */
showUsage()70     public void showUsage() {
71         onShowUsage(System.err);
72     }
73 
74     /**
75      * Convenience to show usage information to error output along
76      * with an error message.
77      */
showError(String message)78     public void showError(String message) {
79         onShowUsage(System.err);
80         System.err.println();
81         System.err.println(message);
82     }
83 
84     /**
85      * Implement the command.
86      */
onRun()87     public abstract void onRun() throws Exception;
88 
89     /**
90      * Print help text for the command.
91      */
onShowUsage(PrintStream out)92     public abstract void onShowUsage(PrintStream out);
93 
94     /**
95      * Return the next option on the command line -- that is an argument that
96      * starts with '-'.  If the next argument is not an option, null is returned.
97      */
nextOption()98     public String nextOption() {
99         return mArgs.getNextOption();
100     }
101 
102     /**
103      * Return the next argument on the command line, whatever it is; if there are
104      * no arguments left, return null.
105      */
nextArg()106     public String nextArg() {
107         return mArgs.getNextArg();
108     }
109 
110     /**
111      * Peek the next argument on the command line, whatever it is; if there are
112      * no arguments left, return null.
113      */
peekNextArg()114     public String peekNextArg() {
115         return mArgs.peekNextArg();
116     }
117 
118     /**
119      * Return the next argument on the command line, whatever it is; if there are
120      * no arguments left, throws an IllegalArgumentException to report this to the user.
121      */
nextArgRequired()122     public String nextArgRequired() {
123         return mArgs.getNextArgRequired();
124     }
125 
126     /**
127      * Return the original raw argument list supplied to the command.
128      */
getRawArgs()129     public String[] getRawArgs() {
130         return mRawArgs;
131     }
132 }
133