55import java .util .Map ;
66
77/**
8- * Displays a list of all available commands in the shell.
9- *
10- * This command provides users with an overview of commands registered in
11- * the shell. It requires access to the command registry (Map) which is
12- * provided during construction. Each command name is printed on a separate
13- * line with a bullet point prefix.
14- *
15- * Usage: help
16- *
17- * The command iterates through all keys in the command registry and displays
18- * them in the order provided by the map's key set.
8+ * Enhanced HelpCommand
9+ * --------------------
10+ * Dynamically lists all available commands with their descriptions.
11+ * Supports detailed help for individual commands using 'help <command>'.
1912 */
2013public class HelpCommand implements Command {
2114 private final Map <String , Command > commands ;
@@ -26,9 +19,41 @@ public HelpCommand(Map<String, Command> commands) {
2619
2720 @ Override
2821 public void execute (String [] args , ShellContext context ) {
29- System .out .println ("Available commands:" );
30- for (String key : commands .keySet ()) {
31- System .out .println (" - " + key );
22+ // Detailed help for a specific command
23+ if (args .length > 1 ) {
24+ String cmdName = args [1 ];
25+ Command cmd = commands .get (cmdName );
26+
27+ if (cmd != null ) {
28+ System .out .println ("\n Command: " + cmdName );
29+ System .out .println ("Description: " + cmd .description ());
30+ System .out .println ("Usage: " + (cmd .usage () != null ? cmd .usage () : cmdName + " [options]" ));
31+ } else {
32+ System .out .println ("No such command: " + cmdName );
33+ }
34+ return ;
35+ }
36+
37+ // General help listing all commands
38+ System .out .println ("\n MyCMD — Available Commands:\n " );
39+ for (Map .Entry <String , Command > entry : commands .entrySet ()) {
40+ String name = entry .getKey ();
41+ Command cmd = entry .getValue ();
42+ String description = (cmd .description () != null && !cmd .description ().isEmpty ())
43+ ? cmd .description ()
44+ : "No description available" ;
45+ System .out .printf (" %-12s : %s%n" , name , description );
3246 }
47+ System .out .println ("\n Type 'help <command>' for detailed info about a specific command.\n " );
48+ }
49+
50+ @ Override
51+ public String description () {
52+ return "Show list of available commands and their descriptions." ;
53+ }
54+
55+ @ Override
56+ public String usage () {
57+ return "help [command]" ;
3358 }
34- }
59+ }
0 commit comments