Skip to content

Commit

Permalink
Wrap CLI Options API to avoid direct dependency on JOpt lib in commands
Browse files Browse the repository at this point in the history
#17

* renaming;
  • Loading branch information
andrus committed Jan 27, 2016
1 parent 52c5f1c commit 63654af
Show file tree
Hide file tree
Showing 12 changed files with 33 additions and 33 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/nhl/bootique/BQCoreModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.google.inject.Module;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import com.nhl.bootique.cli.Options;
import com.nhl.bootique.cli.CommandLine;
import com.nhl.bootique.command.Command;
import com.nhl.bootique.command.ConfigCommand;
import com.nhl.bootique.command.DefaultCommand;
Expand Down Expand Up @@ -52,7 +52,7 @@ public void configure(Binder binder) {
binder.bind(Runner.class).to(DefaultRunner.class).in(Singleton.class);
binder.bind(ShutdownManager.class).to(DefaultShutdownManager.class).in(Singleton.class);
binder.bind(Duration.class).annotatedWith(ShutdownTimeout.class).toInstance(shutdownTimeout);
binder.bind(Options.class).toProvider(JoptOptionsProvider.class).in(Singleton.class);
binder.bind(CommandLine.class).toProvider(JoptOptionsProvider.class).in(Singleton.class);
binder.bind(ConfigurationSource.class).to(CliConfigurationSource.class).in(Singleton.class);

binder.bind(ConfigurationFactory.class).to(YamlConfigurationFactory.class).in(Singleton.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import java.util.List;

/**
* An object that represents a set of command line options passed to Booqtie
* app.
* An object that represents a set of command-line options passed to the
* Bootique app.
*
* @since 0.12
*/
public interface Options {
public interface CommandLine {

// TODO: this probably does not belong here.. instead we should be able to
// extract all options and print them using external renderer
Expand All @@ -30,5 +30,5 @@ public interface Options {
* Returns all arguments that are not options or option values in the order
* they are encountered on the command line.
*/
List<String> nonOptionArgs();
List<String> standaloneArguments();
}
4 changes: 2 additions & 2 deletions src/main/java/com/nhl/bootique/command/Command.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.nhl.bootique.command;

import com.nhl.bootique.cli.OptionsBuilder;
import com.nhl.bootique.cli.Options;
import com.nhl.bootique.cli.CommandLine;

@FunctionalInterface
public interface Command {
Expand All @@ -16,7 +16,7 @@ public interface Command {
* with command chain.
* @since 1.12
*/
CommandOutcome run(Options options);
CommandOutcome run(CommandLine options);

/**
* Allows subclasses to configure visible CLI options.
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/nhl/bootique/command/ConfigCommand.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.nhl.bootique.command;

import com.nhl.bootique.cli.Options;
import com.nhl.bootique.cli.CommandLine;
import com.nhl.bootique.cli.OptionsBuilder;

public class ConfigCommand implements Command {

public static final String CONFIG_OPTION = "config";

@Override
public CommandOutcome run(Options options) {
public CommandOutcome run(CommandLine options) {
return CommandOutcome.skipped();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.nhl.bootique.command;

import com.google.inject.Inject;
import com.nhl.bootique.cli.Options;
import com.nhl.bootique.cli.CommandLine;
import com.nhl.bootique.log.BootLogger;

/**
Expand All @@ -17,7 +17,7 @@ public FailoverHelpCommand(BootLogger bootLogger) {
}

@Override
public CommandOutcome run(Options options) {
public CommandOutcome run(CommandLine options) {
return printHelp(options);
}
}
6 changes: 3 additions & 3 deletions src/main/java/com/nhl/bootique/command/HelpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.io.StringWriter;

import com.google.inject.Inject;
import com.nhl.bootique.cli.Options;
import com.nhl.bootique.cli.CommandLine;
import com.nhl.bootique.cli.OptionsBuilder;
import com.nhl.bootique.log.BootLogger;

Expand All @@ -19,11 +19,11 @@ public HelpCommand(BootLogger bootLogger) {
}

@Override
public CommandOutcome run(Options options) {
public CommandOutcome run(CommandLine options) {
return options.hasOption(HELP_OPTION) ? printHelp(options) : CommandOutcome.skipped();
}

protected CommandOutcome printHelp(Options options) {
protected CommandOutcome printHelp(CommandLine options) {
StringWriter out = new StringWriter();
options.printHelp(out);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package com.nhl.bootique.command;

import com.nhl.bootique.cli.Options;
import com.nhl.bootique.cli.CommandLine;

public abstract class OptionTriggeredCommand implements Command {

@Override
public final CommandOutcome run(Options options) {
public final CommandOutcome run(CommandLine options) {
return hasOption(options) ? doRun(options) : CommandOutcome.skipped();
}

protected boolean hasOption(Options options) {
protected boolean hasOption(CommandLine options) {
return options.hasOption(getOption());
}

protected abstract CommandOutcome doRun(Options options);
protected abstract CommandOutcome doRun(CommandLine options);

protected abstract String getOption();
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.util.function.Function;

import com.google.inject.Inject;
import com.nhl.bootique.cli.Options;
import com.nhl.bootique.cli.CommandLine;
import com.nhl.bootique.command.ConfigCommand;
import com.nhl.bootique.log.BootLogger;

Expand All @@ -21,7 +21,7 @@ public class CliConfigurationSource implements ConfigurationSource {
private String location;

@Inject
public CliConfigurationSource(Options options, BootLogger bootLogger) {
public CliConfigurationSource(CommandLine options, BootLogger bootLogger) {

Collection<String> configs = options.optionStrings(ConfigCommand.CONFIG_OPTION);
if (configs.isEmpty()) {
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/nhl/bootique/jopt/JoptOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@

import javax.swing.JOptionPane;

import com.nhl.bootique.cli.Options;
import com.nhl.bootique.cli.CommandLine;
import com.nhl.bootique.log.BootLogger;

import joptsimple.OptionParser;
import joptsimple.OptionSet;

/**
* {@link Options} implementation on top of {@link JOptionPane} library.
* {@link CommandLine} implementation on top of {@link JOptionPane} library.
*/
public class JoptOptions implements Options {
public class JoptOptions implements CommandLine {

private OptionParser parser;
private OptionSet optionSet;
Expand Down Expand Up @@ -50,7 +50,7 @@ public List<String> optionStrings(String optionName) {

@SuppressWarnings("unchecked")
@Override
public List<String> nonOptionArgs() {
public List<String> standaloneArguments() {
return (List<String>) optionSet.nonOptionArguments();
}
}
6 changes: 3 additions & 3 deletions src/main/java/com/nhl/bootique/jopt/JoptOptionsProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

import com.google.inject.Inject;
import com.google.inject.Provider;
import com.nhl.bootique.cli.Options;
import com.nhl.bootique.cli.CommandLine;
import com.nhl.bootique.command.Command;
import com.nhl.bootique.log.BootLogger;

import joptsimple.OptionParser;

public class JoptOptionsProvider implements Provider<Options> {
public class JoptOptionsProvider implements Provider<CommandLine> {

private String[] args;
private Collection<Command> commands;
Expand All @@ -25,7 +25,7 @@ public JoptOptionsProvider(BootLogger bootLogger, Set<Command> commands, @Args S
}

@Override
public Options get() {
public CommandLine get() {
OptionParser parser = new OptionParser();
JoptOptionsBuilder builder = new JoptOptionsBuilder(parser, bootLogger);

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/nhl/bootique/run/DefaultRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
import java.util.Set;

import com.google.inject.Inject;
import com.nhl.bootique.cli.Options;
import com.nhl.bootique.cli.CommandLine;
import com.nhl.bootique.command.Command;
import com.nhl.bootique.command.CommandOutcome;
import com.nhl.bootique.command.DefaultCommand;

public class DefaultRunner implements Runner {

private Options options;
private CommandLine options;
private Collection<Command> commands;
private Command defaultCommand;

@Inject
public DefaultRunner(Options options, Set<Command> commands, @DefaultCommand Command defaultCommand) {
public DefaultRunner(CommandLine options, Set<Command> commands, @DefaultCommand Command defaultCommand) {
this.options = options;
this.commands = commands;
this.defaultCommand = defaultCommand;
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/nhl/bootique/jopt/JoptOptionsIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ public void testNonOptionArgs_Mix() {
optionsBuilder.add("other", null).mayTakeArgument(null);
optionsBuilder.add("yes", null);

assertEquals(createOptions("a --me=v1 --other v2 b --me v4 --yes c d").nonOptionArgs(), "a", "b", "c", "d");
assertEquals(createOptions("a --me=v1 --other v2 b --me v4 --yes c d").standaloneArguments(), "a", "b", "c", "d");
}

@Test
public void testNonOptionArgs_DashDash() {
optionsBuilder.add("me", null).mayTakeArgument(null);
optionsBuilder.add("other", null).mayTakeArgument(null);

assertEquals(createOptions("a --me=v1 -- --other v2").nonOptionArgs(), "a", "--other", "v2");
assertEquals(createOptions("a --me=v1 -- --other v2").standaloneArguments(), "a", "--other", "v2");
}

private void assertEquals(Collection<String> result, String... expected) {
Expand Down

0 comments on commit 63654af

Please sign in to comment.