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