forked from Drive-for-Java/MyCMD
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.java
More file actions
112 lines (97 loc) · 4.49 KB
/
Copy pathApp.java
File metadata and controls
112 lines (97 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package com.mycmd;
import com.mycmd.commands.*;
import com.mycmd.utils.StringUtils;
import java.util.*;
import java.util.Scanner;
public class App {
public static void main(String[] args) {
ShellContext context = new ShellContext();
// Register commands
Map<String, Command> commands = new HashMap<>();
registerCommands(commands);
System.out.println("MyCMD [Version 1.0]");
System.out.println("(c) 2025 MyCMD Organization. All rights reserved.");
Scanner sc = new Scanner(System.in);
while (true) {
System.out.print(context.getCurrentDir().getAbsolutePath() + ">");
String input = sc.nextLine().trim();
if (input.isEmpty()) continue;
// Resolve aliases before processing
input = resolveAliases(input, context);
String[] parts = input.split("\\s+");
String cmd = parts[0].toLowerCase();
String[] cmdArgs = Arrays.copyOfRange(parts, 1, parts.length);
Command command = commands.get(cmd);
if (command != null) {
try {
command.execute(cmdArgs, context);
// Add to history after successful execution
context.addToHistory(input);
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
} else {
// Single, clear not-recognized message + optional suggestion
System.out.println("Unknown command: '" + cmd + "'. Enter 'help' to list all available commands.");
// compute suggestion safely
try {
List<String> validCommands = new ArrayList<>(commands.keySet());
String suggestion = StringUtils.findClosest(cmd, validCommands);
if (suggestion != null && !suggestion.equalsIgnoreCase(cmd)) {
System.out.println("Did you mean '" + suggestion + "'?");
}
} catch (Exception ex) {
// don't let suggestion errors break the shell
}
}
}
}
private static String resolveAliases(String input, ShellContext context) {
String[] parts = input.split("\\s+", 2);
String cmd = parts[0];
String rest = parts.length > 1 ? parts[1] : "";
// Check if the command is an alias
if (context.hasAlias(cmd)) {
String aliasCommand = context.getAlias(cmd);
// Replace the alias with its command, preserving arguments
return rest.isEmpty() ? aliasCommand : aliasCommand + " " + rest;
}
return input;
}
private static void registerCommands(Map<String, Command> commands) {
commands.put("dir", new DirCommand());
commands.put("cd", new CdCommand());
commands.put("echo", new EchoCommand());
commands.put("mkdir", new MkdirCommand());
commands.put("rmdir", new RmdirCommand());
commands.put("copy", new CopyCommand());
commands.put("del", new DelCommand());
commands.put("type", new TypeCommand());
commands.put("cls", new ClsCommand());
commands.put("help", new HelpCommand(commands));
commands.put("exit", new ExitCommand());
commands.put("ver", new VersionCommand());
commands.put("title", new TitleCommand());
commands.put("color", new ColorCommand());
commands.put("hostname", new HostnameCommand());
commands.put("whoami", new WhoamiCommand());
commands.put("touch", new TouchCommand());
commands.put("time", new TimeCommand());
commands.put("tasklist", new TasklistCommand());
commands.put("tree", new TreeCommand());
commands.put("date", new DateCommand());
commands.put("history", new HistoryCommand());
commands.put("ping", new PingCommand());
commands.put("telnet", new TelnetCommand());
commands.put("pwd", new PwdCommand());
commands.put("uptime", new UptimeCommand());
commands.put("clearhistory", new ClearHistoryCommand());
commands.put("ipconfig", new IpConfig());
commands.put("alias", new AliasCommand());
commands.put("unalias", new UnaliasCommand());
commands.put("rename", new RenameCommand());
commands.put("set", new SetCommand());
commands.put("systeminfo", new SysteminfoCommand());
commands.put("pause", new PauseCommand());
}
}