1 package org.cyclopsgroup.jmxterm.cc;
2
3 import java.beans.IntrospectionException;
4 import java.util.ArrayList;
5 import java.util.Collections;
6 import java.util.List;
7
8 import org.apache.commons.lang.Validate;
9 import org.cyclopsgroup.jcli.annotation.Argument;
10 import org.cyclopsgroup.jcli.annotation.Cli;
11 import org.cyclopsgroup.jcli.spi.CliUtils;
12 import org.cyclopsgroup.jmxterm.Command;
13
14
15
16
17
18
19 @Cli( name = "help", description = "Display available commands or usage of a command", note = "Run \"help [command1] [command2] ...\" to display usage or certain command(s). Help without argument shows list of available commands" )
20 public class HelpCommand
21 extends Command
22 {
23 private String[] argNames = {};
24
25 private CommandCenter commandCenter = null;
26
27
28
29
30 @Override
31 public void execute()
32 {
33 Validate.notNull( commandCenter, "Command center hasn't been set yet" );
34 if ( argNames.length == 0 )
35 {
36 List<String> commandNames = new ArrayList<String>( commandCenter.getCommandNames() );
37 Collections.sort( commandNames );
38 getSession().output.printMessage( "following commands are available to use:" );
39 for ( String commandName : commandNames )
40 {
41 Class<? extends Command> commandType = commandCenter.getCommandType( commandName );
42 Cli cli;
43 try
44 {
45 cli = CliUtils.defineCli( commandType ).getCli();
46 getSession().output.println( String.format( "%-8s - %s", commandName, cli.description() ) );
47 }
48 catch ( IntrospectionException e )
49 {
50 throw new RuntimeException( "Command type " + commandType + " has some problem", e );
51 }
52 }
53 }
54 else
55 {
56 for ( String argName : argNames )
57 {
58 Class<? extends Command> commandType = commandCenter.getCommandType( argName );
59 if ( commandType == null )
60 {
61 throw new IllegalArgumentException( "Command " + argName + " is not found" );
62 }
63 try
64 {
65 commandCenter.printUsage( commandType );
66 }
67 catch ( IntrospectionException e )
68 {
69 throw new RuntimeException( "Command type " + commandType + " has some problem", e );
70 }
71 }
72 }
73 }
74
75
76
77
78 @Argument
79 public final void setArgNames( String[] argNames )
80 {
81 Validate.notNull( argNames, "argNames can't be NULL" );
82 this.argNames = argNames;
83 }
84
85
86
87
88 final void setCommandCenter( CommandCenter commandCenter )
89 {
90 Validate.notNull( commandCenter, "commandCenter can't be NULL" );
91 this.commandCenter = commandCenter;
92 }
93 }