Skip to content

Commit

Permalink
More work on positional argument support (#91)
Browse files Browse the repository at this point in the history
- Capture positional arguments during parsing
- Add PositionalArgumentMetadata variant to ArgumentsRestriction methods
- Modify ParserUtils to also provide injection of positional arguments
- Clean up some deprecation warnings
  • Loading branch information
rvesse committed Apr 15, 2019
1 parent 4a1a0a0 commit 6fffa78
Show file tree
Hide file tree
Showing 16 changed files with 279 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import com.github.rvesse.airline.model.ArgumentMetadata;
import com.github.rvesse.airline.model.PositionalArgumentMetadata;
import com.github.rvesse.airline.types.DefaultTypeConverterProvider;
import com.github.rvesse.airline.types.TypeConverterProvider;

Expand Down Expand Up @@ -65,7 +65,7 @@
* the argument definitions are compatible.
* <p>
* See
* {@link ArgumentMetadata#override(ArgumentMetadata, ArgumentMetadata)}
* {@link PositionalArgumentMetadata#override(PositionalArgumentMetadata, PositionalArgumentMetadata)}
* for legal overrides
* </p>
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.github.rvesse.airline.model.GlobalMetadata;
import com.github.rvesse.airline.model.MetadataLoader;
import com.github.rvesse.airline.model.OptionMetadata;
import com.github.rvesse.airline.model.PositionalArgumentMetadata;
import com.github.rvesse.airline.model.SuggesterMetadata;
import com.github.rvesse.airline.parser.ParseState;
import com.github.rvesse.airline.parser.suggester.SuggestionParser;
Expand Down Expand Up @@ -79,7 +80,8 @@ public Iterable<String> generateSuggestions() {
}

Suggester suggester = createInstance(suggesterMetadata.getSuggesterClass(),
Collections.<OptionMetadata> emptyList(), null, null, null,
Collections.<OptionMetadata> emptyList(), null,
Collections.<PositionalArgumentMetadata> emptyList(), null, null, null,
suggesterMetadata.getMetadataInjections(), AirlineUtils.unmodifiableMapCopy(bindings));

return suggester.suggest();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class CommandMetadata {
private final List<OptionMetadata> groupOptions;
private final List<OptionMetadata> commandOptions;
private final OptionMetadata defaultOption;
private final List<ArgumentMetadata> positionalArgs;
private final List<PositionalArgumentMetadata> positionalArgs;
private final ArgumentsMetadata arguments;
private final List<Accessor> metadataInjections;
private final Class<?> type;
Expand All @@ -50,7 +50,7 @@ public CommandMetadata(String name,
Iterable<OptionMetadata> groupOptions,
Iterable<OptionMetadata> commandOptions,
OptionMetadata defaultOption,
List<ArgumentMetadata> positionalArguments,
List<PositionalArgumentMetadata> positionalArguments,
ArgumentsMetadata arguments,
Iterable<Accessor> metadataInjections,
Class<?> type,
Expand Down Expand Up @@ -151,7 +151,7 @@ public OptionMetadata getDefaultOption() {
return defaultOption;
}

public List<ArgumentMetadata> getPositionalArguments() {
public List<PositionalArgumentMetadata> getPositionalArguments() {
return positionalArgs;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@

import javax.inject.Inject;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.IterableUtils;
import org.apache.commons.collections4.IteratorUtils;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.ArrayUtils;
Expand Down Expand Up @@ -254,7 +254,7 @@ public static <C> GlobalMetadata<C> loadGlobal(Class<?> cliClass, ParserMetadata
}

// Maybe a top level group we've already seen
CommandGroupMetadata group = CollectionUtils.find(groups, new GroupFinder(groupName));
CommandGroupMetadata group = IterableUtils.find(groups, new GroupFinder(groupName));
if (group == null) {
// Maybe a sub-group we've already seen
group = subGroups.get(subGroupPath);
Expand Down Expand Up @@ -750,7 +750,7 @@ public static void loadInjectionMetadata(Class<?> type, InjectionMetadata inject
List<ArgumentsRestriction> restrictions = collectArgumentRestrictions(field, false);

//@formatter:off
injectionMetadata.positionalArgs.add(new ArgumentMetadata(positionalArgumentAnnotation.position(),
injectionMetadata.positionalArgs.add(new PositionalArgumentMetadata(positionalArgumentAnnotation.position(),
title,
positionalArgumentAnnotation.description(),
positionalArgumentAnnotation.sealed(),
Expand Down Expand Up @@ -940,12 +940,12 @@ private static void tryOverrideOptions(Map<Set<String>, OptionMetadata> optionIn
optionIndex.put(names, merged);
}

private static List<ArgumentMetadata> overridePositionalArgumentSet(List<ArgumentMetadata> args) {
private static List<PositionalArgumentMetadata> overridePositionalArgumentSet(List<PositionalArgumentMetadata> args) {
args = ListUtils.unmodifiableList(args);

Map<Integer, ArgumentMetadata> argsIndex = new HashMap<>();
Map<Integer, PositionalArgumentMetadata> argsIndex = new HashMap<>();
int maxIndex = -1;
for (ArgumentMetadata arg : args) {
for (PositionalArgumentMetadata arg : args) {
maxIndex = Math.max(maxIndex, arg.getZeroBasedPosition());

if (argsIndex.containsKey(arg.getZeroBasedPosition())) {
Expand All @@ -956,7 +956,7 @@ private static List<ArgumentMetadata> overridePositionalArgumentSet(List<Argumen
}
}

List<ArgumentMetadata> posArgs = new ArrayList<>(maxIndex);
List<PositionalArgumentMetadata> posArgs = new ArrayList<>(maxIndex);
for (int i = 0; i < maxIndex; i++) {
posArgs.set(i, argsIndex.get(i));
if (posArgs.get(i) == null) {
Expand All @@ -969,13 +969,13 @@ private static List<ArgumentMetadata> overridePositionalArgumentSet(List<Argumen
return ListUtils.unmodifiableList(posArgs);
}

private static void tryOverridePositionalArgument(Map<Integer, ArgumentMetadata> argsIndex,
ArgumentMetadata parent) {
private static void tryOverridePositionalArgument(Map<Integer, PositionalArgumentMetadata> argsIndex,
PositionalArgumentMetadata parent) {

// As the metadata is extracted from the deepest class in the hierarchy
// going upwards we need to treat the passed option as the parent and
// the pre-existing option definition as the child
ArgumentMetadata child = argsIndex.get(parent.getZeroBasedPosition());
PositionalArgumentMetadata child = argsIndex.get(parent.getZeroBasedPosition());

Accessor parentField = parent.getAccessors().iterator().next();
Accessor childField = child.getAccessors().iterator().next();
Expand All @@ -999,7 +999,7 @@ private static void tryOverridePositionalArgument(Map<Integer, ArgumentMetadata>
parentField, childField, parent.getZeroBasedPosition(), parent.getTitle()));

// Attempt overriding, this will error if the overriding is not possible
ArgumentMetadata merged = ArgumentMetadata.override(parent, child);
PositionalArgumentMetadata merged = PositionalArgumentMetadata.override(parent, child);
argsIndex.put(parent.getZeroBasedPosition(), merged);
}

Expand All @@ -1017,7 +1017,7 @@ public static void loadCommandsIntoGroupsByAnnotation(List<CommandMetadata> allC
// now add the command to any groupNames specified in the Command
// annotation
for (String groupName : command.getGroupNames()) {
CommandGroupMetadata group = CollectionUtils.find(commandGroups, new GroupFinder(groupName));
CommandGroupMetadata group = IterableUtils.find(commandGroups, new GroupFinder(groupName));
if (group != null) {
// Add to existing top level group
group.addCommand(command);
Expand All @@ -1030,7 +1030,7 @@ public static void loadCommandsIntoGroupsByAnnotation(List<CommandMetadata> allC
for (int i = 0; i < groups.length; i++) {
if (i == 0) {
// Find/create the necessary top level group
subGroup = CollectionUtils.find(commandGroups, new GroupFinder(groups[i]));
subGroup = IterableUtils.find(commandGroups, new GroupFinder(groups[i]));
if (subGroup == null) {
subGroup = new CommandGroupMetadata(groups[i], "", false,
Collections.<OptionMetadata> emptyList(),
Expand All @@ -1040,7 +1040,7 @@ Collections.<CommandGroupMetadata> emptyList(), null,
}
} else {
// Find/create the next sub-group
CommandGroupMetadata nextSubGroup = CollectionUtils.find(subGroup.getSubGroups(),
CommandGroupMetadata nextSubGroup = IterableUtils.find(subGroup.getSubGroups(),
new GroupFinder(groups[i]));
if (nextSubGroup == null) {
nextSubGroup = new CommandGroupMetadata(groups[i], "", false,
Expand Down Expand Up @@ -1096,7 +1096,7 @@ private static void createGroupsFromAnnotations(List<CommandMetadata> allCommand
// load default command if needed
if (!groupAnno.defaultCommand().equals(Group.NO_DEFAULT.class)) {
defaultCommandClass = groupAnno.defaultCommand();
defaultCommand = CollectionUtils.find(allCommands, new CommandTypeFinder(defaultCommandClass));
defaultCommand = IterableUtils.find(allCommands, new CommandTypeFinder(defaultCommandClass));
if (null == defaultCommand) {
defaultCommand = loadCommand(defaultCommandClass, baseHelpSections);
newCommands.add(defaultCommand);
Expand All @@ -1107,7 +1107,7 @@ private static void createGroupsFromAnnotations(List<CommandMetadata> allCommand
List<CommandMetadata> groupCommands = new ArrayList<CommandMetadata>(groupAnno.commands().length);
CommandMetadata groupCommand = null;
for (Class commandClass : groupAnno.commands()) {
groupCommand = CollectionUtils.find(allCommands, new CommandTypeFinder(commandClass));
groupCommand = IterableUtils.find(allCommands, new CommandTypeFinder(commandClass));
if (null == groupCommand) {
groupCommand = loadCommand(commandClass, baseHelpSections);
newCommands.add(groupCommand);
Expand All @@ -1117,7 +1117,7 @@ private static void createGroupsFromAnnotations(List<CommandMetadata> allCommand

// Find the group metadata
// May already exist as a top level group
CommandGroupMetadata groupMetadata = CollectionUtils.find(commandGroups,
CommandGroupMetadata groupMetadata = IterableUtils.find(commandGroups,
new GroupFinder(groupAnno.name()));
if (groupMetadata == null) {
// Not a top level group
Expand Down Expand Up @@ -1167,7 +1167,7 @@ protected static void buildGroupsHierarchy(List<CommandGroupMetadata> commandGro
for (int i = 0; i < groups.length - 1; i++) {
if (i == 0) {
// Should be a top level group
parentGroup = CollectionUtils.find(commandGroups, new GroupFinder(groups[i]));
parentGroup = IterableUtils.find(commandGroups, new GroupFinder(groups[i]));
if (parentGroup == null) {
// Top level parent group does not exist so create empty
// top level group
Expand All @@ -1179,7 +1179,7 @@ Collections.<CommandGroupMetadata> emptyList(), null,
}
} else {
// Should be a sub-group of the current parent
CommandGroupMetadata nextParent = CollectionUtils.find(parentGroup.getSubGroups(),
CommandGroupMetadata nextParent = IterableUtils.find(parentGroup.getSubGroups(),
new GroupFinder(groups[i]));
if (nextParent == null) {
// Next parent group does not exist so create empty
Expand All @@ -1206,7 +1206,7 @@ private static class InjectionMetadata {
private List<OptionMetadata> groupOptions = new ArrayList<>();
private List<OptionMetadata> commandOptions = new ArrayList<>();
private OptionMetadata defaultOption = null;
private List<ArgumentMetadata> positionalArgs = new ArrayList<>();
private List<PositionalArgumentMetadata> positionalArgs = new ArrayList<>();
private List<ArgumentsMetadata> arguments = new ArrayList<>();
private List<Accessor> metadataInjections = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import org.apache.commons.collections4.SetUtils;
import org.apache.commons.lang3.StringUtils;

public class ArgumentMetadata {
public class PositionalArgumentMetadata {
private final int position;
private final String title, description;
private final boolean sealed, overrides;
Expand All @@ -42,7 +42,7 @@ public class ArgumentMetadata {
private final TypeConverterProvider provider;

/**
* Creates new argument metadata
* Creates new positional argument metadata
*
* @param position
* Zero based position index
Expand All @@ -58,7 +58,7 @@ public class ArgumentMetadata {
* Field to modify
*/
//@formatter:off
public ArgumentMetadata(int position, String title,
public PositionalArgumentMetadata(int position, String title,
String description,
boolean sealed, boolean overrides,
Iterable<ArgumentsRestriction> restrictions,
Expand All @@ -85,13 +85,13 @@ public ArgumentMetadata(int position, String title,
this.accessors = SetUtils.unmodifiableSet(Collections.singleton(new Accessor(path)));
}

public ArgumentMetadata(Iterable<ArgumentMetadata> arguments) {
public PositionalArgumentMetadata(Iterable<PositionalArgumentMetadata> arguments) {
if (arguments == null)
throw new NullPointerException("arguments cannot be null");
if (!arguments.iterator().hasNext())
throw new IllegalArgumentException("arguments cannot be empty");

ArgumentMetadata first = arguments.iterator().next();
PositionalArgumentMetadata first = arguments.iterator().next();

this.sealed = first.sealed;
this.overrides = first.overrides;
Expand All @@ -102,7 +102,7 @@ public ArgumentMetadata(Iterable<ArgumentMetadata> arguments) {
this.provider = first.provider;

Set<Accessor> accessors = new HashSet<>();
for (ArgumentMetadata other : arguments) {
for (PositionalArgumentMetadata other : arguments) {
if (!first.equals(other))
throw new IllegalArgumentException(
String.format("Conflicting arguments definitions: %s, %s", first, other));
Expand Down Expand Up @@ -179,7 +179,7 @@ public boolean equals(Object o) {
return false;
}

ArgumentMetadata that = (ArgumentMetadata) o;
PositionalArgumentMetadata that = (PositionalArgumentMetadata) o;

if (this.position != that.position)
return false;
Expand Down Expand Up @@ -232,7 +232,7 @@ public String toString() {
* Child
* @return Merged metadata
*/
public static ArgumentMetadata override(ArgumentMetadata parent, ArgumentMetadata child) {
public static PositionalArgumentMetadata override(PositionalArgumentMetadata parent, PositionalArgumentMetadata child) {
// Cannot change position
if (parent.position != child.position)
throw new IllegalArgumentException(
Expand Down Expand Up @@ -277,9 +277,9 @@ public static ArgumentMetadata override(ArgumentMetadata parent, ArgumentMetadat
throw new IllegalArgumentException(
String.format("Cannot override positional argument %d (%s) unless child argument sets overrides to true", parent.position, parent.title));

ArgumentMetadata merged;
PositionalArgumentMetadata merged;
//@formatter:off
merged = new ArgumentMetadata(child.position,
merged = new PositionalArgumentMetadata(child.position,
child.title,
child.description,
child.sealed,
Expand Down
Loading

0 comments on commit 6fffa78

Please sign in to comment.