From b9fdd697522afd032feb60c94dfef9b9f065752c Mon Sep 17 00:00:00 2001 From: Loic Rouchon Date: Tue, 16 Mar 2021 19:37:23 +0100 Subject: [PATCH] #1346 Add pathCompletionTypes attribute to CommandLine --- src/main/java/picocli/CommandLine.java | 42 ++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/main/java/picocli/CommandLine.java b/src/main/java/picocli/CommandLine.java index 93a691d11..291271174 100644 --- a/src/main/java/picocli/CommandLine.java +++ b/src/main/java/picocli/CommandLine.java @@ -150,6 +150,7 @@ public class CommandLine { private final Tracer tracer = new Tracer(); private CommandSpec commandSpec; private final Interpreter interpreter; + private final Set pathCompletionTypes = new HashSet(); private final IFactory factory; private Object executionResult; @@ -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. */ @@ -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; } @@ -3277,6 +3286,39 @@ public CommandLine registerConverter(Class cls, ITypeConverter convert return this; } + /** + *

Adds the type {@code type} to the list of supported type for path completion.

+ *

Built-in supported types being: + *

    + *
  • {@link java.io.File}
  • + *
  • {@link java.nio.file.Path}
  • + *
+ *

+ * 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 CommandLine registerForPathCompletion(Class 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() */