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
28 changes: 22 additions & 6 deletions Core/NetArgumentParser/Attributes/EnumValueOptionAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Reflection;
using NetArgumentParser.Generators;
using NetArgumentParser.Options;
Expand Down Expand Up @@ -27,7 +28,9 @@ public EnumValueOptionAttribute(
bool useDefaultChoices = true,
string[]? aliases = null,
T[]? choices = null,
string[]? beforeParseChoices = null)
string[]? beforeParseChoices = null,
bool addChoicesToDescription = false,
bool addBeforeParseChoicesToDescription = false)
: base(
defaultValue,
longName ?? throw new ArgumentNullException(nameof(longName)),
Expand All @@ -40,7 +43,9 @@ public EnumValueOptionAttribute(
ignoreCaseInChoices,
aliases,
choices,
beforeParseChoices)
beforeParseChoices,
addChoicesToDescription,
addBeforeParseChoicesToDescription)
{
UseDefaultChoices = useDefaultChoices;
}
Expand All @@ -58,10 +63,11 @@ public EnumValueOptionAttribute(
bool useDefaultChoices = true,
string[]? aliases = null,
T[]? choices = null,
string[]? beforeParseChoices = null)
string[]? beforeParseChoices = null,
bool addChoicesToDescription = false,
bool addBeforeParseChoicesToDescription = false)
: base(
choices,
beforeParseChoices,
longName ?? throw new ArgumentNullException(nameof(longName)),
shortName ?? throw new ArgumentNullException(nameof(shortName)),
description ?? throw new ArgumentNullException(nameof(description)),
Expand All @@ -70,7 +76,10 @@ public EnumValueOptionAttribute(
isHidden,
isFinal,
ignoreCaseInChoices,
aliases)
aliases,
beforeParseChoices,
addChoicesToDescription,
addBeforeParseChoicesToDescription)
{
UseDefaultChoices = useDefaultChoices;
}
Expand All @@ -85,7 +94,7 @@ public override ICommonOption CreateOption(object source, PropertyInfo propertyI
if (!CanCreateOption(source, propertyInfo))
throw new CannotCreateOptionException(null, propertyInfo);

return new EnumValueOption<T>(
var enumValueOption = new EnumValueOption<T>(
LongName,
ShortName,
Description,
Expand All @@ -101,5 +110,12 @@ public override ICommonOption CreateOption(object source, PropertyInfo propertyI
DefaultValue,
valueRestriction: null,
t => propertyInfo.SetValue(source, t));

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

return enumValueOption;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ public MultipleValueOptionAttribute(
int numberOfItemsToCapture = -1)
: base(
choices: null,
beforeParseChoices: null,
longName ?? throw new ArgumentNullException(nameof(longName)),
shortName ?? throw new ArgumentNullException(nameof(shortName)),
description ?? throw new ArgumentNullException(nameof(description)),
Expand All @@ -71,7 +70,8 @@ public MultipleValueOptionAttribute(
isHidden,
isFinal,
ignoreCaseInChoices,
aliases)
aliases,
beforeParseChoices: null)
{
IgnoreOrderInChoices = ignoreOrderInChoices;
ContextCapture = CreateContextCapture(contextCaptureType, numberOfItemsToCapture);
Expand Down
42 changes: 33 additions & 9 deletions Core/NetArgumentParser/Attributes/ValueOptionAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using NetArgumentParser.Generators;
using NetArgumentParser.Options;
Expand Down Expand Up @@ -27,10 +28,11 @@ public ValueOptionAttribute(
bool ignoreCaseInChoices = false,
string[]? aliases = null,
T[]? choices = null,
string[]? beforeParseChoices = null)
string[]? beforeParseChoices = null,
bool addChoicesToDescription = false,
bool addBeforeParseChoicesToDescription = false)
: this(
choices,
beforeParseChoices,
longName ?? throw new ArgumentNullException(nameof(longName)),
shortName ?? throw new ArgumentNullException(nameof(shortName)),
description ?? throw new ArgumentNullException(nameof(description)),
Expand All @@ -39,7 +41,10 @@ public ValueOptionAttribute(
isHidden,
isFinal,
ignoreCaseInChoices,
aliases)
aliases,
beforeParseChoices,
addChoicesToDescription,
addBeforeParseChoicesToDescription)
{
DefaultValue = new DefaultOptionValue<T>(defaultValue);
}
Expand All @@ -54,10 +59,12 @@ public ValueOptionAttribute(
bool isHidden = false,
bool isFinal = false,
bool ignoreCaseInChoices = false,
string[]? aliases = null)
string[]? aliases = null,
string[]? beforeParseChoices = null,
bool addChoicesToDescription = false,
bool addBeforeParseChoicesToDescription = false)
: this(
choices: null,
beforeParseChoices: null,
longName ?? throw new ArgumentNullException(nameof(longName)),
shortName ?? throw new ArgumentNullException(nameof(shortName)),
description ?? throw new ArgumentNullException(nameof(description)),
Expand All @@ -66,13 +73,15 @@ public ValueOptionAttribute(
isHidden,
isFinal,
ignoreCaseInChoices,
aliases)
aliases,
beforeParseChoices,
addChoicesToDescription,
addBeforeParseChoicesToDescription)
{
}

public ValueOptionAttribute(
T[]? choices,
string[]? beforeParseChoices,
string longName,
string shortName = "",
string description = "",
Expand All @@ -81,7 +90,10 @@ public ValueOptionAttribute(
bool isHidden = false,
bool isFinal = false,
bool ignoreCaseInChoices = false,
string[]? aliases = null)
string[]? aliases = null,
string[]? beforeParseChoices = null,
bool addChoicesToDescription = false,
bool addBeforeParseChoicesToDescription = false)
: base(
longName ?? throw new ArgumentNullException(nameof(longName)),
shortName ?? throw new ArgumentNullException(nameof(shortName)),
Expand All @@ -95,12 +107,17 @@ public ValueOptionAttribute(

MetaVariable = metaVariable;
IgnoreCaseInChoices = ignoreCaseInChoices;
AddChoicesToDescription = addChoicesToDescription;
AddBeforeParseChoicesToDescription = addBeforeParseChoicesToDescription;

Choices = choices;
BeforeParseChoices = beforeParseChoices;
}

public string MetaVariable { get; }
public bool IgnoreCaseInChoices { get; }
public bool AddChoicesToDescription { get; }
public bool AddBeforeParseChoicesToDescription { get; }

public IEnumerable<T>? Choices { get; }
public IEnumerable<string>? BeforeParseChoices { get; }
Expand All @@ -116,7 +133,7 @@ public override ICommonOption CreateOption(object source, PropertyInfo propertyI
if (!CanCreateOption(source, propertyInfo))
throw new CannotCreateOptionException(null, propertyInfo);

return new ValueOption<T>(
var valueOption = new ValueOption<T>(
LongName,
ShortName,
Description,
Expand All @@ -131,5 +148,12 @@ public override ICommonOption CreateOption(object source, PropertyInfo propertyI
DefaultValue,
null,
t => propertyInfo.SetValue(source, t));

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.4</Version>
<Version>1.0.5</Version>
<Product>NetArgumentParser</Product>
<Title>NetArgumentParser</Title>
<Authors>yakovypg</Authors>
Expand Down
10 changes: 8 additions & 2 deletions Documentation/ParserGenerationUsingAttributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ internal class CustomParserConfig
useDefaultChoices: false,
aliases: ["file-mode"],
choices: [FileMode.Create, FileMode.Open],
beforeParseChoices: ["Create", "Open"])
beforeParseChoices: ["Create", "Open"],
addChoicesToDescription: false,
addBeforeParseChoicesToDescription: true)
]
public FileMode Mode { get; set; }

Expand Down Expand Up @@ -139,14 +141,18 @@ internal class CustomParserConfig
isFinal: false,
aliases: [],
choices: [0, 45, 90],
beforeParseChoices: ["0", "45", "90"])
beforeParseChoices: ["0", "45", "90"],
addChoicesToDescription: true,
addBeforeParseChoicesToDescription: false)
]
public double? Angle { get; set; }
}

internal record Point(double X, double Y, double Z);
```

Note that attributes allow you to add choices and before parse choices to the option description (if the corresponding option supports it) using `addChoicesToDescription` and `addBeforeParseChoicesToDescription` parameters, so you don't need to find the generated options to do this.

### Group Attributes
Goups can be configured using `OptionGroupAttribute` attribute. In addition to specifying the group header and description, you should specify the group ID. It is necessary for the correct placement of options, since groups can have the same header. Options that you want to put in the same group must be marked with an attribute with the same ID. You should't specify header and description for all group attributes with same id. It is enough to do this for only one attribute.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ public CustomParserConfig()
useDefaultChoices: false,
aliases: ["file-mode"],
choices: [FileMode.Create, FileMode.Open],
beforeParseChoices: ["Create", "Open"])
beforeParseChoices: ["Create", "Open"],
addChoicesToDescription: false,
addBeforeParseChoicesToDescription: true)
]
[OptionGroup("complex-values", "", "")]
public FileMode Mode { get; set; }
Expand Down Expand Up @@ -174,7 +176,9 @@ public CustomParserConfig()
isFinal: false,
aliases: [],
choices: [0, 45, 90],
beforeParseChoices: ["0", "45", "90"])
beforeParseChoices: ["0", "45", "90"],
addChoicesToDescription: true,
addBeforeParseChoicesToDescription: false)
]
[OptionGroup("values", "", "")]
public double? Angle { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<a href="https://github.com/yakovypg/NetArgumentParser/blob/main/LICENSE">
<img src="https://img.shields.io/badge/License-GPLv3-darkyellow.svg" alt="license" />
</a>
<img src="https://img.shields.io/badge/Version-1.0.4-red.svg" alt="version" />
<img src="https://img.shields.io/badge/Version-1.0.5-red.svg" alt="version" />
<img src="https://img.shields.io/badge/C%23-12.0-blue" alt="csharp" />
</p>

Expand Down