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