You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
1.8 KiB
63 lines
1.8 KiB
package com.crawler.command;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Scanner;
|
|
|
|
public class CommandController {
|
|
private List<Command> commands;
|
|
private Scanner scanner;
|
|
private CommandHistory history;
|
|
|
|
public CommandController() {
|
|
commands = new ArrayList<>();
|
|
scanner = new Scanner(System.in);
|
|
history = CommandHistory.getInstance();
|
|
initCommands();
|
|
}
|
|
|
|
private void initCommands() {
|
|
HelpCommand helpCmd = new HelpCommand(commands);
|
|
commands.add(helpCmd);
|
|
commands.add(new ListCommand());
|
|
commands.add(new CrawlCommand());
|
|
commands.add(new CacheCommand());
|
|
commands.add(new ExitCommand());
|
|
}
|
|
|
|
public void start() {
|
|
System.out.println("========================================");
|
|
System.out.println("Java爬虫框架 - 命令行模式");
|
|
System.out.println("========================================");
|
|
System.out.println("输入 'help' 查看可用指令");
|
|
System.out.println("========================================");
|
|
|
|
while (true) {
|
|
System.out.print("> ");
|
|
String input = scanner.nextLine().trim().toLowerCase();
|
|
|
|
if (input.isEmpty()) {
|
|
continue;
|
|
}
|
|
|
|
history.add(input);
|
|
executeCommand(input);
|
|
}
|
|
}
|
|
|
|
private void executeCommand(String commandName) {
|
|
for (Command cmd : commands) {
|
|
if (cmd.getName().equals(commandName)) {
|
|
cmd.execute();
|
|
return;
|
|
}
|
|
}
|
|
|
|
System.out.println("未知指令: " + commandName);
|
|
System.out.println("输入 'help' 查看可用指令");
|
|
}
|
|
|
|
public void stop() {
|
|
scanner.close();
|
|
}
|
|
}
|