diff --git a/project/CLI.class b/project/CLI.class new file mode 100644 index 0000000..d47df84 Binary files /dev/null and b/project/CLI.class differ diff --git a/project/CLI.java b/project/CLI.java new file mode 100644 index 0000000..83341bd --- /dev/null +++ b/project/CLI.java @@ -0,0 +1,98 @@ +package cli; + +import command.Command; +import command.CrawlCommand; +import command.HelpCommand; +import command.ListCommand; +import controller.CrawlerController; +import exception.CLIException; +import exception.CrawlerException; +import view.CrawlerView; + +import java.util.HashMap; +import java.util.Map; + +public class CLI { + private final CrawlerController controller; + private final CrawlerView view; + private final Map commands; + + public CLI(CrawlerController controller, CrawlerView view) { + this.controller = controller; + this.view = view; + this.commands = new HashMap<>(); + initializeCommands(); + } + + private void initializeCommands() { + commands.put("help", new HelpCommand(controller)); + commands.put("list", new ListCommand(controller)); + commands.put("crawl", new CrawlCommand(controller)); + } + + public void process(String[] args) { + try { + Command command = parseArgs(args); + if (command != null) { + command.execute(); + } + } catch (CLIException e) { + view.displayError(e.getMessage()); + view.displayHelp(); + } catch (CrawlerException e) { + view.displayError(e.getMessage()); + } + } + + private Command parseArgs(String[] args) throws CLIException { + if (args == null || args.length == 0) { + return new CrawlCommand(controller); + } + + String strategy = "all"; + boolean verbose = false; + String outputDir = null; + + for (int i = 0; i < args.length; i++) { + String arg = args[i]; + + switch (arg) { + case "-h": + case "--help": + return new HelpCommand(controller); + + case "-l": + case "--list": + return new ListCommand(controller); + + case "-s": + case "--strategy": + if (i + 1 >= args.length) { + throw new CLIException("缺少策略参数值", arg); + } + strategy = args[++i]; + break; + + case "-v": + case "--verbose": + verbose = true; + controller.setVerbose(true); + break; + + case "-o": + case "--output": + if (i + 1 >= args.length) { + throw new CLIException("缺少输出目录参数值", arg); + } + outputDir = args[++i]; + controller.setOutputDir(outputDir); + break; + + default: + throw new CLIException("未知的参数: " + arg, arg); + } + } + + return new CrawlCommand(controller, strategy); + } +} \ No newline at end of file diff --git a/project/Command.class b/project/Command.class new file mode 100644 index 0000000..7f806b8 Binary files /dev/null and b/project/Command.class differ diff --git a/project/Command.java b/project/Command.java new file mode 100644 index 0000000..8e10c4c --- /dev/null +++ b/project/Command.java @@ -0,0 +1,9 @@ +package command; + +import exception.CrawlerException; + +public interface Command { + void execute() throws CrawlerException; + String getName(); + String getDescription(); +} \ No newline at end of file diff --git a/project/CrawlCommand.class b/project/CrawlCommand.class new file mode 100644 index 0000000..d7ad565 Binary files /dev/null and b/project/CrawlCommand.class differ