1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.creadur.whisker.cli;
20
21 import org.apache.commons.cli.CommandLine;
22 import org.apache.commons.cli.Option;
23 import org.apache.commons.cli.OptionBuilder;
24 import org.apache.commons.cli.OptionGroup;
25 import org.apache.commons.cli.Options;
26
27
28
29
30 public enum CommandLineOption {
31
32
33 LICENSE_DESCRIPTION("license-descriptor", 'l',
34 "use given license descriptor", true, "file", false),
35
36 SOURCE("source", 's', "application source", false, "dir", false),
37
38 ACT_TO_GENERATE("generate", 'g',
39 "generate license and notice", false, null, true),
40
41 ACT_TO_AUDIT("audit", 'a', "report audit details", false, null, true),
42
43 ACT_TO_SKELETON("skeleton", 't',
44 "generates skeleton meta-data", false, null, true),
45
46 PRINT_HELP("help", 'h', "print help then exit, ignoring other options.", false, null, false);
47
48
49
50
51
52 public static Options options() {
53 final Options options = new Options();
54 final OptionGroup acts = new OptionGroup();
55 acts.setRequired(true);
56 for (final CommandLineOption option : values()) {
57 final Option cliOption = option.create();
58 if (option.isAct) {
59 acts.addOption(cliOption);
60 } else {
61 options.addOption(cliOption);
62 }
63 }
64 options.addOptionGroup(acts);
65 return options;
66 }
67
68
69 private final String longName;
70
71 private final char shortName;
72
73 private final String description;
74
75 private final boolean required;
76
77 private final String argument;
78
79 private final boolean isAct;
80
81
82
83
84
85
86
87
88
89
90 private CommandLineOption(final String longName,
91 final char shortName,
92 final String description,
93 final boolean required,
94 final String argument,
95 final boolean isAct) {
96 this.longName = longName;
97 this.shortName = shortName;
98 this.description = description;
99 this.required = required;
100 this.argument = argument;
101 this.isAct = isAct;
102 }
103
104
105
106
107
108 public String getLongName() {
109 return longName;
110 }
111
112
113
114
115
116 public char getShortName() {
117 return shortName;
118 }
119
120
121
122
123
124 public String getDescription() {
125 return description;
126 }
127
128
129
130
131
132 @SuppressWarnings("static-access")
133 public Option create() {
134 final OptionBuilder builder = OptionBuilder
135 .isRequired(required)
136 .withDescription(getDescription())
137 .withLongOpt(getLongName());
138 if (argument != null) {
139 builder.hasArg().withArgName(argument);
140 }
141 return builder.create(getShortName());
142 }
143
144
145
146
147
148
149 public String getOptionValue(final CommandLine commandLine) {
150 return commandLine.getOptionValue(getShortName());
151 }
152
153
154
155
156
157
158 public boolean isSetOn(final CommandLine commandLine) {
159 return commandLine.hasOption(getShortName());
160 }
161 }