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