2
2
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
3
4
4
using System . Collections . Generic ;
5
+ using System . CommandLine . Parsing ;
5
6
using System . IO ;
6
7
7
8
namespace System . CommandLine
@@ -18,7 +19,7 @@ public static class ArgumentValidation
18
19
/// <returns>The configured argument.</returns>
19
20
public static Argument < FileInfo > AcceptExistingOnly ( this Argument < FileInfo > argument )
20
21
{
21
- argument . Validators . Add ( Validate . FileExists ) ;
22
+ argument . Validators . Add ( FileOrDirectoryExists < FileInfo > ) ;
22
23
return argument ;
23
24
}
24
25
@@ -29,7 +30,7 @@ public static Argument<FileInfo> AcceptExistingOnly(this Argument<FileInfo> argu
29
30
/// <returns>The configured argument.</returns>
30
31
public static Argument < DirectoryInfo > AcceptExistingOnly ( this Argument < DirectoryInfo > argument )
31
32
{
32
- argument . Validators . Add ( Validate . DirectoryExists ) ;
33
+ argument . Validators . Add ( FileOrDirectoryExists < DirectoryInfo > ) ;
33
34
return argument ;
34
35
}
35
36
@@ -40,7 +41,7 @@ public static Argument<DirectoryInfo> AcceptExistingOnly(this Argument<Directory
40
41
/// <returns>The configured argument.</returns>
41
42
public static Argument < FileSystemInfo > AcceptExistingOnly ( this Argument < FileSystemInfo > argument )
42
43
{
43
- argument . Validators . Add ( Validate . FileOrDirectoryExists ) ;
44
+ argument . Validators . Add ( FileOrDirectoryExists < FileSystemInfo > ) ;
44
45
return argument ;
45
46
}
46
47
@@ -54,18 +55,51 @@ public static Argument<T> AcceptExistingOnly<T>(this Argument<T> argument)
54
55
{
55
56
if ( typeof ( IEnumerable < FileInfo > ) . IsAssignableFrom ( typeof ( T ) ) )
56
57
{
57
- argument . Validators . Add ( Validate . FileExists ) ;
58
+ argument . Validators . Add ( FileOrDirectoryExists < FileInfo > ) ;
58
59
}
59
60
else if ( typeof ( IEnumerable < DirectoryInfo > ) . IsAssignableFrom ( typeof ( T ) ) )
60
61
{
61
- argument . Validators . Add ( Validate . DirectoryExists ) ;
62
+ argument . Validators . Add ( FileOrDirectoryExists < DirectoryInfo > ) ;
62
63
}
63
64
else
64
65
{
65
- argument . Validators . Add ( Validate . FileOrDirectoryExists ) ;
66
+ argument . Validators . Add ( FileOrDirectoryExists < FileSystemInfo > ) ;
66
67
}
67
68
68
69
return argument ;
69
70
}
71
+
72
+ private static void FileOrDirectoryExists < T > ( ArgumentResult result )
73
+ where T : FileSystemInfo
74
+ {
75
+ // both FileInfo and DirectoryInfo are sealed so following checks are enough
76
+ bool checkFile = typeof ( T ) != typeof ( DirectoryInfo ) ;
77
+ bool checkDirectory = typeof ( T ) != typeof ( FileInfo ) ;
78
+
79
+ for ( var i = 0 ; i < result . Tokens . Count ; i ++ )
80
+ {
81
+ var token = result . Tokens [ i ] ;
82
+
83
+ if ( checkFile && checkDirectory )
84
+ {
85
+ #if NET7_0_OR_GREATER
86
+ if ( ! Path . Exists ( token . Value ) )
87
+ #else
88
+ if ( ! Directory . Exists ( token . Value ) && ! File . Exists ( token . Value ) )
89
+ #endif
90
+ {
91
+ result . AddError ( LocalizationResources . FileOrDirectoryDoesNotExist ( token . Value ) ) ;
92
+ }
93
+ }
94
+ else if ( checkDirectory && ! Directory . Exists ( token . Value ) )
95
+ {
96
+ result . AddError ( LocalizationResources . DirectoryDoesNotExist ( token . Value ) ) ;
97
+ }
98
+ else if ( checkFile && ! Directory . Exists ( token . Value ) && ! File . Exists ( token . Value ) )
99
+ {
100
+ result . AddError ( LocalizationResources . FileDoesNotExist ( token . Value ) ) ;
101
+ }
102
+ }
103
+ }
70
104
}
71
105
}
0 commit comments