Skip to content

merge Validate clas into ArgumentValidation #2077

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 1 commit into from
Mar 7, 2023
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
46 changes: 40 additions & 6 deletions src/System.CommandLine/ArgumentValidation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Collections.Generic;
using System.CommandLine.Parsing;
using System.IO;

namespace System.CommandLine
Expand All @@ -18,7 +19,7 @@ public static class ArgumentValidation
/// <returns>The configured argument.</returns>
public static Argument<FileInfo> AcceptExistingOnly(this Argument<FileInfo> argument)
{
argument.Validators.Add(Validate.FileExists);
argument.Validators.Add(FileOrDirectoryExists<FileInfo>);
return argument;
}

Expand All @@ -29,7 +30,7 @@ public static Argument<FileInfo> AcceptExistingOnly(this Argument<FileInfo> argu
/// <returns>The configured argument.</returns>
public static Argument<DirectoryInfo> AcceptExistingOnly(this Argument<DirectoryInfo> argument)
{
argument.Validators.Add(Validate.DirectoryExists);
argument.Validators.Add(FileOrDirectoryExists<DirectoryInfo>);
return argument;
}

Expand All @@ -40,7 +41,7 @@ public static Argument<DirectoryInfo> AcceptExistingOnly(this Argument<Directory
/// <returns>The configured argument.</returns>
public static Argument<FileSystemInfo> AcceptExistingOnly(this Argument<FileSystemInfo> argument)
{
argument.Validators.Add(Validate.FileOrDirectoryExists);
argument.Validators.Add(FileOrDirectoryExists<FileSystemInfo>);
return argument;
}

Expand All @@ -54,18 +55,51 @@ public static Argument<T> AcceptExistingOnly<T>(this Argument<T> argument)
{
if (typeof(IEnumerable<FileInfo>).IsAssignableFrom(typeof(T)))
{
argument.Validators.Add(Validate.FileExists);
argument.Validators.Add(FileOrDirectoryExists<FileInfo>);
}
else if (typeof(IEnumerable<DirectoryInfo>).IsAssignableFrom(typeof(T)))
{
argument.Validators.Add(Validate.DirectoryExists);
argument.Validators.Add(FileOrDirectoryExists<DirectoryInfo>);
}
else
{
argument.Validators.Add(Validate.FileOrDirectoryExists);
argument.Validators.Add(FileOrDirectoryExists<FileSystemInfo>);
}

return argument;
}

private static void FileOrDirectoryExists<T>(ArgumentResult result)
where T : FileSystemInfo
{
// both FileInfo and DirectoryInfo are sealed so following checks are enough
bool checkFile = typeof(T) != typeof(DirectoryInfo);
bool checkDirectory = typeof(T) != typeof(FileInfo);

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

if (checkFile && checkDirectory)
{
#if NET7_0_OR_GREATER
if (!Path.Exists(token.Value))
#else
if (!Directory.Exists(token.Value) && !File.Exists(token.Value))
#endif
{
result.AddError(LocalizationResources.FileOrDirectoryDoesNotExist(token.Value));
}
}
else if (checkDirectory && !Directory.Exists(token.Value))
{
result.AddError(LocalizationResources.DirectoryDoesNotExist(token.Value));
}
else if (checkFile && !Directory.Exists(token.Value) && !File.Exists(token.Value))
{
result.AddError(LocalizationResources.FileDoesNotExist(token.Value));
}
}
}
}
}
12 changes: 5 additions & 7 deletions src/System.CommandLine/OptionValidation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public static class OptionValidation
/// <returns>The option being extended.</returns>
public static Option<FileInfo> AcceptExistingOnly(this Option<FileInfo> option)
{
option.Argument.Validators.Add(Validate.FileExists);
option._argument.AcceptExistingOnly();

return option;
}

Expand All @@ -29,7 +30,7 @@ public static Option<FileInfo> AcceptExistingOnly(this Option<FileInfo> option)
/// <returns>The option being extended.</returns>
public static Option<DirectoryInfo> AcceptExistingOnly(this Option<DirectoryInfo> option)
{
option.Argument.Validators.Add(Validate.DirectoryExists);
option._argument.AcceptExistingOnly();
return option;
}

Expand All @@ -40,7 +41,7 @@ public static Option<DirectoryInfo> AcceptExistingOnly(this Option<DirectoryInfo
/// <returns>The option being extended.</returns>
public static Option<FileSystemInfo> AcceptExistingOnly(this Option<FileSystemInfo> option)
{
option.Argument.Validators.Add(Validate.FileOrDirectoryExists);
option._argument.AcceptExistingOnly();
return option;
}

Expand All @@ -52,10 +53,7 @@ public static Option<FileSystemInfo> AcceptExistingOnly(this Option<FileSystemIn
public static Option<T> AcceptExistingOnly<T>(this Option<T> option)
where T : IEnumerable<FileSystemInfo>
{
if (option.Argument is Argument<T> arg)
{
arg.AcceptExistingOnly();
}
option._argument.AcceptExistingOnly();

return option;
}
Expand Down
2 changes: 1 addition & 1 deletion src/System.CommandLine/Option{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace System.CommandLine
/// <typeparam name="T">The <see cref="System.Type"/> that the option's arguments are expected to be parsed as.</typeparam>
public class Option<T> : Option, IValueDescriptor<T>
{
private readonly Argument<T> _argument;
internal readonly Argument<T> _argument;

/// <inheritdoc/>
public Option(
Expand Down
46 changes: 0 additions & 46 deletions src/System.CommandLine/Validate.cs

This file was deleted.