Skip to content

Commit 852ad98

Browse files
committed
pwd command with error handling
- Added PwdCommand class to display the current working directory. The implementation ensures error handling for unsupported flags or extra arguments, adhering to the typical behavior of the Unix pwd command.
1 parent d80017a commit 852ad98

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed
Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
package com.unixtools.command.filemanagement;
22

3-
public class PwdCommand {
4-
3+
import com.unixtools.core.Command;
4+
import java.nio.file.Paths;
5+
6+
public class PwdCommand implements Command {
7+
@Override
8+
public void execute(String[] args) {
9+
if (args.length > 0) {
10+
System.out.println("Invalid usage. No flags/args are supported for pwd.");
11+
return;
12+
}
13+
14+
String currentDir = getCurrentDirectory();
15+
System.out.println(currentDir);
16+
}
17+
18+
private String getCurrentDirectory() {
19+
return Paths.get("").toAbsolutePath().normalize().toString();
20+
}
521
}

src/com/unixtools/core/CommandFactory.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public static Command getCommand(String commandName) {
1919
return new MvCommand();
2020
case "touch":
2121
return new TouchCommand();
22+
case "pwd":
23+
return new PwdCommand();
2224
default:
2325
return null;
2426
}

0 commit comments

Comments
 (0)