From e808676ece77445ff0cfb2e68a7ac1307d68fc1c Mon Sep 17 00:00:00 2001 From: studentpiyush Date: Wed, 15 Oct 2025 17:07:28 +0530 Subject: [PATCH 1/2] feat: enhanced dynamic help command with descriptions (fixes #5) --- .../java/com/mycmd/commands/HelpCommand.java | 57 +++++++++++++++++-- 1 file changed, 53 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/mycmd/commands/HelpCommand.java b/src/main/java/com/mycmd/commands/HelpCommand.java index f2fbab9..b264a0d 100644 --- a/src/main/java/com/mycmd/commands/HelpCommand.java +++ b/src/main/java/com/mycmd/commands/HelpCommand.java @@ -4,6 +4,16 @@ import com.mycmd.ShellContext; import java.util.Map; +/** + * Enhanced HelpCommand + * -------------------- + * Dynamically lists all available commands with their descriptions. + * + * Features: + * - Automatically updates when new commands are added to the registry. + * - Displays both command names and short descriptions. + * - Supports optional usage help: "help ". + */ public class HelpCommand implements Command { private final Map commands; @@ -13,9 +23,48 @@ 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); + // Case 1: user asked for detailed help about one 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()); + if (cmd.usage() != null && !cmd.usage().isEmpty()) { + System.out.println("Usage: " + cmd.usage()); + } else { + System.out.println("Usage: " + cmdName + " [options]"); + } + } else { + System.out.println("No such command: " + cmdName); + } + return; + } + + // Case 2: user just typed 'help' + 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 From 86658e3d9aecca1072b93ac7093d4c75045c0166 Mon Sep 17 00:00:00 2001 From: studentpiyush Date: Wed, 15 Oct 2025 17:23:37 +0530 Subject: [PATCH 2/2] Resolved merge conflicts in HelpCommand.java --- .../java/com/mycmd/commands/HelpCommand.java | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/mycmd/commands/HelpCommand.java b/src/main/java/com/mycmd/commands/HelpCommand.java index b264a0d..7b78072 100644 --- a/src/main/java/com/mycmd/commands/HelpCommand.java +++ b/src/main/java/com/mycmd/commands/HelpCommand.java @@ -8,11 +8,7 @@ * Enhanced HelpCommand * -------------------- * Dynamically lists all available commands with their descriptions. - * - * Features: - * - Automatically updates when new commands are added to the registry. - * - Displays both command names and short descriptions. - * - Supports optional usage help: "help ". + * Supports detailed help for individual commands using 'help '. */ public class HelpCommand implements Command { private final Map commands; @@ -23,7 +19,7 @@ public HelpCommand(Map commands) { @Override public void execute(String[] args, ShellContext context) { - // Case 1: user asked for detailed help about one command + // Detailed help for a specific command if (args.length > 1) { String cmdName = args[1]; Command cmd = commands.get(cmdName); @@ -31,30 +27,23 @@ public void execute(String[] args, ShellContext context) { if (cmd != null) { System.out.println("\nCommand: " + cmdName); System.out.println("Description: " + cmd.description()); - if (cmd.usage() != null && !cmd.usage().isEmpty()) { - System.out.println("Usage: " + cmd.usage()); - } else { - System.out.println("Usage: " + cmdName + " [options]"); - } + System.out.println("Usage: " + (cmd.usage() != null ? cmd.usage() : cmdName + " [options]")); } else { System.out.println("No such command: " + cmdName); } return; } - // Case 2: user just typed 'help' + // 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"); }