Skip to content

Commit

Permalink
fix: show allowed values in help text correctly when multiple `Allowe…
Browse files Browse the repository at this point in the history
…dValuesAttribute` are used (natemcmaster#375)
  • Loading branch information
scott-xu authored Aug 28, 2020
1 parent 17476ab commit 497f598
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
20 changes: 12 additions & 8 deletions src/CommandLineUtils/HelpText/DefaultHelpTextGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,13 @@ protected virtual void GenerateArguments(
? $"{arg.Description}\nAllowed values are: {string.Join(", ", enumNames)}."
: arg.Description;

var attributeValidator = arg.Validators.Cast<AttributeValidator>().FirstOrDefault();

if (attributeValidator != null && attributeValidator.ValidationAttribute is AllowedValuesAttribute allowedValuesAttribute)
foreach (var attributeValidator in arg.Validators.Cast<AttributeValidator>())
{
description += $"\nAllowed values are: {string.Join(", ", allowedValuesAttribute.AllowedValues)}.";
if (attributeValidator?.ValidationAttribute is AllowedValuesAttribute allowedValuesAttribute)
{
description += $"\nAllowed values are: {string.Join(", ", allowedValuesAttribute.AllowedValues)}.";
break;
}
}

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

var attributeValidator = opt.Validators.Cast<AttributeValidator>().FirstOrDefault();

if (attributeValidator != null && attributeValidator.ValidationAttribute is AllowedValuesAttribute allowedValuesAttribute)
foreach (var attributeValidator in opt.Validators.Cast<AttributeValidator>())
{
description+= $"\nAllowed values are: {string.Join(", ", allowedValuesAttribute.AllowedValues)}.";
if (attributeValidator?.ValidationAttribute is AllowedValuesAttribute allowedValuesAttribute)
{
description += $"\nAllowed values are: {string.Join(", ", allowedValuesAttribute.AllowedValues)}.";
break;
}
}

var wrappedDescription = IndentWriter?.Write(description);
Expand Down
7 changes: 5 additions & 2 deletions test/CommandLineUtils.Tests/DefaultHelpTextGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Text;
using McMaster.Extensions.CommandLineUtils.HelpText;
Expand Down Expand Up @@ -108,11 +109,11 @@ public void ShowHelp()
var app = new CommandLineApplication();
app.HelpOption();
app.Option("--strOpt <E>", "str option desc.", CommandOptionType.SingleValue);
app.Option("--rStrOpt <E>", "restricted str option desc.", CommandOptionType.SingleValue, o => o.Accepts().Values("Foo", "Bar"));
app.Option("--rStrOpt <E>", "restricted str option desc.", CommandOptionType.SingleValue, o => o.IsRequired().Accepts().Values("Foo", "Bar"));
app.Option<int>("--intOpt <E>", "int option desc.", CommandOptionType.SingleValue);
app.Option<SomeEnum>("--enumOpt <E>", "enum option desc.", CommandOptionType.SingleValue);
app.Argument("SomeStringArgument", "string arg desc.");
app.Argument("RestrictedStringArgument", "restricted string arg desc.", a=>a.Accepts().Values("Foo", "Bar"));
app.Argument("RestrictedStringArgument", "restricted string arg desc.", a => a.IsRequired().Accepts().Values("Foo", "Bar"));
app.Argument<SomeEnum>("SomeEnumArgument", "enum arg desc.");
var helpText = GetHelpText(app);

Expand Down Expand Up @@ -176,6 +177,7 @@ public class MyApp
public string strOpt { get; set; }

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

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

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

Expand Down

0 comments on commit 497f598

Please sign in to comment.