Skip to content

Commit 497f598

Browse files
authored
fix: show allowed values in help text correctly when multiple AllowedValuesAttribute are used (#375)
1 parent 17476ab commit 497f598

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

src/CommandLineUtils/HelpText/DefaultHelpTextGenerator.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,13 @@ protected virtual void GenerateArguments(
197197
? $"{arg.Description}\nAllowed values are: {string.Join(", ", enumNames)}."
198198
: arg.Description;
199199

200-
var attributeValidator = arg.Validators.Cast<AttributeValidator>().FirstOrDefault();
201-
202-
if (attributeValidator != null && attributeValidator.ValidationAttribute is AllowedValuesAttribute allowedValuesAttribute)
200+
foreach (var attributeValidator in arg.Validators.Cast<AttributeValidator>())
203201
{
204-
description += $"\nAllowed values are: {string.Join(", ", allowedValuesAttribute.AllowedValues)}.";
202+
if (attributeValidator?.ValidationAttribute is AllowedValuesAttribute allowedValuesAttribute)
203+
{
204+
description += $"\nAllowed values are: {string.Join(", ", allowedValuesAttribute.AllowedValues)}.";
205+
break;
206+
}
205207
}
206208

207209
var wrappedDescription = IndentWriter?.Write(description);
@@ -239,11 +241,13 @@ protected virtual void GenerateOptions(
239241
? $"{opt.Description}\nAllowed values are: {string.Join(", ", enumNames)}."
240242
: opt.Description;
241243

242-
var attributeValidator = opt.Validators.Cast<AttributeValidator>().FirstOrDefault();
243-
244-
if (attributeValidator != null && attributeValidator.ValidationAttribute is AllowedValuesAttribute allowedValuesAttribute)
244+
foreach (var attributeValidator in opt.Validators.Cast<AttributeValidator>())
245245
{
246-
description+= $"\nAllowed values are: {string.Join(", ", allowedValuesAttribute.AllowedValues)}.";
246+
if (attributeValidator?.ValidationAttribute is AllowedValuesAttribute allowedValuesAttribute)
247+
{
248+
description += $"\nAllowed values are: {string.Join(", ", allowedValuesAttribute.AllowedValues)}.";
249+
break;
250+
}
247251
}
248252

249253
var wrappedDescription = IndentWriter?.Write(description);

test/CommandLineUtils.Tests/DefaultHelpTextGeneratorTests.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections;
66
using System.Collections.Generic;
7+
using System.ComponentModel.DataAnnotations;
78
using System.IO;
89
using System.Text;
910
using McMaster.Extensions.CommandLineUtils.HelpText;
@@ -108,11 +109,11 @@ public void ShowHelp()
108109
var app = new CommandLineApplication();
109110
app.HelpOption();
110111
app.Option("--strOpt <E>", "str option desc.", CommandOptionType.SingleValue);
111-
app.Option("--rStrOpt <E>", "restricted str option desc.", CommandOptionType.SingleValue, o => o.Accepts().Values("Foo", "Bar"));
112+
app.Option("--rStrOpt <E>", "restricted str option desc.", CommandOptionType.SingleValue, o => o.IsRequired().Accepts().Values("Foo", "Bar"));
112113
app.Option<int>("--intOpt <E>", "int option desc.", CommandOptionType.SingleValue);
113114
app.Option<SomeEnum>("--enumOpt <E>", "enum option desc.", CommandOptionType.SingleValue);
114115
app.Argument("SomeStringArgument", "string arg desc.");
115-
app.Argument("RestrictedStringArgument", "restricted string arg desc.", a=>a.Accepts().Values("Foo", "Bar"));
116+
app.Argument("RestrictedStringArgument", "restricted string arg desc.", a => a.IsRequired().Accepts().Values("Foo", "Bar"));
116117
app.Argument<SomeEnum>("SomeEnumArgument", "enum arg desc.");
117118
var helpText = GetHelpText(app);
118119

@@ -176,6 +177,7 @@ public class MyApp
176177
public string strOpt { get; set; }
177178

178179
[Option(ShortName = "rStrOpt", Description = "restricted str option desc.")]
180+
[Required]
179181
[AllowedValues("Foo", "Bar")]
180182
public string rStrOpt { get; set; }
181183

@@ -189,6 +191,7 @@ public class MyApp
189191
public string SomeStringArgument { get; set; }
190192

191193
[Argument(1, Description = "restricted string arg desc.")]
194+
[Required]
192195
[AllowedValues("Foo", "Bar")]
193196
public string RestrictedStringArgument { get; set; }
194197

0 commit comments

Comments
 (0)