From fc1012e77d1dfc1b0450d9c3900e5f3ef6792bc6 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Tue, 28 May 2013 10:12:54 +0100 Subject: [PATCH] [bs-135] Remove support for option commands Command names stating with "--" work just as well. [#50427095] --- .../bootstrap/cli/Command.java | 8 +--- .../bootstrap/cli/NoSuchOptionException.java | 32 -------------- .../bootstrap/cli/SpringBootstrapCli.java | 41 +++++++----------- .../cli/command/AbstractCommand.java | 20 +-------- .../cli/command/OptionParsingCommand.java | 5 +-- .../command/ScriptCompilationCustomizer.java | 3 +- .../bootstrap/cli/command/VersionCommand.java | 2 +- .../cli/SpringBootstrapCliTests.java | 43 ++++++------------- 8 files changed, 35 insertions(+), 119 deletions(-) delete mode 100644 spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/NoSuchOptionException.java diff --git a/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/Command.java b/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/Command.java index 3ced0e65cf..22d9334f26 100644 --- a/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/Command.java +++ b/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/Command.java @@ -23,6 +23,7 @@ import java.io.PrintStream; * A single command that can be run from the CLI. * * @author Phillip Webb + * @author Dave Syer * @see #run(String...) */ public interface Command { @@ -32,13 +33,6 @@ public interface Command { */ String getName(); - /** - * Returns {@code true} if this is an 'option command'. An option command is a special - * type of command that usually makes more sense to present as if it is an option. For - * example '--help'. - */ - boolean isOptionCommand(); - /** * Returns a description of the command. */ diff --git a/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/NoSuchOptionException.java b/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/NoSuchOptionException.java deleted file mode 100644 index ef47911c23..0000000000 --- a/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/NoSuchOptionException.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright 2012-2013 the original author or authors. - * - * Licensed 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.springframework.bootstrap.cli; - -/** - * Exception thrown when an unknown root option is specified. This only applies to - * {@link Command#isOptionCommand() option command}. - * - * @author Phillip Webb - */ -class NoSuchOptionException extends BootstrapCliException { - - private static final long serialVersionUID = 1L; - - public NoSuchOptionException(String name) { - super("Unknown option: --" + name, Option.SHOW_USAGE); - } -} diff --git a/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/SpringBootstrapCli.java b/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/SpringBootstrapCli.java index f5841e0d31..099a0014d4 100644 --- a/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/SpringBootstrapCli.java +++ b/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/SpringBootstrapCli.java @@ -63,6 +63,7 @@ public class SpringBootstrapCli { this.commands.add(command); } } + this.commands.add(0, new HelpOptionCommand()); this.commands.add(0, new HelpCommand()); } @@ -73,6 +74,7 @@ public class SpringBootstrapCli { */ public void setCommands(List commands) { this.commands = new ArrayList(commands); + this.commands.add(0, new HelpOptionCommand()); this.commands.add(0, new HelpCommand()); } @@ -94,7 +96,8 @@ public class SpringBootstrapCli { Set options = NO_EXCEPTION_OPTIONS; if (ex instanceof BootstrapCliException) { options = ((BootstrapCliException) ex).getOptions(); - } else { + } + if (!(ex instanceof NoHelpCommandArgumentsException)) { errorMessage(ex.getMessage()); } if (options.contains(BootstrapCliException.Option.SHOW_USAGE)) { @@ -122,37 +125,23 @@ public class SpringBootstrapCli { } private Command find(String name) { - boolean isOption = name.startsWith("--"); - if (isOption) { - name = name.substring(2); - } for (Command candidate : this.commands) { - if ((isOption && candidate.isOptionCommand() || !isOption) - && candidate.getName().equals(name)) { + if (candidate.getName().equals(name)) { return candidate; } } - throw (isOption ? new NoSuchOptionException(name) : new NoSuchCommandException( - name)); + throw new NoSuchCommandException(name); } protected void showUsage() { System.out.print("usage: " + CLI_APP + " "); - for (Command command : this.commands) { - if (command.isOptionCommand()) { - System.out.print("[--" + command.getName() + "] "); - } - } System.out.println(""); System.out.println(" []"); System.out.println(""); System.out.println("Available commands are:"); for (Command command : this.commands) { - if (!command.isOptionCommand()) { - System.out.println(String.format("\n %1$s %2$-15s\n %3$s", - command.getName(), command.getUsageHelp(), - command.getDescription())); - } + System.out.println(String.format("\n %1$s %2$-15s\n %3$s", + command.getName(), command.getUsageHelp(), command.getDescription())); } System.out.println(""); System.out.println("See '" + CLI_APP @@ -179,6 +168,13 @@ public class SpringBootstrapCli { return rtn.toArray(new String[rtn.size()]); } + private class HelpOptionCommand extends HelpCommand { + @Override + public String getName() { + return "--help"; + } + } + /** * Internal {@link Command} used for 'help' and '--help' requests. */ @@ -191,7 +187,7 @@ public class SpringBootstrapCli { } String commandName = args[0]; for (Command command : SpringBootstrapCli.this.commands) { - if (!command.isOptionCommand() && command.getName().equals(commandName)) { + if (command.getName().equals(commandName)) { System.out.println(CLI_APP + " " + command.getName() + " - " + command.getDescription()); System.out.println(); @@ -212,11 +208,6 @@ public class SpringBootstrapCli { return "help"; } - @Override - public boolean isOptionCommand() { - return false; - } - @Override public String getDescription() { return "Get help on commands"; diff --git a/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/command/AbstractCommand.java b/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/command/AbstractCommand.java index 10c1f24880..f68259d901 100644 --- a/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/command/AbstractCommand.java +++ b/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/command/AbstractCommand.java @@ -25,13 +25,12 @@ import org.springframework.bootstrap.cli.Command; * Abstract {@link Command} implementation. * * @author Phillip Webb + * @author Dave Syer */ public abstract class AbstractCommand implements Command { private String name; - private boolean optionCommand; - private String description; /** @@ -40,20 +39,8 @@ public abstract class AbstractCommand implements Command { * @param description the command description */ public AbstractCommand(String name, String description) { - this(name, description, false); - } - - /** - * Create a new {@link AbstractCommand} instance. - * @param name the name of the command - * @param description the command description - * @param optionCommand if this command is an option command (see - * {@link Command#isOptionCommand()} - */ - public AbstractCommand(String name, String description, boolean optionCommand) { this.name = name; this.description = description; - this.optionCommand = optionCommand; } @Override @@ -66,11 +53,6 @@ public abstract class AbstractCommand implements Command { return this.description; } - @Override - public boolean isOptionCommand() { - return this.optionCommand; - } - @Override public String getUsageHelp() { return null; diff --git a/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/command/OptionParsingCommand.java b/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/command/OptionParsingCommand.java index 39b7b1493a..57cb381bf4 100644 --- a/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/command/OptionParsingCommand.java +++ b/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/command/OptionParsingCommand.java @@ -19,14 +19,13 @@ package org.springframework.bootstrap.cli.command; import java.io.IOException; import java.io.PrintStream; -import joptsimple.OptionParser; - import org.springframework.bootstrap.cli.Command; /** - * Base class for any {@link Command}s that use an {@link OptionParser}. + * Base class for any {@link Command}s that use an {@link OptionHandler}. * * @author Phillip Webb + * @author Dave Syer */ public abstract class OptionParsingCommand extends AbstractCommand { diff --git a/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/command/ScriptCompilationCustomizer.java b/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/command/ScriptCompilationCustomizer.java index bfbda0d782..f6c27fa9f9 100644 --- a/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/command/ScriptCompilationCustomizer.java +++ b/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/command/ScriptCompilationCustomizer.java @@ -22,6 +22,7 @@ import org.codehaus.groovy.control.CompilePhase; import org.codehaus.groovy.control.SourceUnit; import org.codehaus.groovy.control.customizers.CompilationCustomizer; import org.codehaus.groovy.control.customizers.ImportCustomizer; +import org.springframework.bootstrap.cli.Command; /** * @author Dave Syer @@ -43,7 +44,7 @@ public class ScriptCompilationCustomizer extends CompilationCustomizer { ImportCustomizer importCustomizer = new ImportCustomizer(); importCustomizer.addImports("joptsimple.OptionParser", "joptsimple.OptionSet", OptionParsingCommand.class.getCanonicalName(), - OptionHandler.class.getCanonicalName()); + Command.class.getCanonicalName(), OptionHandler.class.getCanonicalName()); importCustomizer.call(source, context, classNode); } diff --git a/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/command/VersionCommand.java b/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/command/VersionCommand.java index 821d8edc72..4b24bf6daf 100644 --- a/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/command/VersionCommand.java +++ b/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/command/VersionCommand.java @@ -26,7 +26,7 @@ import org.springframework.bootstrap.cli.Command; public class VersionCommand extends AbstractCommand { public VersionCommand() { - super("version", "Show the version", true); + super("--version", "Show the version"); } @Override diff --git a/spring-bootstrap-cli/src/test/java/org/springframework/bootstrap/cli/SpringBootstrapCliTests.java b/spring-bootstrap-cli/src/test/java/org/springframework/bootstrap/cli/SpringBootstrapCliTests.java index 5551dba98c..0b9e82e2f8 100644 --- a/spring-bootstrap-cli/src/test/java/org/springframework/bootstrap/cli/SpringBootstrapCliTests.java +++ b/spring-bootstrap-cli/src/test/java/org/springframework/bootstrap/cli/SpringBootstrapCliTests.java @@ -25,6 +25,7 @@ import static org.mockito.Mockito.verify; * Tests for {@link SpringBootstrapCli}. * * @author Phillip Webb + * @author Dave Syer */ public class SpringBootstrapCliTests { @@ -36,9 +37,6 @@ public class SpringBootstrapCliTests { @Mock private Command regularCommand; - @Mock - private Command optionCommand; - private Set calls = EnumSet.noneOf(Call.class); @Before @@ -66,10 +64,7 @@ public class SpringBootstrapCliTests { }; given(this.regularCommand.getName()).willReturn("command"); given(this.regularCommand.getDescription()).willReturn("A regular command"); - given(this.optionCommand.getName()).willReturn("option"); - given(this.optionCommand.getDescription()).willReturn("An optional command"); - given(this.optionCommand.isOptionCommand()).willReturn(true); - this.cli.setCommands(Arrays.asList(this.regularCommand, this.optionCommand)); + this.cli.setCommands(Arrays.asList(this.regularCommand)); } @Test @@ -84,24 +79,6 @@ public class SpringBootstrapCliTests { verify(this.regularCommand).run("--arg1", "arg2"); } - @Test - public void runOptionCommand() throws Exception { - this.cli.run("--option", "--arg1", "arg2"); - verify(this.optionCommand).run("--arg1", "arg2"); - } - - @Test - public void runOptionCommandWithoutOption() throws Exception { - this.cli.run("option", "--arg1", "arg2"); - verify(this.optionCommand).run("--arg1", "arg2"); - } - - @Test - public void runOptionOnNonOptionCommand() throws Exception { - this.thrown.expect(NoSuchOptionException.class); - this.cli.run("--command", "--arg1", "arg2"); - } - @Test public void missingCommand() throws Exception { this.thrown.expect(NoSuchCommandException.class); @@ -110,7 +87,7 @@ public class SpringBootstrapCliTests { @Test public void handlesSuccess() throws Exception { - int status = this.cli.runAndHandleErrors("--option"); + int status = this.cli.runAndHandleErrors("command"); assertThat(status, equalTo(0)); assertThat(this.calls, equalTo((Set) EnumSet.noneOf(Call.class))); } @@ -123,10 +100,10 @@ public class SpringBootstrapCliTests { } @Test - public void handlesNoSuchOptionException() throws Exception { - int status = this.cli.runAndHandleErrors("--missing"); + public void handlesNoSuchCommand() throws Exception { + int status = this.cli.runAndHandleErrors("missing"); assertThat(status, equalTo(1)); - assertThat(this.calls, equalTo((Set) EnumSet.of(Call.SHOW_USAGE))); + assertThat(this.calls, equalTo((Set) EnumSet.of(Call.ERROR_MESSAGE))); } @Test @@ -157,8 +134,6 @@ public class SpringBootstrapCliTests { @Test public void exceptionMessages() throws Exception { - assertThat(new NoSuchOptionException("name").getMessage(), - equalTo("Unknown option: --name")); assertThat(new NoSuchCommandException("name").getMessage(), equalTo("spring: 'name' is not a valid command. See 'spring --help'.")); } @@ -175,6 +150,12 @@ public class SpringBootstrapCliTests { this.cli.run("help"); } + @Test + public void helpLikeOption() throws Exception { + this.thrown.expect(NoHelpCommandArgumentsException.class); + this.cli.run("--help"); + } + @Test public void helpUnknownCommand() throws Exception { this.thrown.expect(NoSuchCommandException.class);