[bs-135] Remove support for option commands

Command names stating with "--" work just as well.

[#50427095]
pull/2/merge
Dave Syer 12 years ago
parent d950d15f9b
commit fc1012e77d

@ -23,6 +23,7 @@ import java.io.PrintStream;
* A single command that can be run from the CLI. * A single command that can be run from the CLI.
* *
* @author Phillip Webb * @author Phillip Webb
* @author Dave Syer
* @see #run(String...) * @see #run(String...)
*/ */
public interface Command { public interface Command {
@ -32,13 +33,6 @@ public interface Command {
*/ */
String getName(); 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. * Returns a description of the command.
*/ */

@ -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);
}
}

@ -63,6 +63,7 @@ public class SpringBootstrapCli {
this.commands.add(command); this.commands.add(command);
} }
} }
this.commands.add(0, new HelpOptionCommand());
this.commands.add(0, new HelpCommand()); this.commands.add(0, new HelpCommand());
} }
@ -73,6 +74,7 @@ public class SpringBootstrapCli {
*/ */
public void setCommands(List<? extends Command> commands) { public void setCommands(List<? extends Command> commands) {
this.commands = new ArrayList<Command>(commands); this.commands = new ArrayList<Command>(commands);
this.commands.add(0, new HelpOptionCommand());
this.commands.add(0, new HelpCommand()); this.commands.add(0, new HelpCommand());
} }
@ -94,7 +96,8 @@ public class SpringBootstrapCli {
Set<BootstrapCliException.Option> options = NO_EXCEPTION_OPTIONS; Set<BootstrapCliException.Option> options = NO_EXCEPTION_OPTIONS;
if (ex instanceof BootstrapCliException) { if (ex instanceof BootstrapCliException) {
options = ((BootstrapCliException) ex).getOptions(); options = ((BootstrapCliException) ex).getOptions();
} else { }
if (!(ex instanceof NoHelpCommandArgumentsException)) {
errorMessage(ex.getMessage()); errorMessage(ex.getMessage());
} }
if (options.contains(BootstrapCliException.Option.SHOW_USAGE)) { if (options.contains(BootstrapCliException.Option.SHOW_USAGE)) {
@ -122,37 +125,23 @@ public class SpringBootstrapCli {
} }
private Command find(String name) { private Command find(String name) {
boolean isOption = name.startsWith("--");
if (isOption) {
name = name.substring(2);
}
for (Command candidate : this.commands) { for (Command candidate : this.commands) {
if ((isOption && candidate.isOptionCommand() || !isOption) if (candidate.getName().equals(name)) {
&& candidate.getName().equals(name)) {
return candidate; return candidate;
} }
} }
throw (isOption ? new NoSuchOptionException(name) : new NoSuchCommandException( throw new NoSuchCommandException(name);
name));
} }
protected void showUsage() { protected void showUsage() {
System.out.print("usage: " + CLI_APP + " "); 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(" <command> [<args>]"); System.out.println(" <command> [<args>]");
System.out.println(""); System.out.println("");
System.out.println("Available commands are:"); System.out.println("Available commands are:");
for (Command command : this.commands) { for (Command command : this.commands) {
if (!command.isOptionCommand()) { System.out.println(String.format("\n %1$s %2$-15s\n %3$s",
System.out.println(String.format("\n %1$s %2$-15s\n %3$s", command.getName(), command.getUsageHelp(), command.getDescription()));
command.getName(), command.getUsageHelp(),
command.getDescription()));
}
} }
System.out.println(""); System.out.println("");
System.out.println("See '" + CLI_APP System.out.println("See '" + CLI_APP
@ -179,6 +168,13 @@ public class SpringBootstrapCli {
return rtn.toArray(new String[rtn.size()]); 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. * Internal {@link Command} used for 'help' and '--help' requests.
*/ */
@ -191,7 +187,7 @@ public class SpringBootstrapCli {
} }
String commandName = args[0]; String commandName = args[0];
for (Command command : SpringBootstrapCli.this.commands) { 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() + " - " System.out.println(CLI_APP + " " + command.getName() + " - "
+ command.getDescription()); + command.getDescription());
System.out.println(); System.out.println();
@ -212,11 +208,6 @@ public class SpringBootstrapCli {
return "help"; return "help";
} }
@Override
public boolean isOptionCommand() {
return false;
}
@Override @Override
public String getDescription() { public String getDescription() {
return "Get help on commands"; return "Get help on commands";

@ -25,13 +25,12 @@ import org.springframework.bootstrap.cli.Command;
* Abstract {@link Command} implementation. * Abstract {@link Command} implementation.
* *
* @author Phillip Webb * @author Phillip Webb
* @author Dave Syer
*/ */
public abstract class AbstractCommand implements Command { public abstract class AbstractCommand implements Command {
private String name; private String name;
private boolean optionCommand;
private String description; private String description;
/** /**
@ -40,20 +39,8 @@ public abstract class AbstractCommand implements Command {
* @param description the command description * @param description the command description
*/ */
public AbstractCommand(String name, String 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.name = name;
this.description = description; this.description = description;
this.optionCommand = optionCommand;
} }
@Override @Override
@ -66,11 +53,6 @@ public abstract class AbstractCommand implements Command {
return this.description; return this.description;
} }
@Override
public boolean isOptionCommand() {
return this.optionCommand;
}
@Override @Override
public String getUsageHelp() { public String getUsageHelp() {
return null; return null;

@ -19,14 +19,13 @@ package org.springframework.bootstrap.cli.command;
import java.io.IOException; import java.io.IOException;
import java.io.PrintStream; import java.io.PrintStream;
import joptsimple.OptionParser;
import org.springframework.bootstrap.cli.Command; 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 Phillip Webb
* @author Dave Syer
*/ */
public abstract class OptionParsingCommand extends AbstractCommand { public abstract class OptionParsingCommand extends AbstractCommand {

@ -22,6 +22,7 @@ import org.codehaus.groovy.control.CompilePhase;
import org.codehaus.groovy.control.SourceUnit; import org.codehaus.groovy.control.SourceUnit;
import org.codehaus.groovy.control.customizers.CompilationCustomizer; import org.codehaus.groovy.control.customizers.CompilationCustomizer;
import org.codehaus.groovy.control.customizers.ImportCustomizer; import org.codehaus.groovy.control.customizers.ImportCustomizer;
import org.springframework.bootstrap.cli.Command;
/** /**
* @author Dave Syer * @author Dave Syer
@ -43,7 +44,7 @@ public class ScriptCompilationCustomizer extends CompilationCustomizer {
ImportCustomizer importCustomizer = new ImportCustomizer(); ImportCustomizer importCustomizer = new ImportCustomizer();
importCustomizer.addImports("joptsimple.OptionParser", "joptsimple.OptionSet", importCustomizer.addImports("joptsimple.OptionParser", "joptsimple.OptionSet",
OptionParsingCommand.class.getCanonicalName(), OptionParsingCommand.class.getCanonicalName(),
OptionHandler.class.getCanonicalName()); Command.class.getCanonicalName(), OptionHandler.class.getCanonicalName());
importCustomizer.call(source, context, classNode); importCustomizer.call(source, context, classNode);
} }

@ -26,7 +26,7 @@ import org.springframework.bootstrap.cli.Command;
public class VersionCommand extends AbstractCommand { public class VersionCommand extends AbstractCommand {
public VersionCommand() { public VersionCommand() {
super("version", "Show the version", true); super("--version", "Show the version");
} }
@Override @Override

@ -25,6 +25,7 @@ import static org.mockito.Mockito.verify;
* Tests for {@link SpringBootstrapCli}. * Tests for {@link SpringBootstrapCli}.
* *
* @author Phillip Webb * @author Phillip Webb
* @author Dave Syer
*/ */
public class SpringBootstrapCliTests { public class SpringBootstrapCliTests {
@ -36,9 +37,6 @@ public class SpringBootstrapCliTests {
@Mock @Mock
private Command regularCommand; private Command regularCommand;
@Mock
private Command optionCommand;
private Set<Call> calls = EnumSet.noneOf(Call.class); private Set<Call> calls = EnumSet.noneOf(Call.class);
@Before @Before
@ -66,10 +64,7 @@ public class SpringBootstrapCliTests {
}; };
given(this.regularCommand.getName()).willReturn("command"); given(this.regularCommand.getName()).willReturn("command");
given(this.regularCommand.getDescription()).willReturn("A regular command"); given(this.regularCommand.getDescription()).willReturn("A regular command");
given(this.optionCommand.getName()).willReturn("option"); this.cli.setCommands(Arrays.asList(this.regularCommand));
given(this.optionCommand.getDescription()).willReturn("An optional command");
given(this.optionCommand.isOptionCommand()).willReturn(true);
this.cli.setCommands(Arrays.asList(this.regularCommand, this.optionCommand));
} }
@Test @Test
@ -84,24 +79,6 @@ public class SpringBootstrapCliTests {
verify(this.regularCommand).run("--arg1", "arg2"); 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 @Test
public void missingCommand() throws Exception { public void missingCommand() throws Exception {
this.thrown.expect(NoSuchCommandException.class); this.thrown.expect(NoSuchCommandException.class);
@ -110,7 +87,7 @@ public class SpringBootstrapCliTests {
@Test @Test
public void handlesSuccess() throws Exception { public void handlesSuccess() throws Exception {
int status = this.cli.runAndHandleErrors("--option"); int status = this.cli.runAndHandleErrors("command");
assertThat(status, equalTo(0)); assertThat(status, equalTo(0));
assertThat(this.calls, equalTo((Set<Call>) EnumSet.noneOf(Call.class))); assertThat(this.calls, equalTo((Set<Call>) EnumSet.noneOf(Call.class)));
} }
@ -123,10 +100,10 @@ public class SpringBootstrapCliTests {
} }
@Test @Test
public void handlesNoSuchOptionException() throws Exception { public void handlesNoSuchCommand() throws Exception {
int status = this.cli.runAndHandleErrors("--missing"); int status = this.cli.runAndHandleErrors("missing");
assertThat(status, equalTo(1)); assertThat(status, equalTo(1));
assertThat(this.calls, equalTo((Set<Call>) EnumSet.of(Call.SHOW_USAGE))); assertThat(this.calls, equalTo((Set<Call>) EnumSet.of(Call.ERROR_MESSAGE)));
} }
@Test @Test
@ -157,8 +134,6 @@ public class SpringBootstrapCliTests {
@Test @Test
public void exceptionMessages() throws Exception { public void exceptionMessages() throws Exception {
assertThat(new NoSuchOptionException("name").getMessage(),
equalTo("Unknown option: --name"));
assertThat(new NoSuchCommandException("name").getMessage(), assertThat(new NoSuchCommandException("name").getMessage(),
equalTo("spring: 'name' is not a valid command. See 'spring --help'.")); equalTo("spring: 'name' is not a valid command. See 'spring --help'."));
} }
@ -175,6 +150,12 @@ public class SpringBootstrapCliTests {
this.cli.run("help"); this.cli.run("help");
} }
@Test
public void helpLikeOption() throws Exception {
this.thrown.expect(NoHelpCommandArgumentsException.class);
this.cli.run("--help");
}
@Test @Test
public void helpUnknownCommand() throws Exception { public void helpUnknownCommand() throws Exception {
this.thrown.expect(NoSuchCommandException.class); this.thrown.expect(NoSuchCommandException.class);

Loading…
Cancel
Save