You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@creadur.apache.org by rd...@apache.org on 2011/08/07 15:40:21 UTC
svn commit: r1154705 - in /incubator/rat/whisker/trunk/legacy/src:
main/java/org/apache/rat/whisker/legacy/cli/CommandLineOption.java
main/java/org/apache/rat/whisker/legacy/cli/Main.java
test/java/org/apache/rat/whisker/legacy/cli/TestCommandParsing.java
Author: rdonkin
Date: Sun Aug 7 13:40:21 2011
New Revision: 1154705
URL: http://svn.apache.org/viewvc?rev=1154705&view=rev
Log:
Factor option details into enum.
Added:
incubator/rat/whisker/trunk/legacy/src/main/java/org/apache/rat/whisker/legacy/cli/CommandLineOption.java (with props)
Modified:
incubator/rat/whisker/trunk/legacy/src/main/java/org/apache/rat/whisker/legacy/cli/Main.java
incubator/rat/whisker/trunk/legacy/src/test/java/org/apache/rat/whisker/legacy/cli/TestCommandParsing.java
Added: incubator/rat/whisker/trunk/legacy/src/main/java/org/apache/rat/whisker/legacy/cli/CommandLineOption.java
URL: http://svn.apache.org/viewvc/incubator/rat/whisker/trunk/legacy/src/main/java/org/apache/rat/whisker/legacy/cli/CommandLineOption.java?rev=1154705&view=auto
==============================================================================
--- incubator/rat/whisker/trunk/legacy/src/main/java/org/apache/rat/whisker/legacy/cli/CommandLineOption.java (added)
+++ incubator/rat/whisker/trunk/legacy/src/main/java/org/apache/rat/whisker/legacy/cli/CommandLineOption.java Sun Aug 7 13:40:21 2011
@@ -0,0 +1,95 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.rat.whisker.legacy.cli;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.OptionBuilder;
+import org.apache.commons.cli.Options;
+
+/**
+ * Describes an option on the command line.
+ */
+public enum CommandLineOption {
+
+ LICENSE_DESCRIPTION("license-descriptor", 'l', "use given license descriptor", true, "file");
+
+ /**
+ * Creates options for the command line.
+ * @return not null
+ */
+ public static Options options() {
+ Options options = new Options();
+ for (final CommandLineOption option: values()) {
+ options.addOption(option.create());
+ }
+ return options;
+ }
+
+
+ private final String longName;
+ private final char shortName;
+ private final String description;
+ private final boolean required;
+ private final String argument;
+
+ private CommandLineOption(final String longName,
+ final char shortName,
+ final String description,
+ final boolean required,
+ final String argument) {
+ this.longName = longName;
+ this.shortName = shortName;
+ this.description = description;
+ this.required = required;
+ this.argument = argument;
+ }
+
+ public String getLongName() {
+ return longName;
+ }
+
+ public char getShortName() {
+ return shortName;
+ }
+
+ public String getDescription() {
+ return description;
+ }
+
+ @SuppressWarnings("static-access")
+ public Option create() {
+ final OptionBuilder builder = OptionBuilder
+ .isRequired(required)
+ .withDescription(getDescription())
+ .withLongOpt(getLongName());
+ if (argument != null) {
+ builder.hasArg().withArgName(argument);
+ }
+ return builder.create(getShortName());
+ }
+
+ /**
+ * @param commandLine
+ * @return
+ */
+ public String getOptionValue(CommandLine commandLine) {
+ return commandLine.getOptionValue(getShortName());
+ }
+}
\ No newline at end of file
Propchange: incubator/rat/whisker/trunk/legacy/src/main/java/org/apache/rat/whisker/legacy/cli/CommandLineOption.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: incubator/rat/whisker/trunk/legacy/src/main/java/org/apache/rat/whisker/legacy/cli/Main.java
URL: http://svn.apache.org/viewvc/incubator/rat/whisker/trunk/legacy/src/main/java/org/apache/rat/whisker/legacy/cli/Main.java?rev=1154705&r1=1154704&r2=1154705&view=diff
==============================================================================
--- incubator/rat/whisker/trunk/legacy/src/main/java/org/apache/rat/whisker/legacy/cli/Main.java (original)
+++ incubator/rat/whisker/trunk/legacy/src/main/java/org/apache/rat/whisker/legacy/cli/Main.java Sun Aug 7 13:40:21 2011
@@ -22,7 +22,6 @@ import org.apache.commons.cli.CommandLin
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter;
-import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.rat.whisker.legacy.app.Act;
@@ -38,8 +37,6 @@ public class Main {
*
*/
private static final String APP_NAME = "apache-rat-whisker";
- public static final String LICENSE_DESCRIPTOR_LONG_OPT = "license-descriptor";
- public static final char LICENSE_DESCRIPTOR_SHORT_OPT = 'l';
/**
* Returns okay to system.
@@ -73,19 +70,6 @@ public class Main {
public CommandLine parse(final String[] args) throws ParseException {
return parser().parse(options(), args);
}
-
- /**
- * @return not null
- */
- @SuppressWarnings("static-access")
- private Options options() {
- return new Options().addOption(OptionBuilder
- .withDescription("use given license descriptor")
- .hasArg().withArgName("file")
- .withLongOpt(LICENSE_DESCRIPTOR_LONG_OPT)
- .isRequired()
- .create(LICENSE_DESCRIPTOR_SHORT_OPT));
- }
public Whisker configure(final String[] args) throws ParseException {
return configure(parse(args));
@@ -97,7 +81,7 @@ public class Main {
* @return not null
*/
private Whisker configure(final CommandLine commandLine) {
- whisker.setLicenseDescriptor(commandLine.getOptionValue(LICENSE_DESCRIPTOR_SHORT_OPT));
+ whisker.setLicenseDescriptor(CommandLineOption.LICENSE_DESCRIPTION.getOptionValue(commandLine));
return whisker.setBase("war").setAct(Act.GENERATE);
}
@@ -119,4 +103,12 @@ public class Main {
final HelpFormatter formatter = new HelpFormatter();
formatter.printHelp(APP_NAME, options());
}
+
+ /**
+ * Builds options for the command line.
+ * @return not null
+ */
+ private Options options() {
+ return CommandLineOption.options();
+ }
}
Modified: incubator/rat/whisker/trunk/legacy/src/test/java/org/apache/rat/whisker/legacy/cli/TestCommandParsing.java
URL: http://svn.apache.org/viewvc/incubator/rat/whisker/trunk/legacy/src/test/java/org/apache/rat/whisker/legacy/cli/TestCommandParsing.java?rev=1154705&r1=1154704&r2=1154705&view=diff
==============================================================================
--- incubator/rat/whisker/trunk/legacy/src/test/java/org/apache/rat/whisker/legacy/cli/TestCommandParsing.java (original)
+++ incubator/rat/whisker/trunk/legacy/src/test/java/org/apache/rat/whisker/legacy/cli/TestCommandParsing.java Sun Aug 7 13:40:21 2011
@@ -18,16 +18,24 @@
*/
package org.apache.rat.whisker.legacy.cli;
+import junit.framework.TestCase;
+
import org.apache.commons.cli.ParseException;
import org.apache.rat.whisker.legacy.app.Whisker;
-import junit.framework.TestCase;
-
/**
*
*/
public class TestCommandParsing extends TestCase {
+ /**
+ *
+ */
+ private static final String LONG_OPT = "--";
+ /**
+ *
+ */
+ private static final String SHORT_OPT = "-";
private Main subject;
@Override
@@ -56,14 +64,32 @@ public class TestCommandParsing extends
* @param aPath
*/
private void exerciseShortLicenseDescriptionWithPath(String aPath) throws Exception {
- exerciseLicenseDescriptor(aPath, "-" + Main.LICENSE_DESCRIPTOR_SHORT_OPT);
+ exerciseLicenseDescriptor(aPath, shortOpt(CommandLineOption.LICENSE_DESCRIPTION.getShortName()));
+ }
+
+
+ /**
+ * @param licenseDescriptorShortOpt
+ * @return
+ */
+ private String shortOpt(char licenseDescriptorShortOpt) {
+ return SHORT_OPT + licenseDescriptorShortOpt;
}
/**
* @param aPath
*/
private void exerciseLongLicenseDescriptionWithPath(String aPath) throws Exception {
- exerciseLicenseDescriptor(aPath, "--" + Main.LICENSE_DESCRIPTOR_LONG_OPT);
+ exerciseLicenseDescriptor(aPath, longOpt(CommandLineOption.LICENSE_DESCRIPTION.getLongName()));
+ }
+
+
+ /**
+ * @param licenseDescriptorLongOpt
+ * @return
+ */
+ private String longOpt(String licenseDescriptorLongOpt) {
+ return LONG_OPT + licenseDescriptorLongOpt;
}