View Javadoc

1   /*
2    * Copyright 2013 University of Glasgow.
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    *      http://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 broadwick;
17  
18  import org.apache.commons.cli2.CommandLine;
19  import org.apache.commons.cli2.Group;
20  import org.apache.commons.cli2.Option;
21  import org.apache.commons.cli2.builder.ArgumentBuilder;
22  import org.apache.commons.cli2.builder.DefaultOptionBuilder;
23  import org.apache.commons.cli2.builder.GroupBuilder;
24  import org.apache.commons.cli2.commandline.Parser;
25  import org.apache.commons.cli2.util.HelpFormatter;
26  
27  /**
28   * Construct and read command line arguments. This class contains methods for extracting some of the main
29   * options such as configuration file etc.
30   */
31  public class CliOptions {
32  
33      private Group options;
34      private static final int LINEWIDTH = 120;
35      private static final String SPACE = " ";
36      private CommandLine cmdLine;
37      private Option configFileOpt;
38  
39  
40      /**
41       * Construct and provide GNU-compatible Options. Read the command line extracting the arguments, this
42       * additionally displays the help message if the command line is empty.
43       *
44       * @param args the command line arguments.
45       */
46      public CliOptions(final String[] args) {
47          buildCommandLineArguments();
48  
49          final Parser parser = new Parser();
50          parser.setGroup(options);
51          final HelpFormatter hf = new HelpFormatter(SPACE, SPACE, SPACE, LINEWIDTH);
52          parser.setHelpFormatter(hf);
53          parser.setHelpTrigger("--help");
54          cmdLine = parser.parseAndHelp(args);
55  
56          if (cmdLine == null) {
57              hf.printHeader();
58              throw new BroadwickException("Empty command line.");
59          }
60  
61          validateCommandLineArguments();
62      }
63  
64      /**
65       * Construct and provide GNU-compatible Options.
66       */
67      private void buildCommandLineArguments() {
68          final DefaultOptionBuilder oBuilder = new DefaultOptionBuilder();
69          final GroupBuilder gbuilder = new GroupBuilder();
70          final ArgumentBuilder abuilder = new ArgumentBuilder();
71  
72          final Option helpOpt = oBuilder.withLongName("help").withShortName("h").withRequired(false)
73                  .withDescription("Print this message").create();
74          final Option guiOpt = oBuilder.withLongName("gui").withShortName("x").withRequired(false)
75                  .withDescription("Use a gui to configure and run a model").create();
76          configFileOpt = oBuilder.withLongName("configFile").withShortName("c").withRequired(false)
77                  .withArgument(abuilder.withName("input").withMinimum(1).withMaximum(1).create())
78                  .withDescription("Use given configuration file").create();
79          options = gbuilder.withName("Options")
80                  .withOption(helpOpt)
81                  .withOption(configFileOpt)
82                  .withOption(guiOpt)
83                  .create();
84      }
85  
86      /**
87       * Perform validation the command line arguments.
88       */
89      private void validateCommandLineArguments() {
90          // nothing to validate (yet)
91      }
92  
93      /**
94       * Get the name of the configuration file specified on the command line.
95       *
96       * @return the name of the configuration file specified by either the -g or -c options. If neither
97       *         option is specified then we return an empty string.
98       */
99      public final String getConfigurationFileName() {
100         if (cmdLine.hasOption(configFileOpt)) {
101             return (String) cmdLine.getValue(configFileOpt);
102         } else {
103             return "";
104         }
105     }
106 
107 }