Skip to content

Commit 7693235

Browse files
authored
Added LegalFileNamesOnly to ArgumentExtensions (#1225)
* Added LegalFileNamesOnly to ArgumentExtensions * Added test cases for LegalFileNamesOnly
1 parent 063cbfe commit 7693235

19 files changed

+186
-1
lines changed

src/System.CommandLine.Tests/ParsingValidationTests.cs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,77 @@ public void LegalFilePathsOnly_accepts_option_arguments_containing_valid_path_ch
383383
}
384384
}
385385

386+
public class FileNameValidity
387+
{
388+
[Fact]
389+
public void LegalFileNamesOnly_rejects_command_arguments_containing_invalid_file_name_characters()
390+
{
391+
var command = new Command("the-command")
392+
{
393+
new Argument<string>().LegalFileNamesOnly()
394+
};
395+
396+
var invalidCharacter = Path.GetInvalidFileNameChars().First(c => c != '"');
397+
398+
var result = command.Parse($"the-command {invalidCharacter}");
399+
400+
result.Errors
401+
.Should()
402+
.Contain(e => e.SymbolResult.Symbol == command.Arguments.First() &&
403+
e.Message == $"Character not allowed in a file name: {invalidCharacter}");
404+
}
405+
406+
[Fact]
407+
public void LegalFileNamesOnly_rejects_option_arguments_containing_invalid_file_name_characters()
408+
{
409+
var command = new Command("the-command")
410+
{
411+
new Option<string>("-x").LegalFileNamesOnly()
412+
};
413+
414+
var invalidCharacter = Path.GetInvalidFileNameChars().First(c => c != '"');
415+
416+
var result = command.Parse($"the-command -x {invalidCharacter}");
417+
418+
result.Errors
419+
.Should()
420+
.Contain(e => e.SymbolResult.Symbol.Name == "x" &&
421+
e.Message == $"Character not allowed in a file name: {invalidCharacter}");
422+
}
423+
424+
[Fact]
425+
public void LegalFileNamesOnly_accepts_command_arguments_containing_valid_file_name_characters()
426+
{
427+
var command = new Command("the-command")
428+
{
429+
new Argument<string[]>().LegalFileNamesOnly()
430+
};
431+
432+
var validFileName = Path.GetFileName(Directory.GetCurrentDirectory());
433+
var validNonExistingFileName = Guid.NewGuid().ToString();
434+
435+
var result = command.Parse($"the-command {validFileName} {validNonExistingFileName}");
436+
437+
result.Errors.Should().BeEmpty();
438+
}
439+
440+
[Fact]
441+
public void LegalFileNamesOnly_accepts_option_arguments_containing_valid_file_name_characters()
442+
{
443+
var command = new Command("the-command")
444+
{
445+
new Option<string[]>("-x").LegalFileNamesOnly()
446+
};
447+
448+
var validFileName = Path.GetFileName(Directory.GetCurrentDirectory());
449+
var validNonExistingFileName = Guid.NewGuid().ToString();
450+
451+
var result = command.Parse($"the-command -x {validFileName} {validNonExistingFileName}");
452+
453+
result.Errors.Should().BeEmpty();
454+
}
455+
}
456+
386457
public class FileExistence
387458
{
388459
[Fact]

src/System.CommandLine/ArgumentExtensions.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public static TArgument LegalFilePathsOnly<TArgument>(
114114
where TArgument : Argument
115115
{
116116
var invalidPathChars = Path.GetInvalidPathChars();
117-
117+
118118
argument.AddValidator(symbol =>
119119
{
120120
for (var i = 0; i < symbol.Tokens.Count; i++)
@@ -137,6 +137,31 @@ public static TArgument LegalFilePathsOnly<TArgument>(
137137
return argument;
138138
}
139139

140+
public static TArgument LegalFileNamesOnly<TArgument>(
141+
this TArgument argument)
142+
where TArgument : Argument
143+
{
144+
var invalidFileNameChars = Path.GetInvalidFileNameChars();
145+
146+
argument.AddValidator(symbol =>
147+
{
148+
for (var i = 0; i < symbol.Tokens.Count; i++)
149+
{
150+
var token = symbol.Tokens[i];
151+
var invalidCharactersIndex = token.Value.IndexOfAny(invalidFileNameChars);
152+
153+
if (invalidCharactersIndex >= 0)
154+
{
155+
return symbol.ValidationMessages.InvalidCharactersInFileName(token.Value[invalidCharactersIndex]);
156+
}
157+
}
158+
159+
return null;
160+
});
161+
162+
return argument;
163+
}
164+
140165
public static ParseResult Parse(
141166
this Argument argument,
142167
string commandLine) =>

src/System.CommandLine/OptionExtensions.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,15 @@ public static TOption LegalFilePathsOnly<TOption>(
101101
return option;
102102
}
103103

104+
public static TOption LegalFileNamesOnly<TOption>(
105+
this TOption option)
106+
where TOption : Option
107+
{
108+
option.Argument.LegalFileNamesOnly();
109+
110+
return option;
111+
}
112+
104113
public static ParseResult Parse(
105114
this Option option,
106115
string commandLine) =>

src/System.CommandLine/Properties/Resources.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/System.CommandLine/Properties/Resources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,7 @@
171171
<data name="HelpOptionDescription" xml:space="preserve">
172172
<value>Show help and usage information</value>
173173
</data>
174+
<data name="InvalidCharactersInFileName" xml:space="preserve">
175+
<value>Character not allowed in a file name: {0}</value>
176+
</data>
174177
</root>

src/System.CommandLine/Properties/xlf/Resources.cs.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@
4747
<target state="new">Show help and usage information</target>
4848
<note />
4949
</trans-unit>
50+
<trans-unit id="InvalidCharactersInFileName">
51+
<source>Character not allowed in a file name: {0}</source>
52+
<target state="new">Character not allowed in a file name: {0}</target>
53+
<note />
54+
</trans-unit>
5055
<trans-unit id="InvalidCharactersInPath">
5156
<source>Character not allowed in a path: {0}</source>
5257
<target state="new">Character not allowed in a path: {0}</target>

src/System.CommandLine/Properties/xlf/Resources.de.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@
4747
<target state="new">Show help and usage information</target>
4848
<note />
4949
</trans-unit>
50+
<trans-unit id="InvalidCharactersInFileName">
51+
<source>Character not allowed in a file name: {0}</source>
52+
<target state="new">Character not allowed in a file name: {0}</target>
53+
<note />
54+
</trans-unit>
5055
<trans-unit id="InvalidCharactersInPath">
5156
<source>Character not allowed in a path: {0}</source>
5257
<target state="new">Character not allowed in a path: {0}</target>

src/System.CommandLine/Properties/xlf/Resources.es.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@
4747
<target state="new">Show help and usage information</target>
4848
<note />
4949
</trans-unit>
50+
<trans-unit id="InvalidCharactersInFileName">
51+
<source>Character not allowed in a file name: {0}</source>
52+
<target state="new">Character not allowed in a file name: {0}</target>
53+
<note />
54+
</trans-unit>
5055
<trans-unit id="InvalidCharactersInPath">
5156
<source>Character not allowed in a path: {0}</source>
5257
<target state="new">Character not allowed in a path: {0}</target>

src/System.CommandLine/Properties/xlf/Resources.fr.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@
4747
<target state="new">Show help and usage information</target>
4848
<note />
4949
</trans-unit>
50+
<trans-unit id="InvalidCharactersInFileName">
51+
<source>Character not allowed in a file name: {0}</source>
52+
<target state="new">Character not allowed in a file name: {0}</target>
53+
<note />
54+
</trans-unit>
5055
<trans-unit id="InvalidCharactersInPath">
5156
<source>Character not allowed in a path: {0}</source>
5257
<target state="new">Character not allowed in a path: {0}</target>

src/System.CommandLine/Properties/xlf/Resources.it.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@
4747
<target state="new">Show help and usage information</target>
4848
<note />
4949
</trans-unit>
50+
<trans-unit id="InvalidCharactersInFileName">
51+
<source>Character not allowed in a file name: {0}</source>
52+
<target state="new">Character not allowed in a file name: {0}</target>
53+
<note />
54+
</trans-unit>
5055
<trans-unit id="InvalidCharactersInPath">
5156
<source>Character not allowed in a path: {0}</source>
5257
<target state="new">Character not allowed in a path: {0}</target>

0 commit comments

Comments
 (0)