View Javadoc
1   package us.codecraft.webmagic.scripts.config;
2   
3   import java.util.List;
4   
5   import org.apache.commons.cli.CommandLine;
6   
7   import lombok.Getter;
8   import us.codecraft.webmagic.scripts.Params;
9   
10  public abstract class CommandLineOption {
11      @Getter
12      char option;
13  
14      public CommandLineOption(char option) {
15          this.option = option;
16      }
17  
18      protected abstract void addParamOption(Params params, CommandLine commandLine);
19  
20      public void addParamOptionIfInCommandLine(Params params, CommandLine commandLine) {
21          if (commandLine.hasOption(this.option))
22              this.addParamOption(params, commandLine);
23      }
24  
25      public static List<CommandLineOption> getAllOptions() {
26          return List.of(new OptionL(), new OptionF(), new OptionS(), new OptionT(), new OptionG());
27      }
28  }
29  
30  class OptionL extends CommandLineOption {
31      public OptionL() {
32          super('l');
33      }
34  
35      protected void addParamOption(Params params, CommandLine commandLine) {
36          String language = commandLine.getOptionValue("l");
37          params.setLanguagefromArg(language);
38      }
39  }
40  
41  class OptionF extends CommandLineOption {
42      public OptionF() {
43          super('f');
44      }
45  
46      protected void addParamOption(Params params, CommandLine commandLine) {
47          String scriptFilename = commandLine.getOptionValue("f");
48          params.setScriptFileName(scriptFilename);
49      }
50  }
51  
52  class OptionS extends CommandLineOption {
53      public OptionS() {
54          super('s');
55      }
56  
57      protected void addParamOption(Params params, CommandLine commandLine) {
58          Integer sleepTime = Integer.parseInt(commandLine.getOptionValue("s"));
59          params.setSleepTime(sleepTime);
60      }
61  }
62  
63  class OptionT extends CommandLineOption {
64      public OptionT() {
65          super('t');
66      }
67  
68      protected void addParamOption(Params params, CommandLine commandLine) {
69          Integer thread = Integer.parseInt(commandLine.getOptionValue("t"));
70          params.setThread(thread);
71      }
72  }
73  
74  class OptionG extends CommandLineOption {
75      public OptionG() {
76          super('g');
77      }
78  
79      protected void addParamOption(Params params, CommandLine commandLine) {
80          ConfigLogger.configLogger(commandLine.getOptionValue("g"));
81      }
82  }