diff --git a/Core/NetArgumentParser/Attributes/EnumValueOptionAttribute.cs b/Core/NetArgumentParser/Attributes/EnumValueOptionAttribute.cs index 37b9dd0..8339bf5 100644 --- a/Core/NetArgumentParser/Attributes/EnumValueOptionAttribute.cs +++ b/Core/NetArgumentParser/Attributes/EnumValueOptionAttribute.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using System.Reflection; using NetArgumentParser.Generators; using NetArgumentParser.Options; @@ -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)), @@ -40,7 +43,9 @@ public EnumValueOptionAttribute( ignoreCaseInChoices, aliases, choices, - beforeParseChoices) + beforeParseChoices, + addChoicesToDescription, + addBeforeParseChoicesToDescription) { UseDefaultChoices = useDefaultChoices; } @@ -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)), @@ -70,7 +76,10 @@ public EnumValueOptionAttribute( isHidden, isFinal, ignoreCaseInChoices, - aliases) + aliases, + beforeParseChoices, + addChoicesToDescription, + addBeforeParseChoicesToDescription) { UseDefaultChoices = useDefaultChoices; } @@ -85,7 +94,7 @@ public override ICommonOption CreateOption(object source, PropertyInfo propertyI if (!CanCreateOption(source, propertyInfo)) throw new CannotCreateOptionException(null, propertyInfo); - return new EnumValueOption( + var enumValueOption = new EnumValueOption( LongName, ShortName, Description, @@ -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; } } diff --git a/Core/NetArgumentParser/Attributes/MultipleValueOptionAttribute.cs b/Core/NetArgumentParser/Attributes/MultipleValueOptionAttribute.cs index 08edac7..fa4a08a 100644 --- a/Core/NetArgumentParser/Attributes/MultipleValueOptionAttribute.cs +++ b/Core/NetArgumentParser/Attributes/MultipleValueOptionAttribute.cs @@ -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)), @@ -71,7 +70,8 @@ public MultipleValueOptionAttribute( isHidden, isFinal, ignoreCaseInChoices, - aliases) + aliases, + beforeParseChoices: null) { IgnoreOrderInChoices = ignoreOrderInChoices; ContextCapture = CreateContextCapture(contextCaptureType, numberOfItemsToCapture); diff --git a/Core/NetArgumentParser/Attributes/ValueOptionAttribute.cs b/Core/NetArgumentParser/Attributes/ValueOptionAttribute.cs index ce3a33b..348a6af 100644 --- a/Core/NetArgumentParser/Attributes/ValueOptionAttribute.cs +++ b/Core/NetArgumentParser/Attributes/ValueOptionAttribute.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Reflection; using NetArgumentParser.Generators; using NetArgumentParser.Options; @@ -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)), @@ -39,7 +41,10 @@ public ValueOptionAttribute( isHidden, isFinal, ignoreCaseInChoices, - aliases) + aliases, + beforeParseChoices, + addChoicesToDescription, + addBeforeParseChoicesToDescription) { DefaultValue = new DefaultOptionValue(defaultValue); } @@ -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)), @@ -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 = "", @@ -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)), @@ -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? Choices { get; } public IEnumerable? BeforeParseChoices { get; } @@ -116,7 +133,7 @@ public override ICommonOption CreateOption(object source, PropertyInfo propertyI if (!CanCreateOption(source, propertyInfo)) throw new CannotCreateOptionException(null, propertyInfo); - return new ValueOption( + var valueOption = new ValueOption( LongName, ShortName, Description, @@ -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; } } diff --git a/Core/NetArgumentParser/NetArgumentParser.csproj b/Core/NetArgumentParser/NetArgumentParser.csproj index e79d3cd..90a5f9f 100644 --- a/Core/NetArgumentParser/NetArgumentParser.csproj +++ b/Core/NetArgumentParser/NetArgumentParser.csproj @@ -2,7 +2,7 @@ NetArgumentParser - 1.0.4 + 1.0.5 NetArgumentParser NetArgumentParser yakovypg diff --git a/Documentation/ParserGenerationUsingAttributes.md b/Documentation/ParserGenerationUsingAttributes.md index 1df4a46..bf80a89 100644 --- a/Documentation/ParserGenerationUsingAttributes.md +++ b/Documentation/ParserGenerationUsingAttributes.md @@ -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; } @@ -139,7 +141,9 @@ 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; } } @@ -147,6 +151,8 @@ internal class CustomParserConfig 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. diff --git a/Examples/NetArgumentParser.Examples.ParserGenerationUsingAttributes/Program.cs b/Examples/NetArgumentParser.Examples.ParserGenerationUsingAttributes/Program.cs index 264427a..2e91850 100644 --- a/Examples/NetArgumentParser.Examples.ParserGenerationUsingAttributes/Program.cs +++ b/Examples/NetArgumentParser.Examples.ParserGenerationUsingAttributes/Program.cs @@ -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; } @@ -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; } diff --git a/README.md b/README.md index c931574..184079b 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ license - version + version csharp