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
239 lines (223 loc) · 11.7 KB
/
Copy pathApp.java
File metadata and controls
239 lines (223 loc) · 11.7 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package com.mycmd;
import com.mycmd.commands.*;
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);
context.setScanner(sc); // ← Added this line
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 ARP = "arp";
private static final String ASSOC = "assoc";
private static final String ATTRIB = "attrib";
private static final String CD = "cd";
private static final String CHKDSK = "chkdsk";
private static final String CHOICE = "choice";
private static final String CLEARHISTORY = "clearhistory";
private static final String CLIP = "clip";
private static final String CLS = "cls";
private static final String COLOR = "color";
private static final String COMPACT = "compact";
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 DRIVERQUERY = "driverquery";
private static final String ECHO = "echo";
private static final String EXIT = "exit";
private static final String FC = "fc";
private static final String FIND = "find";
private static final String FINDSTR = "findstr";
private static final String FORFILES = "forfiles";
private static final String FSUTIL = "fsutil";
private static final String FTYPE = "ftype";
private static final String GETMAC = "getmac";
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 LABEL = "label";
private static final String LS = "ls";
private static final String MKDIR = "mkdir";
private static final String MORE = "more";
private static final String MOVE = "move";
private static final String MSG = "msg";
private static final String NET = "net";
private static final String NETSH = "netsh";
private static final String NETSTAT = "netstat";
private static final String NSLOOKUP = "nslookup";
private static final String PATH = "path";
private static final String PAUSE = "pause";
private static final String PING = "ping";
private static final String PWD = "pwd";
private static final String REM = "rem";
private static final String RENAME = "rename";
private static final String REPLACE = "replace";
private static final String RMDIR = "rmdir";
private static final String ROBOCOPY = "robocopy";
private static final String ROUTE = "route";
private static final String SET = "set";
private static final String SFC = "sfc";
private static final String SHUTDOWN = "shutdown";
private static final String SORT = "sort";
private static final String START = "start";
private static final String SYSTEMINFO = "systeminfo";
private static final String TASKKILL = "taskkill";
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 TRACERT = "tracert";
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 VERIFY = "verify";
private static final String VOL = "vol";
private static final String WHOAMI = "whoami";
private static final String WMIC = "wmic";
private static final String XCOPY = "xcopy";
private static final String SEARCHHISTORY = "searchhistory";
private static final String ISEARCH = "isearch";
}
private static void registerCommands(Map<String, Command> commands) {
commands.put(CommandNames.ALIAS, new AliasCommand());
commands.put(CommandNames.ARP, new ArpCommand());
commands.put(CommandNames.ASSOC, new AssocCommand());
commands.put(CommandNames.ATTRIB, new AttribCommand());
commands.put(CommandNames.CD, new CdCommand());
commands.put(CommandNames.CHKDSK, new ChkdskCommand());
commands.put(CommandNames.CHOICE, new ChoiceCommand());
commands.put(CommandNames.CLEARHISTORY, new ClearHistoryCommand());
commands.put(CommandNames.CLIP, new ClipCommand());
commands.put(CommandNames.CLS, new ClsCommand());
commands.put(CommandNames.COLOR, new ColorCommand());
commands.put(CommandNames.COMPACT, new CompactCommand());
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.DRIVERQUERY, new DriverqueryCommand());
commands.put(CommandNames.ECHO, new EchoCommand());
commands.put(CommandNames.EXIT, new ExitCommand());
commands.put(CommandNames.FC, new FcCommand());
commands.put(CommandNames.FIND, new FindCommand());
commands.put(CommandNames.FINDSTR, new FindstrCommand());
commands.put(CommandNames.FORFILES, new ForfilesCommand());
commands.put(CommandNames.FSUTIL, new FsutilCommand());
commands.put(CommandNames.FTYPE, new FtypeCommand());
commands.put(CommandNames.GETMAC, new GetmacCommand());
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.LABEL, new LabelCommand());
commands.put(CommandNames.LS, new LsCommand());
commands.put(CommandNames.MKDIR, new MkdirCommand());
commands.put(CommandNames.MORE, new MoreCommand());
commands.put(CommandNames.MOVE, new MoveCommand());
commands.put(CommandNames.MSG, new MsgCommand());
commands.put(CommandNames.NET, new NetCommand());
commands.put(CommandNames.NETSH, new NetshCommand());
commands.put(CommandNames.NETSTAT, new NetstatCommand());
commands.put(CommandNames.NSLOOKUP, new NslookupCommand());
commands.put(CommandNames.PATH, new PathCommand());
commands.put(CommandNames.PAUSE, new PauseCommand());
commands.put(CommandNames.PING, new PingCommand());
commands.put(CommandNames.PWD, new PwdCommand());
commands.put(CommandNames.REM, new RemCommand());
commands.put(CommandNames.RENAME, new RenameCommand());
commands.put(CommandNames.REPLACE, new ReplaceCommand());
commands.put(CommandNames.RMDIR, new RmdirCommand());
commands.put(CommandNames.ROBOCOPY, new RobocopyCommand());
commands.put(CommandNames.ROUTE, new RouteCommand());
commands.put(CommandNames.SET, new SetCommand());
commands.put(CommandNames.SFC, new SfcCommand());
commands.put(CommandNames.SHUTDOWN, new ShutdownCommand());
commands.put(CommandNames.SORT, new SortCommand());
commands.put(CommandNames.START, new StartCommand());
commands.put(CommandNames.SYSTEMINFO, new SysteminfoCommand());
commands.put(CommandNames.TASKKILL, new TaskkillCommand());
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.TRACERT, new TracertCommand());
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.VERIFY, new VerifyCommand());
commands.put(CommandNames.VOL, new VolCommand());
commands.put(CommandNames.WHOAMI, new WhoamiCommand());
commands.put(CommandNames.WMIC, new WmicCommand());
commands.put(CommandNames.XCOPY, new XcopyCommand());
commands.put(CommandNames.SEARCHHISTORY, new SearchHistoryCommand());
commands.put(CommandNames.ISEARCH, new InteractiveSearchCommand());
}
}