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