Skip to content

Commit cc21bd6

Browse files
committed
Fix validator in new version of System.CommandLine
1 parent e4fa49e commit cc21bd6

File tree

4 files changed

+17
-6
lines changed

4 files changed

+17
-6
lines changed

dotnet-json/Commands/FileArgument.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@ public FileArgument(string name, string? description = null)
2222
private void AddValidator()
2323
{
2424
this.AddValidator(symbol =>
25-
symbol.Tokens
25+
{
26+
symbol.ErrorMessage ??= symbol.Tokens
2627
.Select(t => t.Value)
2728
.Where(_ => !AllowNewFile) // Need to check AllowNewFile at this point because AddValidator() is called from constructor
2829
.Where(filePath => filePath != "-")
2930
.Where(filePath => !File.Exists(filePath))
3031
.Select(filePath => $"File does not exist: {filePath}")
31-
.FirstOrDefault());
32+
.FirstOrDefault();
33+
});
3234
}
3335
}
3436
}

dotnet-json/Commands/FileOption.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ public FileOption(string[] aliases, string? description = null)
2525
private void AddValidator()
2626
{
2727
this.AddValidator(symbol =>
28-
symbol.Tokens
28+
{
29+
symbol.ErrorMessage ??= symbol.Tokens
2930
.Select(t => t.Value)
3031
.Where(_ => !AllowNewFile) // Need to check AllowNewFile at this point because AddValidator() is called from constructor
3132
.Where(filePath => filePath != "-")
3233
.Where(filePath => !File.Exists(filePath))
3334
.Select(filePath => $"File does not exist: {filePath}")
34-
.FirstOrDefault());
35+
.FirstOrDefault();
36+
});
3537
}
3638
}
3739
}

dotnet-json/Commands/FilesArgument.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ public FilesArgument(string name, string? description = null)
2323
private void AddValidator()
2424
{
2525
this.AddValidator(symbol =>
26-
symbol.Tokens
26+
{
27+
symbol.ErrorMessage ??= symbol.Tokens
2728
.Select(t => t.Value)
2829
.Where(_ => !AllowNewFile) // Need to check AllowNewFile at this point because AddValidator() is called from constructor
2930
.Where(filePath => filePath != "-")
3031
.Where(filePath => !File.Exists(filePath))
3132
.Select(filePath => $"File does not exist: {filePath}")
32-
.FirstOrDefault());
33+
.FirstOrDefault();
34+
});
3335
}
3436
}
3537
}

dotnet-json/Commands/RemoveCommand.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ public class RemoveCommand : CommandBase, ICommandHandler
1313
public RemoveCommand()
1414
: base("remove", "Remove a value from the json")
1515
{
16+
Key.AddValidator(symbol =>
17+
{
18+
if (symbol.Tokens.Count == 1 && string.IsNullOrEmpty(symbol.Tokens[0].Value))
19+
symbol.ErrorMessage = "Removing an empty key is not allowed.";
20+
});
1621
AddArgument(Key);
1722

1823
AddAlias("rm");

0 commit comments

Comments
 (0)