1 /* 2 * Copyright (C) 2018 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.dialer.commandline.impl; 18 19 import android.content.Context; 20 import android.support.annotation.NonNull; 21 import com.android.dialer.commandline.Arguments; 22 import com.android.dialer.commandline.Command; 23 import com.android.dialer.commandline.CommandLineComponent; 24 import com.android.dialer.inject.ApplicationContext; 25 import com.google.common.collect.ImmutableMap; 26 import com.google.common.util.concurrent.Futures; 27 import com.google.common.util.concurrent.ListenableFuture; 28 import java.util.Locale; 29 import java.util.Map.Entry; 30 import java.util.concurrent.ExecutionException; 31 import javax.inject.Inject; 32 33 /** List available commands */ 34 public class Help implements Command { 35 36 @NonNull 37 @Override getShortDescription()38 public String getShortDescription() { 39 return "Print this message"; 40 } 41 42 @NonNull 43 @Override getUsage()44 public String getUsage() { 45 return "help"; 46 } 47 48 private final Context context; 49 50 @Inject Help(@pplicationContext Context context)51 Help(@ApplicationContext Context context) { 52 this.context = context; 53 } 54 55 @Override run(Arguments args)56 public ListenableFuture<String> run(Arguments args) throws IllegalCommandLineArgumentException { 57 boolean showHidden = args.getFlags().containsKey("showHidden"); 58 59 StringBuilder stringBuilder = new StringBuilder(); 60 ImmutableMap<String, Command> commands = 61 CommandLineComponent.get(context).commandSupplier().get(); 62 stringBuilder 63 .append(runOrThrow(commands.get("version"))) 64 .append("\n") 65 .append("\n") 66 .append("usage: <command> [args...]\n") 67 .append("\n") 68 .append("<command>\n"); 69 70 for (Entry<String, Command> entry : commands.entrySet()) { 71 String description = entry.getValue().getShortDescription(); 72 if (!showHidden && description.startsWith("@hide ")) { 73 continue; 74 } 75 stringBuilder.append(String.format(Locale.US, " %20s %s\n", entry.getKey(), description)); 76 } 77 78 return Futures.immediateFuture(stringBuilder.toString()); 79 } 80 runOrThrow(Command command)81 private static String runOrThrow(Command command) throws IllegalCommandLineArgumentException { 82 try { 83 return command.run(Arguments.EMPTY).get(); 84 } catch (InterruptedException e) { 85 Thread.interrupted(); 86 throw new RuntimeException(e); 87 } catch (ExecutionException e) { 88 throw new RuntimeException(e); 89 } 90 } 91 } 92