Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

namespace NetArgumentParser.Options.Configuration;

public record OptionValueRestriction<T>(Predicate<T> IsValueAllowed);
public record OptionValueRestriction<T>(Predicate<T> IsValueAllowed, string? ValueNotSatisfyRestrictionMessage = null);
15 changes: 12 additions & 3 deletions Core/NetArgumentParser/Options/ValueOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,17 @@ protected ValueOption(
if (valueRestriction is not null)
{
if (defaultValue is not null && !valueRestriction.IsValueAllowed(defaultValue.Value))
throw new OptionValueNotSatisfyRestrictionException(null, [$"{defaultValue.Value}"]);
{
throw new OptionValueNotSatisfyRestrictionException(
valueRestriction.ValueNotSatisfyRestrictionMessage,
[$"{defaultValue.Value}"]);
}

if (_choices.Count > 0 && _choices.Any(t => !valueRestriction.IsValueAllowed(t)))
{
throw new OptionValueNotSatisfyRestrictionException(
null, [$"{_choices.First(t => !valueRestriction.IsValueAllowed(t))}"]);
valueRestriction.ValueNotSatisfyRestrictionMessage,
[$"{_choices.First(t => !valueRestriction.IsValueAllowed(t))}"]);
}
}

Expand Down Expand Up @@ -384,7 +389,11 @@ protected void VerifyValueIsAllowed(T value, string[] valueSource)
ExtendedArgumentNullException.ThrowIfNull(valueSource, nameof(valueSource));

if (!IsValueSatisfyRestriction(value))
throw new OptionValueNotSatisfyRestrictionException(null, valueSource);
{
throw new OptionValueNotSatisfyRestrictionException(
ValueRestriction?.ValueNotSatisfyRestrictionMessage,
valueSource);
}

if (!IsValueSatisfyChoices(value))
{
Expand Down
10 changes: 10 additions & 0 deletions Documentation/OptionalArgumentsConfig.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,16 @@ var enumValueOption = new EnumValueOption<StringSplitOptions>("options", "o",
valueRestriction: new OptionValueRestriction<StringSplitOptions>(t => t != StringSplitOptions.None));
```

You can also specify a message to be shown if the value doesn't satisfy the restriction:

```cs
var restriction = new OptionValueRestriction<int>(t => t > 0, "angle must be greater than zero");

var valueOption = new ValueOption<int>("angle", "a",
description: "angle by which you want to rotate the image",
valueRestriction: restriction);
```

### Required Options
You can indicate that the option is required. In this case, if the input argument list doesn't contain a matching argument, an exception will be thrown.

Expand Down
4 changes: 3 additions & 1 deletion Examples/NetArgumentParser.Examples.AllUseCases/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@
shortName: "i",
description: "images that need to be processed",
isRequired: true,
valueRestriction: new OptionValueRestriction<IList<string>>(t => t.All(p => File.Exists(p))),
valueRestriction: new OptionValueRestriction<IList<string>>(
t => t.All(p => File.Exists(p)),
"all files must exist"),
contextCapture: new OneOrMoreContextCapture(),
afterValueParsingAction: t => resultValues.InputFiles = [.. t]),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@
shortName: "i",
description: "images that need to be processed",
isRequired: true,
valueRestriction: new OptionValueRestriction<IList<string>>(t => t.All(p => File.Exists(p))),
valueRestriction: new OptionValueRestriction<IList<string>>(
t => t.All(p => File.Exists(p)),
"all files must exist"),
contextCapture: new OneOrMoreContextCapture(),
afterValueParsingAction: t => resultValues.InputFiles = [.. t]),

Expand Down