diff --git a/src/main/java/com/mycmd/commands/HelpCommand.java b/src/main/java/com/mycmd/commands/HelpCommand.java index 93b1408..7b78072 100644 --- a/src/main/java/com/mycmd/commands/HelpCommand.java +++ b/src/main/java/com/mycmd/commands/HelpCommand.java @@ -5,17 +5,10 @@ import java.util.Map; /** - * Displays a list of all available commands in the shell. - * - * This command provides users with an overview of commands registered in - * the shell. It requires access to the command registry (Map) which is - * provided during construction. Each command name is printed on a separate - * line with a bullet point prefix. - * - * Usage: help - * - * The command iterates through all keys in the command registry and displays - * them in the order provided by the map's key set. + * Enhanced HelpCommand + * -------------------- + * Dynamically lists all available commands with their descriptions. + * Supports detailed help for individual commands using 'help '. */ public class HelpCommand implements Command { private final Map commands; @@ -26,9 +19,41 @@ public HelpCommand(Map commands) { @Override public void execute(String[] args, ShellContext context) { - System.out.println("Available commands:"); - for (String key : commands.keySet()) { - System.out.println(" - " + key); + // Detailed help for a specific command + if (args.length > 1) { + String cmdName = args[1]; + Command cmd = commands.get(cmdName); + + if (cmd != null) { + System.out.println("\nCommand: " + cmdName); + System.out.println("Description: " + cmd.description()); + System.out.println("Usage: " + (cmd.usage() != null ? cmd.usage() : cmdName + " [options]")); + } else { + System.out.println("No such command: " + cmdName); + } + return; + } + + // General help listing all commands + System.out.println("\nMyCMD — Available Commands:\n"); + for (Map.Entry entry : commands.entrySet()) { + String name = entry.getKey(); + Command cmd = entry.getValue(); + String description = (cmd.description() != null && !cmd.description().isEmpty()) + ? cmd.description() + : "No description available"; + System.out.printf(" %-12s : %s%n", name, description); } + System.out.println("\nType 'help ' for detailed info about a specific command.\n"); + } + + @Override + public String description() { + return "Show list of available commands and their descriptions."; + } + + @Override + public String usage() { + return "help [command]"; } -} +} \ No newline at end of file