public IEnumerable <string> Suggest(IShellState shellState, HttpState programState, ICoreParseResult parseResult) { if (parseResult.Sections.Count == 0) { return(new[] { Name }); } if (parseResult.Sections.Count > 0 && parseResult.SelectedSection == 0 && Name.StartsWith(parseResult.Sections[0].Substring(0, parseResult.CaretPositionWithinSelectedSection), StringComparison.OrdinalIgnoreCase)) { return(new[] { Name }); } if (string.Equals(Name, parseResult.Sections[0], StringComparison.OrdinalIgnoreCase) && parseResult.SelectedSection == 1 && (parseResult.Sections.Count < 2 || SubCommand.StartsWith(parseResult.Sections[1].Substring(0, parseResult.CaretPositionWithinSelectedSection), StringComparison.OrdinalIgnoreCase))) { return(new[] { SubCommand }); } if (parseResult.Sections.Count > 2 && string.Equals(Name, parseResult.Sections[0], StringComparison.OrdinalIgnoreCase) && string.Equals(SubCommand, parseResult.Sections[1], StringComparison.OrdinalIgnoreCase) && parseResult.SelectedSection == 2) { string prefix = parseResult.Sections[2].Substring(0, parseResult.CaretPositionWithinSelectedSection); return(HeaderCompletion.GetCompletions(null, prefix)); } if (parseResult.Sections.Count > 3 && string.Equals(Name, parseResult.Sections[0], StringComparison.OrdinalIgnoreCase) && string.Equals(SubCommand, parseResult.Sections[1], StringComparison.OrdinalIgnoreCase) && parseResult.SelectedSection == 3) { string prefix = parseResult.Sections[3].Substring(0, parseResult.CaretPositionWithinSelectedSection); return(HeaderCompletion.GetValueCompletions(null, string.Empty, parseResult.Sections[2], prefix, programState)); } return(null); }
public void GetCompletions_NullExistingHeaders_ProperResults() { IEnumerable <string> result = HeaderCompletion.GetCompletions(null, "U"); Assert.Equal(3, result.Count()); Assert.Contains("Upgrade", result, StringComparer.OrdinalIgnoreCase); Assert.Contains("Upgrade-Insecure-Requests", result, StringComparer.OrdinalIgnoreCase); Assert.Contains("User-Agent", result, StringComparer.OrdinalIgnoreCase); }
public void GetCompletions_ExistingHeader_ProperResults() { IReadOnlyCollection <string> existingHeaders = new Collection <string>() { "User-Agent" }; IEnumerable <string> result = HeaderCompletion.GetCompletions(existingHeaders, "U"); Assert.Equal(2, result.Count()); Assert.Contains("Upgrade", result, StringComparer.OrdinalIgnoreCase); Assert.Contains("Upgrade-Insecure-Requests", result, StringComparer.OrdinalIgnoreCase); }
protected override IEnumerable <string> GetOptionValueCompletions(IShellState shellState, HttpState programState, string optionId, DefaultCommandInput <ICoreParseResult> commandInput, ICoreParseResult parseResult, string normalizedCompletionText) { if (string.Equals(optionId, BodyFileOption, StringComparison.Ordinal) || string.Equals(optionId, ResponseFileOption, StringComparison.OrdinalIgnoreCase) || string.Equals(optionId, ResponseBodyFileOption, StringComparison.OrdinalIgnoreCase) || string.Equals(optionId, ResponseHeadersFileOption, StringComparison.OrdinalIgnoreCase)) { return(FileSystemCompletion.GetCompletions(normalizedCompletionText)); } if (string.Equals(optionId, HeaderOption, StringComparison.Ordinal)) { HashSet <string> alreadySpecifiedHeaders = new HashSet <string>(StringComparer.Ordinal); IReadOnlyList <InputElement> options = commandInput.Options[HeaderOption]; for (int i = 0; i < options.Count; ++i) { if (options[i] == commandInput.SelectedElement) { continue; } string elementText = options[i].Text; string existingHeaderName = elementText.Split(HeaderSeparatorChars)[0]; alreadySpecifiedHeaders.Add(existingHeaderName); } //Check to see if the selected element is in a header name or value int equalsIndex = normalizedCompletionText.IndexOfAny(HeaderSeparatorChars); string path = commandInput.Arguments.Count > 0 ? commandInput.Arguments[0].Text : string.Empty; if (equalsIndex < 0) { IEnumerable <string> headerNameOptions = HeaderCompletion.GetCompletions(alreadySpecifiedHeaders, normalizedCompletionText); List <string> allSuggestions = new List <string>(); foreach (string suggestion in headerNameOptions.Select(x => x)) { allSuggestions.Add(suggestion + ":"); IEnumerable <string> suggestions = HeaderCompletion.GetValueCompletions(Verb, path, suggestion, string.Empty, programState); if (suggestions != null) { foreach (string valueSuggestion in suggestions) { allSuggestions.Add(suggestion + ":" + valueSuggestion); } } } return(allSuggestions); } else { //Didn't exit from the header name check, so must be a value string headerName = normalizedCompletionText.Substring(0, equalsIndex); IEnumerable <string> suggestions = HeaderCompletion.GetValueCompletions(Verb, path, headerName, normalizedCompletionText.Substring(equalsIndex + 1), programState); if (suggestions == null) { return(null); } return(suggestions.Select(x => normalizedCompletionText.Substring(0, equalsIndex + 1) + x)); } } return(null); }
public void GetCompletions_NoResults_ReturnsEmpty() { IEnumerable <string> result = HeaderCompletion.GetCompletions(null, "Not-A-Real-Header"); Assert.Empty(result); }