View Javadoc
1   /*
2    *    Copyright 2010-2022 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
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 }