Skip to content
Merged
Prev Previous commit
Next Next commit
Move non-generic extensions to be instance methods
  • Loading branch information
jozkee committed Nov 8, 2022
commit 647896058ba2e2d5a1b98a1c2a92143b8052f3a6
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@ System.CommandLine
public System.Boolean HasDefaultValue { get; }
public System.String HelpName { get; set; }
public System.Type ValueType { get; }
public Argument AcceptOnlyFromAmong(System.String[] values)
public Argument AddCompletions(System.String[] values)
public Argument AddCompletions(System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.String>> complete)
public Argument AddCompletions(System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem>> complete)
public System.Void AddValidator(System.Action<System.CommandLine.Parsing.ArgumentResult> validate)
public System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem> GetCompletions(System.CommandLine.Completions.CompletionContext context)
public System.Object GetDefaultValue()
public Argument LegalFileNamesOnly()
public Argument LegalFilePathsOnly()
public ParseResult Parse(System.String commandLine)
public ParseResult Parse(System.String[] args)
public System.Void SetDefaultValue(System.Object value)
public System.Void SetDefaultValueFactory(System.Func<System.Object> defaultValueFactory)
public System.Void SetDefaultValueFactory(System.Func<System.CommandLine.Parsing.ArgumentResult,System.Object> defaultValueFactory)
Expand All @@ -32,19 +40,11 @@ System.CommandLine
public System.Boolean Equals(ArgumentArity other)
public System.Boolean Equals(System.Object obj)
public System.Int32 GetHashCode()
public static class ArgumentExtensions
public static class ArgumentValidationExtensions
public static Argument<System.IO.FileInfo> AcceptExistingOnly(this Argument<System.IO.FileInfo> argument)
public static Argument<System.IO.DirectoryInfo> AcceptExistingOnly(this Argument<System.IO.DirectoryInfo> argument)
public static Argument<System.IO.FileSystemInfo> AcceptExistingOnly(this Argument<System.IO.FileSystemInfo> argument)
public static Argument<T> AcceptExistingOnly<T>(this Argument<T> argument)
public static TArgument AcceptOnlyFromAmong<TArgument>(this TArgument argument, System.String[] values)
public static TArgument AddCompletions<TArgument>(this TArgument argument, System.String[] values)
public static TArgument AddCompletions<TArgument>(this TArgument argument, System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.String>> complete)
public static TArgument AddCompletions<TArgument>(this TArgument argument, System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem>> complete)
public static TArgument LegalFileNamesOnly<TArgument>(this TArgument argument)
public static TArgument LegalFilePathsOnly<TArgument>(this TArgument argument)
public static ParseResult Parse(this Argument argument, System.String commandLine)
public static ParseResult Parse(this Argument argument, System.String[] args)
public class Command : IdentifierSymbol, System.Collections.Generic.IEnumerable<Symbol>, System.Collections.IEnumerable, System.CommandLine.Completions.ICompletionSource
.ctor(System.String name, System.String description = null)
public System.Collections.Generic.IReadOnlyList<Argument> Arguments { get; }
Expand Down Expand Up @@ -194,9 +194,17 @@ System.CommandLine
public ArgumentArity Arity { get; set; }
public System.Boolean IsRequired { get; set; }
public System.Type ValueType { get; }
public Option AcceptOnlyFromAmong(System.String[] values)
public Option AddCompletions(System.String[] values)
public Option AddCompletions(System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.String>> complete)
public Option AddCompletions(System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem>> complete)
public System.Void AddValidator(System.Action<System.CommandLine.Parsing.OptionResult> validate)
public System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem> GetCompletions(System.CommandLine.Completions.CompletionContext context)
public System.Boolean HasAliasIgnoringPrefix(System.String alias)
public Option LegalFileNamesOnly()
public Option LegalFilePathsOnly()
public ParseResult Parse(System.String commandLine)
public ParseResult Parse(System.String[] args)
public System.Void SetDefaultValue(System.Object value)
public System.Void SetDefaultValueFactory(System.Func<System.Object> defaultValueFactory)
public class Option<T> : Option, IValueDescriptor<T>, System.CommandLine.Binding.IValueDescriptor, System.CommandLine.Completions.ICompletionSource
Expand All @@ -207,19 +215,11 @@ System.CommandLine
.ctor(System.String name, Func<T> defaultValueFactory, System.String description = null)
.ctor(System.String[] aliases, Func<T> defaultValueFactory, System.String description = null)
public ArgumentArity Arity { get; set; }
public static class OptionExtensions
public static class OptionValidationExtensions
public static Option<System.IO.FileInfo> AcceptExistingOnly(this Option<System.IO.FileInfo> option)
public static Option<System.IO.DirectoryInfo> AcceptExistingOnly(this Option<System.IO.DirectoryInfo> option)
public static Option<System.IO.FileSystemInfo> AcceptExistingOnly(this Option<System.IO.FileSystemInfo> option)
public static Option<T> AcceptExistingOnly<T>(this Option<T> option)
public static TOption AcceptOnlyFromAmong<TOption>(this TOption option, System.String[] values)
public static TOption AddCompletions<TOption>(this TOption option, System.String[] values)
public static TOption AddCompletions<TOption>(this TOption option, System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.String>> complete)
public static TOption AddCompletions<TOption>(this TOption option, System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem>> complete)
public static TOption LegalFileNamesOnly<TOption>(this TOption option)
public static TOption LegalFilePathsOnly<TOption>(this TOption option)
public static ParseResult Parse(this Option option, System.String commandLine)
public static ParseResult Parse(this Option option, System.String[] args)
public class ParseResult
public System.CommandLine.Parsing.CommandResult CommandResult { get; }
public System.Collections.Generic.IReadOnlyDictionary<System.String,System.Collections.Generic.IReadOnlyList<System.String>> Directives { get; }
Expand Down
12 changes: 9 additions & 3 deletions src/System.CommandLine.Suggest/SuggestionDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,15 @@ public SuggestionDispatcher(ISuggestionRegistration suggestionRegistration, ISug

private Command GetCommand { get; }

private Option<FileInfo> ExecutableOption { get; } =
new Option<FileInfo>(new[] { "-e", "--executable" }, "The executable to call for suggestions")
.LegalFilePathsOnly();
private Option<FileInfo> ExecutableOption { get; } = GetExecutableOption();

private static Option<FileInfo> GetExecutableOption()
{
var option = new Option<FileInfo>(new[] { "-e", "--executable" }, "The executable to call for suggestions");
option.LegalFilePathsOnly();

return option;
}

private Command ListCommand { get; }

Expand Down
120 changes: 120 additions & 0 deletions src/System.CommandLine/Argument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.CommandLine.Parsing;
using System.CommandLine.Completions;
using System.Linq;
using System.IO;

namespace System.CommandLine
{
Expand Down Expand Up @@ -202,5 +203,124 @@ public override IEnumerable<CompletionItem> GetCompletions(CompletionContext con

/// <inheritdoc />
string IValueDescriptor.ValueName => Name;

/// <summary>
/// Adds completions for the argument.
/// </summary>
/// <param name="values">The completions to add.</param>
/// <returns>The configured argument.</returns>
public Argument AddCompletions(params string[] values)
{
Completions.Add(values);
return this;
}

/// <summary>
/// Adds completions for the argument.
/// </summary>
/// <param name="complete">A function that will be called to provide completions.</param>
/// <returns>The option being extended.</returns>
public Argument AddCompletions(Func<CompletionContext, IEnumerable<string>> complete)
{
Completions.Add(complete);
return this;
}

/// <summary>
/// Adds completions for the argument.
/// </summary>
/// <param name="complete">A function that will be called to provide completions.</param>
/// <returns>The configured argument.</returns>
public Argument AddCompletions(Func<CompletionContext, IEnumerable<CompletionItem>> complete)
{
Completions.Add(complete);
return this;
}

/// <summary>
/// Configures the argument to accept only the specified values, and to suggest them as command line completions.
/// </summary>
/// <param name="values">The values that are allowed for the argument.</param>
/// <returns>The configured argument.</returns>
public Argument AcceptOnlyFromAmong(params string[] values)
{
AllowedValues?.Clear();
AddAllowedValues(values);
Completions.Clear();
Completions.Add(values);

return this;
}

/// <summary>
/// Configures the argument to accept only values representing legal file paths.
/// </summary>
/// <returns>The configured argument.</returns>
public Argument LegalFilePathsOnly()
{
var invalidPathChars = Path.GetInvalidPathChars();

AddValidator(result =>
{
for (var i = 0; i < result.Tokens.Count; i++)
{
var token = result.Tokens[i];

// File class no longer check invalid character
// https://blogs.msdn.microsoft.com/jeremykuhne/2018/03/09/custom-directory-enumeration-in-net-core-2-1/
var invalidCharactersIndex = token.Value.IndexOfAny(invalidPathChars);

if (invalidCharactersIndex >= 0)
{
result.ErrorMessage = result.LocalizationResources.InvalidCharactersInPath(token.Value[invalidCharactersIndex]);
}
}
});

return this;
}

/// <summary>
/// Configures the argument to accept only values representing legal file names.
/// </summary>
/// <remarks>A parse error will result, for example, if file path separators are found in the parsed value.</remarks>
/// <returns>The configured argument.</returns>
public Argument LegalFileNamesOnly()
{
var invalidFileNameChars = Path.GetInvalidFileNameChars();

AddValidator(result =>
{
for (var i = 0; i < result.Tokens.Count; i++)
{
var token = result.Tokens[i];
var invalidCharactersIndex = token.Value.IndexOfAny(invalidFileNameChars);

if (invalidCharactersIndex >= 0)
{
result.ErrorMessage = result.LocalizationResources.InvalidCharactersInFileName(token.Value[invalidCharactersIndex]);
}
}
});

return this;
}

/// <summary>
/// Parses a command line string value using the argument.
/// </summary>
/// <remarks>The command line string input will be split into tokens as if it had been passed on the command line.</remarks>
/// <param name="commandLine">A command line string to parse, which can include spaces and quotes equivalent to what can be entered into a terminal.</param>
/// <returns>A parse result describing the outcome of the parse operation.</returns>
public ParseResult Parse(string commandLine) =>
this.GetOrCreateDefaultSimpleParser().Parse(commandLine);

/// <summary>
/// Parses a command line string value using the argument.
/// </summary>
/// <param name="args">The string arguments to parse.</param>
/// <returns>A parse result describing the outcome of the parse operation.</returns>
public ParseResult Parse(string[] args) =>
this.GetOrCreateDefaultSimpleParser().Parse(args);
}
}
Loading