package com.example.datacollect; import java.util.Scanner; // Controller层:处理用户输入、调用工具类/模型,是程序的入口 public class CommandController { public static void main(String[] args) { // 在CommandController的main方法开头 System.out.println("===== 暗色主题模式 ====="); System.out.println("背景色:" + ThemeConfig.BACKGROUND_COLOR); System.out.println("文字色:" + ThemeConfig.TEXT_COLOR); // 1. 创建扫描器,接收用户输入 Scanner scanner = new Scanner(System.in); System.out.println("===== 命令行工具 ====="); System.out.println("指令说明:c/crawl=执行爬虫 | exit=退出 | 其他=未知命令"); // 2. 循环接收命令(直到输入exit) while (true) { System.out.print("\n请输入命令:"); String inputCommand = scanner.nextLine().trim(); // 去除首尾空格 // 3. 把命令记录到历史(调用HistoryCommand的方法) HistoryCommand.addCommand(inputCommand); // 在CommandController的while循环中,新增else if分支 if (inputCommand.startsWith("url ")) { // 比如输入:url https://www.baidu.com // 截取URL部分(去掉"url "前缀) String url = inputCommand.substring(4).trim(); if (UrlValidator.isValidUrl(url)) { System.out.println("✅ URL格式合法:" + url); } else { System.out.println("❌ URL格式非法:" + url); } } // 4. 命令别名:c 等价于 crawl if (inputCommand.equals("c") || inputCommand.equals("crawl")) { System.out.println("✅ 执行爬虫命令..."); // 这里可以后续扩展:调用爬虫逻辑,比如爬取文章并封装到Article } else if (inputCommand.equals("exit")) { System.out.println("❌ 退出程序,历史命令如下:"); // 打印所有历史命令 System.out.println(HistoryCommand.getCommandHistory()); break; // 退出循环,结束程序 } else { System.out.println("❓ 未知命令,请重新输入!"); } } // 5. 关闭扫描器(释放资源) scanner.close(); } }