blob: 05ec9e90513e7145636972abe7b42c5da36757f6 [file] [log] [blame]
Dianne Hackborn79f7ec72013-04-04 18:50:23 -07001/*
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
18package com.android.internal.os;
19
Dianne Hackborn3cdb56e2015-11-11 12:45:44 -080020import android.os.ShellCommand;
21
Dianne Hackborn79f7ec72013-04-04 18:50:23 -070022import java.io.PrintStream;
23
24public abstract class BaseCommand {
25
Dianne Hackborn3cdb56e2015-11-11 12:45:44 -080026 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 Hackborn79f7ec72013-04-04 18:50:23 -070033
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 Hackborn354736e2016-08-22 17:00:05 -070039 private String[] mRawArgs;
40
Dianne Hackborn79f7ec72013-04-04 18:50:23 -070041 /**
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 Hackborn354736e2016-08-22 17:00:05 -070050 mRawArgs = args;
51 mArgs.init(null, null, null, null, args, null, 0);
Dianne Hackborn79f7ec72013-04-04 18:50:23 -070052
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 Hackborn3cdb56e2015-11-11 12:45:44 -080097 return mArgs.getNextOption();
Dianne Hackborn79f7ec72013-04-04 18:50:23 -070098 }
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 Hackborn3cdb56e2015-11-11 12:45:44 -0800105 return mArgs.getNextArg();
Dianne Hackborn79f7ec72013-04-04 18:50:23 -0700106 }
107
108 /**
Mike Mad2239822017-10-31 12:30:42 -0700109 * 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 Hackborn79f7ec72013-04-04 18:50:23 -0700117 * 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 Hackborn3cdb56e2015-11-11 12:45:44 -0800121 return mArgs.getNextArgRequired();
Dianne Hackborn79f7ec72013-04-04 18:50:23 -0700122 }
Dianne Hackborn354736e2016-08-22 17:00:05 -0700123
124 /**
125 * Return the original raw argument list supplied to the command.
126 */
127 public String[] getRawArgs() {
128 return mRawArgs;
129 }
Dianne Hackborn79f7ec72013-04-04 18:50:23 -0700130}