Skip to content

Commit

Permalink
Add support for a command factory
Browse files Browse the repository at this point in the history
This allows programs using Airline to have systems like Guice
or Dagger instantiate commands for use with Airline.
  • Loading branch information
Jason van Zyl authored and electrum committed Sep 2, 2015
1 parent 78959f8 commit a02e9d2
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 5 deletions.
21 changes: 20 additions & 1 deletion src/main/java/io/airlift/airline/Cli.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ public C parse(String... args)
}

public C parse(Iterable<String> args)
{
return parse(new DefaultCommandFactory<C>(), args);
}

public C parse(CommandFactory<C> commandFactory, String... args)
{
return parse(commandFactory, ImmutableList.copyOf(args));
}

public C parse(CommandFactory<C> commandFactory, Iterable<String> args)
{
Preconditions.checkNotNull(args, "args is null");

Expand All @@ -124,7 +134,8 @@ public C parse(Iterable<String> args)
command.getArguments(),
state.getParsedArguments(),
command.getMetadataInjections(),
ImmutableMap.<Class<?>, Object>of(GlobalMetadata.class, metadata));
ImmutableMap.<Class<?>, Object>of(GlobalMetadata.class, metadata),
commandFactory);
}

private void validate(ParseState state)
Expand Down Expand Up @@ -173,6 +184,7 @@ public static class CliBuilder<C>
private Class<? extends C> defaultCommand;
private final List<Class<? extends C>> defaultCommandGroupCommands = newArrayList();
protected final Map<String, GroupBuilder<C>> groups = newHashMap();
protected CommandFactory<C> commandFactory;

public CliBuilder(String name)
{
Expand All @@ -189,6 +201,13 @@ public CliBuilder<C> withDescription(String description)
return this;
}

public CliBuilder<C> withCommandFactory(CommandFactory<C> commandFactory)
{
Preconditions.checkNotNull(commandFactory, "commandFactory is null");
this.commandFactory = commandFactory;
return this;
}

// public CliBuilder<C> withTypeConverter(TypeConverter typeConverter)
// {
// Preconditions.checkNotNull(typeConverter, "typeConverter is null");
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/io/airlift/airline/CommandFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.airlift.airline;

public interface CommandFactory<T>
{
T createInstance(Class<?> type);
}
12 changes: 12 additions & 0 deletions src/main/java/io/airlift/airline/DefaultCommandFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.airlift.airline;

public class DefaultCommandFactory<T>
implements CommandFactory<T>
{
@SuppressWarnings("unchecked")
@Override
public T createInstance(Class<?> type)
{
return (T) ParserUtil.createInstance(type);
}
}
5 changes: 3 additions & 2 deletions src/main/java/io/airlift/airline/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ public static <T> T createInstance(Class<?> type,
ArgumentsMetadata arguments,
Iterable<Object> parsedArguments,
Iterable<Accessor> metadataInjection,
Map<Class<?>, Object> bindings)
Map<Class<?>, Object> bindings,
CommandFactory<T> commandFactory)
{
// create the command instance
T commandInstance = (T) ParserUtil.createInstance(type);
T commandInstance = commandFactory.createInstance(type);

// inject options
for (OptionMetadata option : options) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/io/airlift/airline/SingleCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ public C parse(Iterable<String> args)
command.getArguments(),
state.getParsedArguments(),
command.getMetadataInjections(),
ImmutableMap.<Class<?>, Object>of(CommandMetadata.class, commandMetadata));
ImmutableMap.<Class<?>, Object>of(CommandMetadata.class, commandMetadata),
new DefaultCommandFactory<C>());
}

private void validate(ParseState state)
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/io/airlift/airline/SuggestCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ public Iterable<String> generateSuggestions()
null,
null,
suggesterMetadata.getMetadataInjections(),
bindings.build());
bindings.build(),
new DefaultCommandFactory<Suggester>());

return suggester.suggest();
}
Expand Down

0 comments on commit a02e9d2

Please sign in to comment.