Skip to content

Commit

Permalink
remkop#1346 Add pathCompletionTypes attribute to CommandLine
Browse files Browse the repository at this point in the history
  • Loading branch information
loicrouchon committed Mar 16, 2021
1 parent 7438ef4 commit b9fdd69
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/main/java/picocli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ public class CommandLine {
private final Tracer tracer = new Tracer();
private CommandSpec commandSpec;
private final Interpreter interpreter;
private final Set<String> pathCompletionTypes = new HashSet<String>();
private final IFactory factory;

private Object executionResult;
Expand Down Expand Up @@ -229,6 +230,12 @@ private CommandLine(Object command, IFactory factory, boolean userCalled) {
if (userCalled) { this.applyModelTransformations(); }
commandSpec.validate();
if (commandSpec.unmatchedArgsBindings().size() > 0) { setUnmatchedArgumentsAllowed(true); }
registerDefaultPathCompletionTypes();
}

private void registerDefaultPathCompletionTypes() {
pathCompletionTypes.add("java.io.File");
pathCompletionTypes.add("java.nio.file.Path");
}

/** Apply transformers to command spec recursively. */
Expand All @@ -253,6 +260,8 @@ private CommandLine copy() {

result.interpreter.converterRegistry.clear();
result.interpreter.converterRegistry.putAll(interpreter.converterRegistry);
result.pathCompletionTypes.clear();
result.pathCompletionTypes.addAll(pathCompletionTypes);
return result;
}

Expand Down Expand Up @@ -3277,6 +3286,39 @@ public <K> CommandLine registerConverter(Class<K> cls, ITypeConverter<K> convert
return this;
}

/**
* <p>Adds the type {@code type} to the list of supported type for path completion.</p>
* <p>Built-in supported types being:
* <ul>
* <li>{@link java.io.File}</li>
* <li>{@link java.nio.file.Path}</li>
* </ul>
* </p>
* type {@code type}.
* @param type the type to check if path completion is supported for
* @return this CommandLine object, to allow method chaining
* @see #supportsPathCompletion(Class)
*/
public <K> CommandLine registerForPathCompletion(Class<K> cls) {
pathCompletionTypes.add(cls.getName());
for (CommandLine command : getCommandSpec().commands.values()) {
command.registerForPathCompletion(cls);
}
return this;
}

/**
* Returns {@code true} if the CommandLine supports path completion for {@link Option} and {@link Parameters} of
* type {@code type}.
* @param type the type to check if path completion is supported for
* @return {@code true} if the CommandLine supports path completion for {@link Option} and {@link Parameters} of
* type {@code type}.
* @see #registerForPathCompletion(Class)
*/
public boolean supportsPathCompletion(Class<?> type) {
return pathCompletionTypes.contains(type.getName());
}

/** Returns the String that separates option names from option values when parsing command line options.
* @return the String the parser uses to separate option names from option values
* @see ParserSpec#separator() */
Expand Down

0 comments on commit b9fdd69

Please sign in to comment.