Skip to content

Support attribute of array of FileInfo or DirectoryInfo #385

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 127 additions & 0 deletions src/System.CommandLine.Tests/ParsingValidationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.CommandLine.Invocation;
using System.IO;
using System.Linq;
using System.Reflection;
using FluentAssertions;
using Xunit;
using Xunit.Abstractions;
Expand Down Expand Up @@ -192,6 +193,66 @@ public void An_argument_can_be_invalid_based_on_file_existence()
e.Message == $"File does not exist: {guid}");
}

[Fact]
public void An_argument_with_multiple_file_info_can_be_invalid_based_on_first_file_existence()
{
var command = new Command(
"move",
argument: new Argument<FileInfo[]>
{
Arity = ArgumentArity.ZeroOrMore
}.ExistingOnly());
command.AddOption(
new Option(
"--to",
argument: new Argument
{
Arity = ArgumentArity.ExactlyOne
}));

Guid guid1 = Guid.NewGuid();
Guid guid2 = Guid.NewGuid();
var result =
command.Parse(
$@"move ""{guid1}"" ""{guid2}"" --to ""{Path.Combine(Directory.GetCurrentDirectory(), ".trash")}""");

result.Errors
.Should()
.HaveCount(1)
.And
.Contain(e => e.SymbolResult.Name == "move" && e.Message == $"File does not exist: {guid1}");
}

[Fact]
public void An_argument_with_multiple_file_info_can_be_invalid_based_on_second_file_existence()
{
var command = new Command(
"move",
argument: new Argument<FileInfo[]>
{
Arity = ArgumentArity.ZeroOrMore
}.ExistingOnly());
command.AddOption(
new Option(
"--to",
argument: new Argument
{
Arity = ArgumentArity.ExactlyOne
}));

var executingAssemblyLocation = Assembly.GetExecutingAssembly().Location;
var guid = Guid.NewGuid();
var result =
command.Parse(
$@"move ""{executingAssemblyLocation}"" ""{guid}"" --to ""{Path.Combine(Directory.GetCurrentDirectory(), ".trash")}""");

result.Errors
.Should()
.HaveCount(1)
.And
.Contain(e => e.SymbolResult.Name == "move" && e.Message == $"File does not exist: {guid}");
}

[Fact]
public void An_argument_can_be_invalid_based_on_directory_existence()
{
Expand Down Expand Up @@ -223,6 +284,72 @@ public void An_argument_can_be_invalid_based_on_directory_existence()
e.Message == $"Directory does not exist: {trash}");
}

[Fact]
public void An_argument_with_multiple_directory_info_can_be_invalid_based_on_first_directory_existence()
{
var command = new Command(
"move",
argument: new Argument
{
Arity = ArgumentArity.ExactlyOne
});
command.AddOption(
new Option("--to",
argument: new Argument<DirectoryInfo[]>
{
Arity = ArgumentArity.ZeroOrMore
}.ExistingOnly()));

var currentDirectory = Directory.GetCurrentDirectory();
var trash1 = Path.Combine(currentDirectory, ".trash1");
var trash2 = Path.Combine(currentDirectory, ".trash2");

var commandLine = $@"move ""{currentDirectory}"" --to ""{trash1}"" ""{trash2}""";

var result = command.Parse(commandLine);

_output.WriteLine(result.Diagram());

result.Errors
.Should()
.HaveCount(1)
.And
.Contain(e => e.SymbolResult.Name == "to" && e.Message == $"Directory does not exist: {trash1}");
}

[Fact]
public void An_argument_with_multiple_directory_info_can_be_invalid_based_on_second_directory_existence()
{
var command = new Command(
"move",
argument: new Argument
{
Arity = ArgumentArity.ExactlyOne
});
command.AddOption(
new Option("--to",
argument: new Argument<DirectoryInfo[]>
{
Arity = ArgumentArity.ZeroOrMore
}.ExistingOnly()));

var currentDirectory = Directory.GetCurrentDirectory();
var executionAssemblyPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var trash = Path.Combine(currentDirectory, ".trash2");

var commandLine = $@"move ""{currentDirectory}"" --to ""{executionAssemblyPath}"" ""{trash}""";

var result = command.Parse(commandLine);

_output.WriteLine(result.Diagram());

result.Errors
.Should()
.HaveCount(1)
.And
.Contain(e => e.SymbolResult.Name == "to" && e.Message == $"Directory does not exist: {trash}");
}

[Fact]
public void A_command_with_subcommands_is_invalid_to_invoke_if_it_has_no_handler()
{
Expand Down
20 changes: 20 additions & 0 deletions src/System.CommandLine/Builder/ArgumentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,26 @@ public static Argument<DirectoryInfo> ExistingOnly(this Argument<DirectoryInfo>
return argument;
}

public static Argument<FileInfo[]> ExistingOnly(this Argument<FileInfo[]> argument)
{
argument.AddValidator(symbol =>
symbol.Arguments
.Where(filePath => !File.Exists(filePath))
.Select(symbol.ValidationMessages.FileDoesNotExist)
.FirstOrDefault());
return argument;
}

public static Argument<DirectoryInfo[]> ExistingOnly(this Argument<DirectoryInfo[]> argument)
{
argument.AddValidator(symbol =>
symbol.Arguments
.Where(filePath => !Directory.Exists(filePath))
.Select(symbol.ValidationMessages.DirectoryDoesNotExist)
.FirstOrDefault());
return argument;
}

public static TArgument LegalFilePathsOnly<TArgument>(
this TArgument argument)
where TArgument : Argument
Expand Down