Skip to content

Commit 58b9cce

Browse files
author
Thidas Senavirathna
committed
fixed compiling issue
1 parent fafba30 commit 58b9cce

File tree

3 files changed

+89
-64
lines changed

3 files changed

+89
-64
lines changed
Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,32 @@
11
package com.mycmd;
22

3-
import com.mycmd.ShellContext;
3+
import java.io.IOException;
44

5+
/**
6+
* Represents a shell command that can be executed inside the MyCMD shell.
7+
* Implementations perform their operation when {@link #execute(String[], ShellContext)} is called.
8+
*/
59
public interface Command {
6-
String getName();
7-
String getDescription();
810
/**
911
* Execute the command.
10-
* @param context shell context
11-
* @param args command arguments
12+
*
13+
* @param args command-line style arguments passed to the command. May be empty but will not be null.
14+
* @param context current shell context containing state such as the current working directory.
15+
* @throws IOException if the command cannot complete successfully.
1216
*/
13-
void execute(ShellContext context, String[] args) throws Exception;
14-
}
17+
void execute(String[] args, ShellContext context) throws IOException;
18+
19+
/**
20+
* Short description of the command. Default is empty so existing implementations do not break.
21+
*/
22+
default String description() {
23+
return "";
24+
}
25+
26+
/**
27+
* Usage string for the command. Default is empty.
28+
*/
29+
default String usage() {
30+
return "";
31+
}
32+
}
Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,69 @@
11
package com.mycmd.commands;
22

3+
import com.mycmd.Command;
34
import com.mycmd.ShellContext;
5+
6+
import java.io.IOException;
47
import java.util.Map;
58

69
public class AliasCommand implements Command {
710
@Override
8-
public void execute(String[] args, ShellContext context) {
9-
if (args.length == 0) {
10-
// List all aliases
11+
public void execute(String[] args, ShellContext context) throws IOException {
12+
// no args: list aliases
13+
if (args == null || args.length == 0) {
1114
Map<String, String> aliases = context.getAliases();
1215
if (aliases.isEmpty()) {
1316
System.out.println("No aliases defined.");
1417
} else {
15-
System.out.println("Current aliases:");
16-
for (Map.Entry<String, String> entry : aliases.entrySet()) {
17-
System.out.println(" " + entry.getKey() + "=\"" + entry.getValue() + "\"");
18-
}
19-
}
20-
} else {
21-
// Create alias: alias name="command" or alias name=command
22-
String input = String.join(" ", args);
23-
24-
if (!input.contains("=")) {
25-
System.out.println("Usage: alias name=\"command\"");
26-
System.out.println("Example: alias ll=\"dir /w\"");
27-
return;
18+
aliases.forEach((k, v) -> System.out.println(k + "=" + v));
2819
}
20+
return;
21+
}
2922

30-
String[] parts = input.split("=", 2);
23+
// single arg of form name=command
24+
if (args.length == 1 && args[0].contains("=")) {
25+
String[] parts = args[0].split("=", 2);
3126
String name = parts[0].trim();
32-
String command = parts[1].trim();
33-
34-
// Remove quotes if present
35-
if (command.startsWith("\"") && command.endsWith("\"")) {
36-
command = command.substring(1, command.length() - 1);
37-
}
38-
39-
// Validate alias name
40-
if (name.isEmpty() || name.contains(" ")) {
41-
System.out.println("Error: Alias name cannot be empty or contain spaces.");
27+
String cmd = parts[1].trim();
28+
if (name.isEmpty() || cmd.isEmpty()) {
29+
System.out.println("Invalid alias format. Usage: alias name=command");
4230
return;
4331
}
32+
context.addAlias(name, cmd);
33+
System.out.println("Alias added: " + name + "=" + cmd);
34+
return;
35+
}
4436

45-
if (command.isEmpty()) {
46-
System.out.println("Error: Alias command cannot be empty.");
47-
return;
37+
// multiple args: first is name, rest form command
38+
if (args.length >= 2) {
39+
String name = args[0];
40+
StringBuilder sb = new StringBuilder();
41+
for (int i = 1; i < args.length; i++) {
42+
if (i > 1) sb.append(' ');
43+
sb.append(args[i]);
4844
}
49-
50-
// Check for circular reference
51-
if (command.split("\\s+")[0].equalsIgnoreCase(name)) {
52-
System.out.println("Error: Cannot create circular alias reference.");
45+
String cmd = sb.toString();
46+
if (name.trim().isEmpty() || cmd.trim().isEmpty()) {
47+
System.out.println("Invalid alias. Usage: alias name command... or alias name=command");
5348
return;
5449
}
55-
56-
context.addAlias(name, command);
57-
System.out.println("Alias created: " + name + "=\"" + command + "\"");
50+
context.addAlias(name, cmd);
51+
System.out.println("Alias added: " + name + "=" + cmd);
52+
return;
5853
}
54+
55+
System.out.println("Invalid usage. Usage: alias [name=command] | alias [name command...]");
56+
}
57+
58+
@Override
59+
public String description() {
60+
return "Create or list command aliases.";
5961
}
6062

6163
@Override
62-
public String getHelp() {
63-
return "alias [name=\"command\"] - Create or list command aliases\n" +
64-
" alias - List all aliases\n" +
65-
" alias name=\"command\" - Create new alias\n" +
66-
" Example: alias ll=\"dir /w\"";
64+
public String usage() {
65+
return "alias # list aliases\n" +
66+
"alias name=command # create alias\n" +
67+
"alias name command... # create alias";
6768
}
6869
}
Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,36 @@
11
package com.mycmd.commands;
22

3+
import com.mycmd.Command;
34
import com.mycmd.ShellContext;
45

6+
import java.io.IOException;
7+
58
public class UnaliasCommand implements Command {
69
@Override
7-
public void execute(String[] args, ShellContext context) {
8-
if (args.length == 0) {
9-
System.out.println("Usage: unalias <name>");
10-
System.out.println("Example: unalias ll");
10+
public void execute(String[] args, ShellContext context) throws IOException {
11+
if (args == null || args.length == 0) {
12+
System.out.println("Usage: unalias name [name2 ...]");
1113
return;
1214
}
1315

14-
String aliasName = args[0].trim();
15-
16-
if (!context.hasAlias(aliasName)) {
17-
System.out.println("Error: Alias '" + aliasName + "' not found.");
18-
return;
16+
for (String name : args) {
17+
if (name == null || name.trim().isEmpty()) continue;
18+
if (context.hasAlias(name)) {
19+
context.removeAlias(name);
20+
System.out.println("Removed alias: " + name);
21+
} else {
22+
System.out.println("Alias not found: " + name);
23+
}
1924
}
25+
}
2026

21-
context.removeAlias(aliasName);
22-
System.out.println("Alias removed: " + aliasName);
27+
@Override
28+
public String description() {
29+
return "Remove one or more aliases.";
2330
}
2431

2532
@Override
26-
public String getHelp() {
27-
return "unalias <name> - Remove a command alias\n" +
28-
" Example: unalias ll";
33+
public String usage() {
34+
return "unalias name [name2 ...]";
2935
}
30-
}
36+
}

0 commit comments

Comments
 (0)