Home | History | Annotate | Download | only in svc
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  *
      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
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     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.
     15  */
     16 
     17 package com.android.commands.svc;
     18 
     19 public class Svc {
     20 
     21     public static abstract class Command {
     22         private String mName;
     23 
     24         public Command(String name) {
     25             mName = name;
     26         }
     27 
     28         public String name() {
     29             return mName;
     30         }
     31 
     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
     35     }
     36 
     37     public static void main(String[] args) {
     38         if (args.length >= 1) {
     39             Command c = lookupCommand(args[0]);
     40             if (c != null) {
     41                 c.run(args);
     42                 return;
     43             }
     44         }
     45         COMMAND_HELP.run(args);
     46     }
     47 
     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)) {
     53                 return c;
     54             }
     55         }
     56         return null;
     57     }
     58 
     59     public static final Command COMMAND_HELP = new Command("help") {
     60         public String shortHelp() {
     61             return "Show information about the subcommands";
     62         }
     63         public String longHelp() {
     64             return shortHelp();
     65         }
     66         public void run(String[] args) {
     67             if (args.length == 2) {
     68                 Command c = lookupCommand(args[1]);
     69                 if (c != null) {
     70                     System.err.println(c.longHelp());
     71                     return;
     72                 }
     73             }
     74 
     75             System.err.println("Available commands:");
     76             final int N = COMMANDS.length;
     77             int maxlen = 0;
     78             for (int i=0; i<N; i++) {
     79                 Command c = COMMANDS[i];
     80                 int len = c.name().length();
     81                 if (maxlen < len) {
     82                     maxlen = len;
     83                 }
     84             }
     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()));
     89             }
     90         }
     91     };
     92 
     93     public static final Command[] COMMANDS = new Command[] {
     94             COMMAND_HELP,
     95             new PowerCommand(),
     96             new DataCommand(),
     97             new WifiCommand()
     98     };
     99 }
    100