• 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     /**
40      * Call to run the command.
41      */
run(String[] args)42     public void run(String[] args) {
43         if (args.length < 1) {
44             onShowUsage(System.out);
45             return;
46         }
47 
48         mArgs.init(null, null, null, null, args, 0);
49 
50         try {
51             onRun();
52         } catch (IllegalArgumentException e) {
53             onShowUsage(System.err);
54             System.err.println();
55             System.err.println("Error: " + e.getMessage());
56         } catch (Exception e) {
57             e.printStackTrace(System.err);
58             System.exit(1);
59         }
60     }
61 
62     /**
63      * Convenience to show usage information to error output.
64      */
showUsage()65     public void showUsage() {
66         onShowUsage(System.err);
67     }
68 
69     /**
70      * Convenience to show usage information to error output along
71      * with an error message.
72      */
showError(String message)73     public void showError(String message) {
74         onShowUsage(System.err);
75         System.err.println();
76         System.err.println(message);
77     }
78 
79     /**
80      * Implement the command.
81      */
onRun()82     public abstract void onRun() throws Exception;
83 
84     /**
85      * Print help text for the command.
86      */
onShowUsage(PrintStream out)87     public abstract void onShowUsage(PrintStream out);
88 
89     /**
90      * Return the next option on the command line -- that is an argument that
91      * starts with '-'.  If the next argument is not an option, null is returned.
92      */
nextOption()93     public String nextOption() {
94         return mArgs.getNextOption();
95     }
96 
97     /**
98      * Return the next argument on the command line, whatever it is; if there are
99      * no arguments left, return null.
100      */
nextArg()101     public String nextArg() {
102         return mArgs.getNextArg();
103     }
104 
105     /**
106      * Return the next argument on the command line, whatever it is; if there are
107      * no arguments left, throws an IllegalArgumentException to report this to the user.
108      */
nextArgRequired()109     public String nextArgRequired() {
110         return mArgs.getNextArgRequired();
111     }
112 }
113