Dianne Hackborn | 79f7ec7 | 2013-04-04 18:50:23 -0700 | [diff] [blame] | 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 | |
Dianne Hackborn | 3cdb56e | 2015-11-11 12:45:44 -0800 | [diff] [blame] | 20 | import android.os.ShellCommand; |
| 21 | |
Dianne Hackborn | 79f7ec7 | 2013-04-04 18:50:23 -0700 | [diff] [blame] | 22 | import java.io.PrintStream; |
| 23 | |
| 24 | public abstract class BaseCommand { |
| 25 | |
Dianne Hackborn | 3cdb56e | 2015-11-11 12:45:44 -0800 | [diff] [blame] | 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 | }; |
Dianne Hackborn | 79f7ec7 | 2013-04-04 18:50:23 -0700 | [diff] [blame] | 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 | |
Dianne Hackborn | 354736e | 2016-08-22 17:00:05 -0700 | [diff] [blame] | 39 | private String[] mRawArgs; |
| 40 | |
Dianne Hackborn | 79f7ec7 | 2013-04-04 18:50:23 -0700 | [diff] [blame] | 41 | /** |
| 42 | * Call to run the command. |
| 43 | */ |
| 44 | public void run(String[] args) { |
| 45 | if (args.length < 1) { |
| 46 | onShowUsage(System.out); |
| 47 | return; |
| 48 | } |
| 49 | |
Dianne Hackborn | 354736e | 2016-08-22 17:00:05 -0700 | [diff] [blame] | 50 | mRawArgs = args; |
| 51 | mArgs.init(null, null, null, null, args, null, 0); |
Dianne Hackborn | 79f7ec7 | 2013-04-04 18:50:23 -0700 | [diff] [blame] | 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 | */ |
| 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 | */ |
| 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 | */ |
| 85 | public abstract void onRun() throws Exception; |
| 86 | |
| 87 | /** |
| 88 | * Print help text for the command. |
| 89 | */ |
| 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 | */ |
| 96 | public String nextOption() { |
Dianne Hackborn | 3cdb56e | 2015-11-11 12:45:44 -0800 | [diff] [blame] | 97 | return mArgs.getNextOption(); |
Dianne Hackborn | 79f7ec7 | 2013-04-04 18:50:23 -0700 | [diff] [blame] | 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 | */ |
| 104 | public String nextArg() { |
Dianne Hackborn | 3cdb56e | 2015-11-11 12:45:44 -0800 | [diff] [blame] | 105 | return mArgs.getNextArg(); |
Dianne Hackborn | 79f7ec7 | 2013-04-04 18:50:23 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /** |
Mike Ma | d223982 | 2017-10-31 12:30:42 -0700 | [diff] [blame] | 109 | * Peek the next argument on the command line, whatever it is; if there are |
| 110 | * no arguments left, return null. |
| 111 | */ |
| 112 | public String peekNextArg() { |
| 113 | return mArgs.peekNextArg(); |
| 114 | } |
| 115 | |
| 116 | /** |
Dianne Hackborn | 79f7ec7 | 2013-04-04 18:50:23 -0700 | [diff] [blame] | 117 | * Return the next argument on the command line, whatever it is; if there are |
| 118 | * no arguments left, throws an IllegalArgumentException to report this to the user. |
| 119 | */ |
| 120 | public String nextArgRequired() { |
Dianne Hackborn | 3cdb56e | 2015-11-11 12:45:44 -0800 | [diff] [blame] | 121 | return mArgs.getNextArgRequired(); |
Dianne Hackborn | 79f7ec7 | 2013-04-04 18:50:23 -0700 | [diff] [blame] | 122 | } |
Dianne Hackborn | 354736e | 2016-08-22 17:00:05 -0700 | [diff] [blame] | 123 | |
| 124 | /** |
| 125 | * Return the original raw argument list supplied to the command. |
| 126 | */ |
| 127 | public String[] getRawArgs() { |
| 128 | return mRawArgs; |
| 129 | } |
Dianne Hackborn | 79f7ec7 | 2013-04-04 18:50:23 -0700 | [diff] [blame] | 130 | } |