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
152 lines (136 loc) · 6.86 KB
/
Copy pathApp.java
File metadata and controls
152 lines (136 loc) · 6.86 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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 '" + CommandNames.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 final class CommandNames {
private CommandNames() {}
private static final String ALIAS = "alias";
private static final String CD = "cd";
private static final String CLEARHISTORY = "clearhistory";
private static final String CLS = "cls";
private static final String COLOR = "color";
private static final String COPY = "copy";
private static final String DATE = "date";
private static final String DEL = "del";
private static final String DIR = "dir";
private static final String ECHO = "echo";
private static final String EXIT = "exit";
private static final String HELP = "help";
private static final String HISTORY = "history";
private static final String HOSTNAME = "hostname";
private static final String IPCONFIG = "ipconfig";
private static final String MKDIR = "mkdir";
private static final String PAUSE = "pause";
private static final String PING = "ping";
private static final String PWD = "pwd";
private static final String RENAME = "rename";
private static final String RMDIR = "rmdir";
private static final String SET = "set";
private static final String SYSTEMINFO = "systeminfo";
private static final String TASKLIST = "tasklist";
private static final String TELNET = "telnet";
private static final String TIME = "time";
private static final String TIMEOUT = "timeout";
private static final String TITLE = "title";
private static final String TOUCH = "touch";
private static final String TREE = "tree";
private static final String TYPE = "type";
private static final String UNALIAS = "unalias";
private static final String UPTIME = "uptime";
private static final String VER = "ver";
private static final String WHOAMI = "whoami";
}
private static void registerCommands(Map<String, Command> commands) {
commands.put(CommandNames.ALIAS, new AliasCommand());
commands.put(CommandNames.CD, new CdCommand());
commands.put(CommandNames.CLEARHISTORY, new ClearHistoryCommand());
commands.put(CommandNames.CLS, new ClsCommand());
commands.put(CommandNames.COLOR, new ColorCommand());
commands.put(CommandNames.COPY, new CopyCommand());
commands.put(CommandNames.DATE, new DateCommand());
commands.put(CommandNames.DEL, new DelCommand());
commands.put(CommandNames.DIR, new DirCommand());
commands.put(CommandNames.ECHO, new EchoCommand());
commands.put(CommandNames.EXIT, new ExitCommand());
commands.put(CommandNames.HELP, new HelpCommand(commands));
commands.put(CommandNames.HISTORY, new HistoryCommand());
commands.put(CommandNames.HOSTNAME, new HostnameCommand());
commands.put(CommandNames.IPCONFIG, new IpConfig());
commands.put(CommandNames.MKDIR, new MkdirCommand());
commands.put(CommandNames.PAUSE, new PauseCommand());
commands.put(CommandNames.PING, new PingCommand());
commands.put(CommandNames.PWD, new PwdCommand());
commands.put(CommandNames.RENAME, new RenameCommand());
commands.put(CommandNames.RMDIR, new RmdirCommand());
commands.put(CommandNames.SET, new SetCommand());
commands.put(CommandNames.SYSTEMINFO, new SysteminfoCommand());
commands.put(CommandNames.TASKLIST, new TasklistCommand());
commands.put(CommandNames.TELNET, new TelnetCommand());
commands.put(CommandNames.TIME, new TimeCommand());
commands.put(CommandNames.TIMEOUT, new TimeoutCommand());
commands.put(CommandNames.TITLE, new TitleCommand());
commands.put(CommandNames.TOUCH, new TouchCommand());
commands.put(CommandNames.TREE, new TreeCommand());
commands.put(CommandNames.TYPE, new TypeCommand());
commands.put(CommandNames.UNALIAS, new UnaliasCommand());
commands.put(CommandNames.UPTIME, new UptimeCommand());
commands.put(CommandNames.VER, new VersionCommand());
commands.put(CommandNames.WHOAMI, new WhoamiCommand());
}
}