1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.migration.options;
17
18 import static org.apache.ibatis.migration.utils.Util.isOption;
19
20 import java.io.File;
21
22 public enum OptionsParser {
23 ;
24
25 public static SelectedOptions parse(String[] args) {
26 final SelectedOptions selectedOptions = new SelectedOptions();
27
28 for (String arg : args) {
29 final boolean isOption = isOption(arg);
30 if (isOption) {
31 parseOptions(arg, selectedOptions);
32 } else {
33 setCommandOrAppendParams(arg, selectedOptions);
34 }
35 }
36
37 return selectedOptions;
38 }
39
40 private static void setCommandOrAppendParams(String arg, SelectedOptions options) {
41 if (options.getCommand() == null) {
42 options.setCommand(arg);
43 } else {
44 final String myParams = options.getParams() == null ? arg : options.getParams() + " " + arg;
45 options.setParams(myParams);
46 }
47 }
48
49 private static boolean parseOptions(String arg, SelectedOptions options) {
50 final boolean isOption = isOption(arg);
51
52 if (isOption) {
53 final String[] argParts = arg.substring(2).split("=");
54 final Options option = Options.valueOf(argParts[0].toUpperCase());
55
56 switch (option) {
57 case PATH:
58 options.getPaths().setBasePath(new File(argParts[1]));
59 break;
60 case ENVPATH:
61 options.getPaths().setEnvPath(new File(argParts[1]));
62 break;
63 case SCRIPTPATH:
64 options.getPaths().setScriptPath(new File(argParts[1]));
65 break;
66 case DRIVERPATH:
67 options.getPaths().setDriverPath(new File(argParts[1]));
68 break;
69 case HOOKPATH:
70 options.getPaths().setHookPath(new File(argParts[1]));
71 break;
72 case ENV:
73 options.setEnvironment(argParts[1]);
74 break;
75 case FORCE:
76 options.setForce(true);
77 break;
78 case TRACE:
79 options.setTrace(true);
80 break;
81 case HELP:
82 options.setHelp(true);
83 break;
84 case TEMPLATE:
85 options.setTemplate(argParts[1]);
86 break;
87 case IDPATTERN:
88 options.setIdPattern(argParts[1]);
89 break;
90 case QUIET:
91 options.setQuiet(true);
92 break;
93 case COLOR:
94 options.setColor(true);
95 break;
96 }
97 }
98
99 return isOption;
100 }
101 }