1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
29
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
42
43
44
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
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
88
89 private void validateCommandLineArguments() {
90
91 }
92
93
94
95
96
97
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 }