Skip to content

Commit c338ad3

Browse files
committed
feat: add pwd command and fix bug in help command
1 parent 9a128a1 commit c338ad3

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

src/main/java/com/mycmd/App.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,6 @@ private static void registerCommands(Map<String, Command> commands) {
7777
commands.put("tree", new TreeCommand());
7878
commands.put("date", new DateCommand());
7979
commands.put("history", new HistoryCommand());
80+
commands.put("pwd", new PwdCommand());
8081
}
8182
}

src/main/java/com/mycmd/commands/HelpCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public HelpCommand(Map<String, Command> commands) {
2020
@Override
2121
public void execute(String[] args, ShellContext context) {
2222
// Detailed help for a specific command
23-
if (args.length > 1) {
24-
String cmdName = args[1];
23+
if (args.length > 0) {
24+
String cmdName = args[0];
2525
Command cmd = commands.get(cmdName);
2626

2727
if (cmd != null) {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.mycmd.commands;
2+
3+
import com.mycmd.Command;
4+
import com.mycmd.ShellContext;
5+
6+
/**
7+
* Prints the current working directory.
8+
*
9+
* This command displays the absolute path of the current working directory
10+
* stored in the shell context. It's equivalent to the Unix/Linux 'pwd' command
11+
* and provides the same functionality as 'cd' without arguments.
12+
*
13+
* Usage:
14+
* - pwd : Print the current working directory path
15+
*
16+
* The command always prints the absolute path of the current directory,
17+
* making it useful for scripts and when you need to know your exact location
18+
* in the file system.
19+
*/
20+
public class PwdCommand implements Command {
21+
@Override
22+
public void execute(String[] args, ShellContext context) {
23+
System.out.println(context.getCurrentDir().getAbsolutePath());
24+
}
25+
26+
@Override
27+
public String description() {
28+
return "Print the current working directory path.";
29+
}
30+
31+
@Override
32+
public String usage() {
33+
return "pwd";
34+
}
35+
}

0 commit comments

Comments
 (0)