|
| 1 | +package com.mycmd; |
| 2 | + |
| 3 | +import com.mycmd.commands.*; |
| 4 | +import java.util.*; |
| 5 | + |
| 6 | +public class App { |
| 7 | + public static void main(String[] args) { |
| 8 | + ShellContext context = new ShellContext(); |
| 9 | + |
| 10 | + // Register commands |
| 11 | + Map<String, Command> commands = new HashMap<>(); |
| 12 | + registerCommands(commands); |
| 13 | + |
| 14 | + System.out.println("MyCMD [Version 1.0]"); |
| 15 | + System.out.println("(c) 2025 MyCMD Organization. All rights reserved."); |
| 16 | + |
| 17 | + Scanner sc = new Scanner(System.in); |
| 18 | + |
| 19 | + while (true) { |
| 20 | + System.out.print(context.getCurrentDir().getAbsolutePath() + ">"); |
| 21 | + String input = sc.nextLine().trim(); |
| 22 | + if (input.isEmpty()) continue; |
| 23 | + |
| 24 | + String[] parts = input.split("\\s+"); |
| 25 | + String cmd = parts[0].toLowerCase(); |
| 26 | + String[] cmdArgs = Arrays.copyOfRange(parts, 1, parts.length); |
| 27 | + |
| 28 | + Command command = commands.get(cmd); |
| 29 | + if (command != null) { |
| 30 | + try { |
| 31 | + command.execute(cmdArgs, context); |
| 32 | + } catch (Exception e) { |
| 33 | + System.out.println("Error: " + e.getMessage()); |
| 34 | + } |
| 35 | + } else { |
| 36 | + System.out.println("'" + cmd + "' is not recognized as an internal or external command."); |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + private static void registerCommands(Map<String, Command> commands) { |
| 42 | + commands.put("dir", new DirCommand()); |
| 43 | + commands.put("cd", new CdCommand()); |
| 44 | + commands.put("echo", new EchoCommand()); |
| 45 | + commands.put("mkdir", new MkdirCommand()); |
| 46 | + commands.put("rmdir", new RmdirCommand()); |
| 47 | + commands.put("copy", new CopyCommand()); |
| 48 | + commands.put("del", new DelCommand()); |
| 49 | + commands.put("type", new TypeCommand()); |
| 50 | + commands.put("cls", new ClsCommand()); |
| 51 | + commands.put("help", new HelpCommand(commands)); |
| 52 | + commands.put("exit", new ExitCommand()); |
| 53 | + commands.put("ver", new VersionCommand()); |
| 54 | + } |
| 55 | +} |
0 commit comments