2 * Copyright (C) 2008 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com
.android
.commands
.svc
;
21 public static abstract class Command
{
24 public Command(String name
) {
28 public String
name() {
32 public abstract String
shortHelp(); // should fit on one short line
33 public abstract String
longHelp(); // take as much space as you need, 75 col max
34 public abstract void run(String
[] args
); // run the command
37 public static void main(String
[] args
) {
38 if (args
.length
>= 1) {
39 Command c
= lookupCommand(args
[0]);
45 COMMAND_HELP
.run(args
);
48 private static Command
lookupCommand(String name
) {
49 final int N
= COMMANDS
.length
;
50 for (int i
=0; i
<N
; i
++) {
51 Command c
= COMMANDS
[i
];
52 if (c
.name().equals(name
)) {
59 public static final Command COMMAND_HELP
= new Command("help") {
60 public String
shortHelp() {
61 return "Show information about the subcommands";
63 public String
longHelp() {
66 public void run(String
[] args
) {
67 if (args
.length
== 2) {
68 Command c
= lookupCommand(args
[1]);
70 System
.err
.println(c
.longHelp());
75 System
.err
.println("Available commands:");
76 final int N
= COMMANDS
.length
;
78 for (int i
=0; i
<N
; i
++) {
79 Command c
= COMMANDS
[i
];
80 int len
= c
.name().length();
85 String format
= " %-" + maxlen
+ "s %s";
86 for (int i
=0; i
<N
; i
++) {
87 Command c
= COMMANDS
[i
];
88 System
.err
.println(String
.format(format
, c
.name(), c
.shortHelp()));
93 public static final Command
[] COMMANDS
= new Command
[] {