Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A fix for "show allowed values in help text" #375

Merged
merged 2 commits into from
Aug 28, 2020
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
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
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