diff --git a/src/System.CommandLine/Command.cs b/src/System.CommandLine/Command.cs index be565651d2..e1756cd10f 100644 --- a/src/System.CommandLine/Command.cs +++ b/src/System.CommandLine/Command.cs @@ -265,6 +265,13 @@ public override IEnumerable GetCompletions(CompletionContext con if (context.WordToComplete is { } textToMatch) { + // If the text to match is the command name, we should suggest all possible completions (subcommands, options, arguments). + var textToMatchIsCommandName = context.ParseResult.CommandResult.Command.Name.Equals(textToMatch, StringComparison.OrdinalIgnoreCase); + if (textToMatchIsCommandName) + { + textToMatch = null; + } + if (HasSubcommands) { var commands = Subcommands; @@ -291,7 +298,7 @@ public override IEnumerable GetCompletions(CompletionContext con var argument = arguments[i]; foreach (var completion in argument.GetCompletions(context)) { - if (completion.Label.ContainsCaseInsensitive(textToMatch)) + if (textToMatch is null || completion.Label.ContainsCaseInsensitive(textToMatch)) { completions.Add(completion); } @@ -335,7 +342,7 @@ void AddCompletionsFor(Symbol identifier, AliasSet? aliases) { if (!identifier.Hidden) { - if (identifier.Name.ContainsCaseInsensitive(textToMatch)) + if (textToMatch is null || identifier.Name.ContainsCaseInsensitive(textToMatch)) { completions.Add(new CompletionItem(identifier.Name, CompletionItem.KindKeyword, detail: identifier.Description)); } @@ -344,7 +351,7 @@ void AddCompletionsFor(Symbol identifier, AliasSet? aliases) { foreach (string alias in aliases) { - if (alias.ContainsCaseInsensitive(textToMatch)) + if (textToMatch is null || alias.ContainsCaseInsensitive(textToMatch)) { completions.Add(new CompletionItem(alias, CompletionItem.KindKeyword, detail: identifier.Description)); } diff --git a/src/System.CommandLine/Completions/CompletionAction.cs b/src/System.CommandLine/Completions/CompletionAction.cs index 44cee60f58..44858cd975 100644 --- a/src/System.CommandLine/Completions/CompletionAction.cs +++ b/src/System.CommandLine/Completions/CompletionAction.cs @@ -24,7 +24,7 @@ public override int Invoke(ParseResult parseResult) int position = !string.IsNullOrEmpty(parsedValues) ? int.Parse(parsedValues) : rawInput?.Length ?? 0; - var commandLineToComplete = parseResult.Tokens.LastOrDefault(t => t.Type != TokenType.Directive)?.Value ?? ""; + var commandLineToComplete = parseResult.Tokens.Where(t => t.Type != TokenType.Directive).Select(t => t.Value).ToArray(); var completionParseResult = parseResult.RootCommandResult.Command.Parse(commandLineToComplete, parseResult.Configuration);