Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 40 additions & 15 deletions src/main/java/com/mycmd/commands/HelpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <command>'.
*/
public class HelpCommand implements Command {
private final Map<String, Command> commands;
Expand All @@ -26,9 +19,41 @@ public HelpCommand(Map<String, Command> 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<String, Command> 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 <command>' 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]";
}
}
}