5 changed files with 107 additions and 0 deletions
Binary file not shown.
@ -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<String, Command> 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); |
||||
|
} |
||||
|
} |
||||
Binary file not shown.
@ -0,0 +1,9 @@ |
|||||
|
package command; |
||||
|
|
||||
|
import exception.CrawlerException; |
||||
|
|
||||
|
public interface Command { |
||||
|
void execute() throws CrawlerException; |
||||
|
String getName(); |
||||
|
String getDescription(); |
||||
|
} |
||||
Binary file not shown.
Loading…
Reference in new issue