From a02e9d29ff6f9a897df5303d65a57f7cbf256540 Mon Sep 17 00:00:00 2001 From: Jason van Zyl Date: Wed, 19 Sep 2012 15:29:43 -0700 Subject: [PATCH] Add support for a command factory This allows programs using Airline to have systems like Guice or Dagger instantiate commands for use with Airline. --- src/main/java/io/airlift/airline/Cli.java | 21 ++++++++++++++++++- .../io/airlift/airline/CommandFactory.java | 6 ++++++ .../airline/DefaultCommandFactory.java | 12 +++++++++++ .../java/io/airlift/airline/ParserUtil.java | 5 +++-- .../io/airlift/airline/SingleCommand.java | 3 ++- .../io/airlift/airline/SuggestCommand.java | 3 ++- 6 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 src/main/java/io/airlift/airline/CommandFactory.java create mode 100644 src/main/java/io/airlift/airline/DefaultCommandFactory.java diff --git a/src/main/java/io/airlift/airline/Cli.java b/src/main/java/io/airlift/airline/Cli.java index a5c96dd55..2c4af5385 100644 --- a/src/main/java/io/airlift/airline/Cli.java +++ b/src/main/java/io/airlift/airline/Cli.java @@ -99,6 +99,16 @@ public C parse(String... args) } public C parse(Iterable args) + { + return parse(new DefaultCommandFactory(), args); + } + + public C parse(CommandFactory commandFactory, String... args) + { + return parse(commandFactory, ImmutableList.copyOf(args)); + } + + public C parse(CommandFactory commandFactory, Iterable args) { Preconditions.checkNotNull(args, "args is null"); @@ -124,7 +134,8 @@ public C parse(Iterable args) command.getArguments(), state.getParsedArguments(), command.getMetadataInjections(), - ImmutableMap., Object>of(GlobalMetadata.class, metadata)); + ImmutableMap., Object>of(GlobalMetadata.class, metadata), + commandFactory); } private void validate(ParseState state) @@ -173,6 +184,7 @@ public static class CliBuilder private Class defaultCommand; private final List> defaultCommandGroupCommands = newArrayList(); protected final Map> groups = newHashMap(); + protected CommandFactory commandFactory; public CliBuilder(String name) { @@ -189,6 +201,13 @@ public CliBuilder withDescription(String description) return this; } + public CliBuilder withCommandFactory(CommandFactory commandFactory) + { + Preconditions.checkNotNull(commandFactory, "commandFactory is null"); + this.commandFactory = commandFactory; + return this; + } + // public CliBuilder withTypeConverter(TypeConverter typeConverter) // { // Preconditions.checkNotNull(typeConverter, "typeConverter is null"); diff --git a/src/main/java/io/airlift/airline/CommandFactory.java b/src/main/java/io/airlift/airline/CommandFactory.java new file mode 100644 index 000000000..e84632e22 --- /dev/null +++ b/src/main/java/io/airlift/airline/CommandFactory.java @@ -0,0 +1,6 @@ +package io.airlift.airline; + +public interface CommandFactory +{ + T createInstance(Class type); +} diff --git a/src/main/java/io/airlift/airline/DefaultCommandFactory.java b/src/main/java/io/airlift/airline/DefaultCommandFactory.java new file mode 100644 index 000000000..c49f99a34 --- /dev/null +++ b/src/main/java/io/airlift/airline/DefaultCommandFactory.java @@ -0,0 +1,12 @@ +package io.airlift.airline; + +public class DefaultCommandFactory + implements CommandFactory +{ + @SuppressWarnings("unchecked") + @Override + public T createInstance(Class type) + { + return (T) ParserUtil.createInstance(type); + } +} diff --git a/src/main/java/io/airlift/airline/ParserUtil.java b/src/main/java/io/airlift/airline/ParserUtil.java index ac6f6d33a..46ede92a3 100644 --- a/src/main/java/io/airlift/airline/ParserUtil.java +++ b/src/main/java/io/airlift/airline/ParserUtil.java @@ -31,10 +31,11 @@ public static T createInstance(Class type, ArgumentsMetadata arguments, Iterable parsedArguments, Iterable metadataInjection, - Map, Object> bindings) + Map, Object> bindings, + CommandFactory commandFactory) { // create the command instance - T commandInstance = (T) ParserUtil.createInstance(type); + T commandInstance = commandFactory.createInstance(type); // inject options for (OptionMetadata option : options) { diff --git a/src/main/java/io/airlift/airline/SingleCommand.java b/src/main/java/io/airlift/airline/SingleCommand.java index 6c4f84e52..9a9ca4c11 100644 --- a/src/main/java/io/airlift/airline/SingleCommand.java +++ b/src/main/java/io/airlift/airline/SingleCommand.java @@ -72,7 +72,8 @@ public C parse(Iterable args) command.getArguments(), state.getParsedArguments(), command.getMetadataInjections(), - ImmutableMap., Object>of(CommandMetadata.class, commandMetadata)); + ImmutableMap., Object>of(CommandMetadata.class, commandMetadata), + new DefaultCommandFactory()); } private void validate(ParseState state) diff --git a/src/main/java/io/airlift/airline/SuggestCommand.java b/src/main/java/io/airlift/airline/SuggestCommand.java index 59094b428..826cf0164 100644 --- a/src/main/java/io/airlift/airline/SuggestCommand.java +++ b/src/main/java/io/airlift/airline/SuggestCommand.java @@ -64,7 +64,8 @@ public Iterable generateSuggestions() null, null, suggesterMetadata.getMetadataInjections(), - bindings.build()); + bindings.build(), + new DefaultCommandFactory()); return suggester.suggest(); }