001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.juneau.microservice.console; 018 019import static org.apache.juneau.commons.utils.Shorts.*; 020 021import java.io.*; 022import java.util.*; 023 024import org.apache.juneau.commons.runtime.*; 025import org.apache.juneau.marshall.cp.*; 026import org.apache.juneau.microservice.*; 027 028/** 029 * Implements the 'help' console command to provide help on other commands. 030 * 031 * <h5 class='section'>See Also:</h5><ul> 032 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauMicroservice">juneau-microservice</a> 033 * </ul> 034 */ 035public class HelpCommand extends ConsoleCommand { 036 037 private final Messages mb = Messages.of(HelpCommand.class, "Messages"); 038 039 @Override /* Overridden from ConsoleCommand */ 040 @SuppressWarnings({ 041 "java:S3776" // Cognitive complexity acceptable for console command logic 042 }) 043 public boolean execute(Scanner in, PrintWriter out, Args args) throws Exception { 044 var commands = Microservice.getInstance().getConsoleCommands(); 045 if (args.argCount() + args.optionCount() == 1) { 046 out.println(mb.getString("ListOfAvailableCommands")); 047 commands.forEach((k, v) -> out.append("\t").append(v.getName()).append(" -- ").append(indent(v.getInfo())).println()); 048 out.println(); 049 } else { 050 var cc = commands.get(args.get(1).orElse(null)); 051 if (cc == null) { 052 out.println(mb.getString("CommandNotFound")); 053 } else { 054 var info = cc.getInfo(); 055 var synopsis = cc.getSynopsis(); 056 var description = cc.getDescription(); 057 var examples = cc.getExamples(); 058 059 out.append(mb.getString("NAME")).append("\n\t").append(cc.getName()).append(info == null ? "" : " -- " + indent(info)).println(); 060 061 if (nn(synopsis)) 062 out.append('\n').append(mb.getString("SYNOPSIS")).append("\n\t").append(indent(synopsis)).println(); 063 064 if (nn(description)) 065 out.append('\n').append(mb.getString("DESCRIPTION")).append("\n\t").append(indent(description)).println(); 066 067 if (nn(examples)) 068 out.append('\n').append(mb.getString("EXAMPLES")).append("\n\t").append(indent(examples)).println(); 069 } 070 } 071 return false; 072 } 073 074 @Override /* Overridden from ConsoleCommand */ 075 public String getDescription() { return mb.getString("description"); } 076 077 @Override /* Overridden from ConsoleCommand */ 078 public String getExamples() { return mb.getString("examples"); } 079 080 @Override /* Overridden from ConsoleCommand */ 081 public String getInfo() { return mb.getString("info"); } 082 083 @Override /* Overridden from ConsoleCommand */ 084 public String getName() { return "help"; } 085 086 @Override /* Overridden from ConsoleCommand */ 087 public String getSynopsis() { return "help [command]"; } 088 089 private static String indent(String in) { 090 if (in == null) 091 return ""; 092 return in.replace("\n", "\n\t"); 093 } 094}