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
14 changes: 9 additions & 5 deletions Core/NetArgumentParser/Attributes/EnumValueOptionAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public EnumValueOptionAttribute(
string[]? beforeParseChoices = null,
bool addChoicesToDescription = false,
bool addBeforeParseChoicesToDescription = false,
bool addDefaultValueToDescription = false)
bool addDefaultValueToDescription = false,
string? valueRestriction = null)
: base(
defaultValue,
longName ?? throw new ArgumentNullException(nameof(longName)),
Expand All @@ -47,7 +48,8 @@ public EnumValueOptionAttribute(
beforeParseChoices,
addChoicesToDescription,
addBeforeParseChoicesToDescription,
addDefaultValueToDescription)
addDefaultValueToDescription,
valueRestriction)
{
UseDefaultChoices = useDefaultChoices;
}
Expand All @@ -68,7 +70,8 @@ public EnumValueOptionAttribute(
string[]? beforeParseChoices = null,
bool addChoicesToDescription = false,
bool addBeforeParseChoicesToDescription = false,
bool addDefaultValueToDescription = false)
bool addDefaultValueToDescription = false,
string? valueRestriction = null)
: base(
choices,
longName ?? throw new ArgumentNullException(nameof(longName)),
Expand All @@ -83,7 +86,8 @@ public EnumValueOptionAttribute(
beforeParseChoices,
addChoicesToDescription,
addBeforeParseChoicesToDescription,
addDefaultValueToDescription)
addDefaultValueToDescription,
valueRestriction)
{
UseDefaultChoices = useDefaultChoices;
}
Expand Down Expand Up @@ -112,7 +116,7 @@ public override ICommonOption CreateOption(object source, PropertyInfo propertyI
Choices,
BeforeParseChoices,
DefaultValue,
valueRestriction: null,
valueRestriction: ValueRestriction,
t => propertyInfo.SetValue(source, t));

if (AddBeforeParseChoicesToDescription && BeforeParseChoices is not null && BeforeParseChoices.Any())
Expand Down
18 changes: 15 additions & 3 deletions Core/NetArgumentParser/Attributes/MultipleValueOptionAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Reflection;
using NetArgumentParser.Generators;
using NetArgumentParser.Options;
using NetArgumentParser.Options.Configuration;
using NetArgumentParser.Options.Context;

namespace NetArgumentParser.Attributes;
Expand All @@ -29,7 +30,8 @@ public MultipleValueOptionAttribute(
string[]? aliases = null,
ContextCaptureType contextCaptureType = ContextCaptureType.None,
int numberOfItemsToCapture = -1,
bool addDefaultValueToDescription = false)
bool addDefaultValueToDescription = false,
string? valueRestriction = null)
: base(
defaultValue,
longName ?? throw new ArgumentNullException(nameof(longName)),
Expand All @@ -47,6 +49,7 @@ public MultipleValueOptionAttribute(
{
IgnoreOrderInChoices = ignoreOrderInChoices;
ContextCapture = CreateContextCapture(contextCaptureType, numberOfItemsToCapture);
ValueRestriction = CreateValueRestriction(valueRestriction);
}

public MultipleValueOptionAttribute(
Expand All @@ -62,7 +65,8 @@ public MultipleValueOptionAttribute(
string[]? aliases = null,
ContextCaptureType contextCaptureType = ContextCaptureType.None,
int numberOfItemsToCapture = -1,
bool addDefaultValueToDescription = false)
bool addDefaultValueToDescription = false,
string? valueRestriction = null)
: base(
choices: null,
longName ?? throw new ArgumentNullException(nameof(longName)),
Expand All @@ -79,6 +83,7 @@ public MultipleValueOptionAttribute(
{
IgnoreOrderInChoices = ignoreOrderInChoices;
ContextCapture = CreateContextCapture(contextCaptureType, numberOfItemsToCapture);
ValueRestriction = CreateValueRestriction(valueRestriction);
}
#pragma warning restore CA1019 // Define accessors for attribute arguments

Expand Down Expand Up @@ -118,7 +123,7 @@ public override ICommonOption CreateOption(object source, PropertyInfo propertyI
Aliases,
choices: null,
DefaultValue,
valueRestriction: null,
valueRestriction: ValueRestriction,
t => propertyInfo.SetValue(source, t),
ContextCapture);
}
Expand All @@ -138,4 +143,11 @@ public override ICommonOption CreateOption(object source, PropertyInfo propertyI
contextCaptureType,
itemsToCapture);
}

private static OptionValueRestriction<IList<T>>? CreateValueRestriction(string? data)
{
return data is not null && !string.IsNullOrWhiteSpace(data)
? OptionValueRestrictionParser.ParseForList<T>(data, true)
: null;
}
}
43 changes: 35 additions & 8 deletions Core/NetArgumentParser/Attributes/ValueOptionAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public ValueOptionAttribute(
string[]? beforeParseChoices = null,
bool addChoicesToDescription = false,
bool addBeforeParseChoicesToDescription = false,
bool addDefaultValueToDescription = false)
bool addDefaultValueToDescription = false,
string? valueRestriction = null)
: this(
choices,
longName ?? throw new ArgumentNullException(nameof(longName)),
Expand All @@ -46,7 +47,8 @@ public ValueOptionAttribute(
beforeParseChoices,
addChoicesToDescription,
addBeforeParseChoicesToDescription,
addDefaultValueToDescription)
addDefaultValueToDescription,
valueRestriction)
{
DefaultValue = new DefaultOptionValue<T>(defaultValue);
}
Expand All @@ -65,7 +67,8 @@ public ValueOptionAttribute(
string[]? beforeParseChoices = null,
bool addChoicesToDescription = false,
bool addBeforeParseChoicesToDescription = false,
bool addDefaultValueToDescription = false)
bool addDefaultValueToDescription = false,
string? valueRestriction = null)
: this(
choices: null,
longName ?? throw new ArgumentNullException(nameof(longName)),
Expand All @@ -80,7 +83,8 @@ public ValueOptionAttribute(
beforeParseChoices,
addChoicesToDescription,
addBeforeParseChoicesToDescription,
addDefaultValueToDescription)
addDefaultValueToDescription,
valueRestriction)
{
}

Expand All @@ -98,7 +102,8 @@ public ValueOptionAttribute(
string[]? beforeParseChoices = null,
bool addChoicesToDescription = false,
bool addBeforeParseChoicesToDescription = false,
bool addDefaultValueToDescription = false)
bool addDefaultValueToDescription = false,
string? valueRestriction = null)
: base(
longName ?? throw new ArgumentNullException(nameof(longName)),
shortName ?? throw new ArgumentNullException(nameof(shortName)),
Expand All @@ -118,6 +123,7 @@ public ValueOptionAttribute(

Choices = choices;
BeforeParseChoices = beforeParseChoices;
ValueRestriction = CreateValueRestriction(valueRestriction);
}

public string MetaVariable { get; }
Expand All @@ -128,6 +134,7 @@ public ValueOptionAttribute(

public IEnumerable<T>? Choices { get; }
public IEnumerable<string>? BeforeParseChoices { get; }
public OptionValueRestriction<T>? ValueRestriction { get; protected set; }
public DefaultOptionValue<T>? DefaultValue { get; }

protected override IReadOnlyList<Type> ValidPropertyTypes => [typeof(T)];
Expand All @@ -153,17 +160,37 @@ public override ICommonOption CreateOption(object source, PropertyInfo propertyI
Choices,
BeforeParseChoices,
DefaultValue,
null,
ValueRestriction,
t => propertyInfo.SetValue(source, t));

AddDefaultValueToDescriptionIfNeeded(valueOption);
AddChoicesToDescriptionIfNeeded(valueOption);

return valueOption;
}

private static OptionValueRestriction<T>? CreateValueRestriction(string? data)
{
return data is not null && !string.IsNullOrWhiteSpace(data)
? OptionValueRestrictionParser.Parse<T>(data, true)
: null;
}

private void AddDefaultValueToDescriptionIfNeeded(ValueOption<T> valueOption)
{
ExtendedArgumentNullException.ThrowIfNull(valueOption, nameof(valueOption));

if (AddDefaultValueToDescription)
valueOption.AddDefaultValueToDescription();
}

private void AddChoicesToDescriptionIfNeeded(ValueOption<T> valueOption)
{
ExtendedArgumentNullException.ThrowIfNull(valueOption, nameof(valueOption));

if (AddBeforeParseChoicesToDescription && BeforeParseChoices is not null && BeforeParseChoices.Any())
valueOption.AddBeforeParseChoicesToDescription();
else if (AddChoicesToDescription && Choices is not null && Choices.Any())
valueOption.AddChoicesToDescription();

return valueOption;
}
}
2 changes: 1 addition & 1 deletion Core/NetArgumentParser/NetArgumentParser.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<PackageId>NetArgumentParser</PackageId>
<Version>1.0.5</Version>
<Version>1.0.6</Version>
<Product>NetArgumentParser</Product>
<Title>NetArgumentParser</Title>
<Authors>yakovypg</Authors>
Expand Down
Loading