Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
CommandLineOption |
|
| 1.375;1.375 |
1 | /** | |
2 | * Licensed to the Apache Software Foundation (ASF) under one | |
3 | * or more contributor license agreements. See the NOTICE file | |
4 | * distributed with this work for additional information | |
5 | * regarding copyright ownership. The ASF licenses this file | |
6 | * to you under the Apache License, Version 2.0 (the | |
7 | * "License"); you may not use this file except in compliance | |
8 | * with the License. You may obtain a copy of the License at | |
9 | * | |
10 | * http://www.apache.org/licenses/LICENSE-2.0 | |
11 | * | |
12 | * Unless required by applicable law or agreed to in writing, | |
13 | * software distributed under the License is distributed on an | |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
15 | * KIND, either express or implied. See the License for the | |
16 | * specific language governing permissions and limitations | |
17 | * under the License. | |
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 | * Describes an option on the command line. | |
29 | */ | |
30 | 54 | public enum CommandLineOption { |
31 | ||
32 | /** License descriptor command line argument. */ | |
33 | 2 | LICENSE_DESCRIPTION("license-descriptor", 'l', |
34 | "use given license descriptor", true, "file", false), | |
35 | /** Application source command line argument. */ | |
36 | 2 | SOURCE("source", 's', "application source", false, "dir", false), |
37 | /** Generation command line argument. */ | |
38 | 2 | ACT_TO_GENERATE("generate", 'g', |
39 | "generate license and notice", false, null, true), | |
40 | /** Audit command line argument. */ | |
41 | 2 | ACT_TO_AUDIT("audit", 'a', "report audit details", false, null, true), |
42 | /** Generate skeleton mete-data command line argument. */ | |
43 | 2 | ACT_TO_SKELETON("skeleton", 't', |
44 | "generates skeleton meta-data", false, null, true), | |
45 | /** Print help then exit, ignoring other options. */ | |
46 | 2 | PRINT_HELP("help", 'h', "print help then exit, ignoring other options.", false, null, false); |
47 | ||
48 | /** | |
49 | * Creates options for the command line. | |
50 | * @return not null | |
51 | */ | |
52 | public static Options options() { | |
53 | 52 | final Options options = new Options(); |
54 | 52 | final OptionGroup acts = new OptionGroup(); |
55 | 52 | acts.setRequired(true); |
56 | 364 | for (final CommandLineOption option : values()) { |
57 | 312 | final Option cliOption = option.create(); |
58 | 312 | if (option.isAct) { |
59 | 156 | acts.addOption(cliOption); |
60 | } else { | |
61 | 156 | options.addOption(cliOption); |
62 | } | |
63 | } | |
64 | 52 | options.addOptionGroup(acts); |
65 | 52 | return options; |
66 | } | |
67 | ||
68 | /** The long name used for this command line argument. */ | |
69 | private final String longName; | |
70 | /** The character short for this command line argument.*/ | |
71 | private final char shortName; | |
72 | /** A description of this command line argument suitable for user.*/ | |
73 | private final String description; | |
74 | /** Is this a mandatory argument? */ | |
75 | private final boolean required; | |
76 | /** The argument name. */ | |
77 | private final String argument; | |
78 | /** Is this argument within the act group? */ | |
79 | private final boolean isAct; | |
80 | ||
81 | /** | |
82 | * Describes one argument. | |
83 | * @param longName not null | |
84 | * @param shortName not null | |
85 | * @param description not null | |
86 | * @param required is this mandatory? | |
87 | * @param argument possibly null | |
88 | * @param isAct is this argument an act? | |
89 | */ | |
90 | private CommandLineOption(final String longName, | |
91 | final char shortName, | |
92 | final String description, | |
93 | final boolean required, | |
94 | final String argument, | |
95 | 12 | final boolean isAct) { |
96 | 12 | this.longName = longName; |
97 | 12 | this.shortName = shortName; |
98 | 12 | this.description = description; |
99 | 12 | this.required = required; |
100 | 12 | this.argument = argument; |
101 | 12 | this.isAct = isAct; |
102 | 12 | } |
103 | ||
104 | /** | |
105 | * Gets the long name of this command line argument. | |
106 | * @return not null | |
107 | */ | |
108 | public String getLongName() { | |
109 | 406 | return longName; |
110 | } | |
111 | ||
112 | /** | |
113 | * Gets the short name of this command line argument. | |
114 | * @return the character short for this option | |
115 | */ | |
116 | public char getShortName() { | |
117 | 542 | return shortName; |
118 | } | |
119 | ||
120 | /** | |
121 | * Gets the description for this option. | |
122 | * @return not null | |
123 | */ | |
124 | public String getDescription() { | |
125 | 322 | return description; |
126 | } | |
127 | ||
128 | /** | |
129 | * Builds a representation. | |
130 | * @return not null | |
131 | */ | |
132 | @SuppressWarnings("static-access") | |
133 | public Option create() { | |
134 | 320 | final OptionBuilder builder = OptionBuilder |
135 | .isRequired(required) | |
136 | .withDescription(getDescription()) | |
137 | .withLongOpt(getLongName()); | |
138 | 320 | if (argument != null) { |
139 | 104 | builder.hasArg().withArgName(argument); |
140 | } | |
141 | 320 | return builder.create(getShortName()); |
142 | } | |
143 | ||
144 | /** | |
145 | * Gets an option value from the command line. | |
146 | * @param commandLine not null | |
147 | * @return not null | |
148 | */ | |
149 | public String getOptionValue(final CommandLine commandLine) { | |
150 | 92 | return commandLine.getOptionValue(getShortName()); |
151 | } | |
152 | ||
153 | /** | |
154 | * Is the short name option set? | |
155 | * @param commandLine not null | |
156 | * @return true when the option is present, false otherwise | |
157 | */ | |
158 | public boolean isSetOn(final CommandLine commandLine) { | |
159 | 80 | return commandLine.hasOption(getShortName()); |
160 | } | |
161 | } |