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

Show allowed values in help text #369

Merged
merged 1 commit into from
Jun 13, 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
3 changes: 3 additions & 0 deletions src/CommandLineUtils/Attributes/AllowedValuesAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations;

namespace McMaster.Extensions.CommandLineUtils
Expand All @@ -28,6 +29,8 @@ public AllowedValuesAttribute(params string[] allowedValues)
{
}

internal ReadOnlyCollection<string> AllowedValues => Array.AsReadOnly(this._allowedValues);

/// <summary>
/// Initializes an instance of <see cref="AllowedValuesAttribute"/>.
/// </summary>
Expand Down
15 changes: 15 additions & 0 deletions src/CommandLineUtils/HelpText/DefaultHelpTextGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Linq;
using System.Reflection;
using System.Text;
using McMaster.Extensions.CommandLineUtils.Validation;

namespace McMaster.Extensions.CommandLineUtils.HelpText
{
Expand Down Expand Up @@ -196,6 +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)
{
description += $"\nAllowed values are: {string.Join(", ", allowedValuesAttribute.AllowedValues)}.";
}

var wrappedDescription = IndentWriter?.Write(description);
var message = string.Format(outputFormat, arg.Name, wrappedDescription);

Expand Down Expand Up @@ -231,6 +239,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)
{
description+= $"\nAllowed values are: {string.Join(", ", allowedValuesAttribute.AllowedValues)}.";
}

var wrappedDescription = IndentWriter?.Write(description);
var message = string.Format(outputFormat, Format(opt), wrappedDescription);

Expand Down
2 changes: 2 additions & 0 deletions src/CommandLineUtils/Validation/AttributeValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public AttributeValidator(ValidationAttribute attribute)
_attribute = attribute ?? throw new ArgumentNullException(nameof(attribute));
}

internal ValidationAttribute ValidationAttribute => this._attribute;

/// <summary>
/// Gets the validation result for a command line option.
/// </summary>
Expand Down
40 changes: 29 additions & 11 deletions test/CommandLineUtils.Tests/DefaultHelpTextGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,25 +108,31 @@ 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<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<SomeEnum>("SomeEnumArgument", "enum arg desc.");
var helpText = GetHelpText(app);

Assert.Equal(@"Usage: [options] <SomeStringArgument> <SomeEnumArgument>
Assert.Equal(@"Usage: [options] <SomeStringArgument> <RestrictedStringArgument> <SomeEnumArgument>

Arguments:
SomeStringArgument string arg desc.
SomeEnumArgument enum arg desc.
Allowed values are: None, Normal, Extreme.
SomeStringArgument string arg desc.
RestrictedStringArgument restricted string arg desc.
Allowed values are: Foo, Bar.
SomeEnumArgument enum arg desc.
Allowed values are: None, Normal, Extreme.

Options:
-?|-h|--help Show help information.
--strOpt <E> str option desc.
--intOpt <E> int option desc.
--enumOpt <E> enum option desc.
Allowed values are: None, Normal, Extreme.
-?|-h|--help Show help information.
--strOpt <E> str option desc.
--rStrOpt <E> restricted str option desc.
Allowed values are: Foo, Bar.
--intOpt <E> int option desc.
--enumOpt <E> enum option desc.
Allowed values are: None, Normal, Extreme.

",
helpText,
Expand All @@ -141,15 +147,19 @@ public void ShowHelpFromAttributes()
app.Conventions.UseDefaultConventions();
var helpText = GetHelpText(app);

Assert.Equal(@"Usage: test [options] <SomeStringArgument> <SomeEnumArgument>
Assert.Equal(@"Usage: test [options] <SomeStringArgument> <RestrictedStringArgument> <SomeEnumArgument>

Arguments:
SomeStringArgument string arg desc.
RestrictedStringArgument restricted string arg desc.
Allowed values are: Foo, Bar.
SomeEnumArgument enum arg desc.
Allowed values are: None, Normal, Extreme.

Options:
-strOpt|--str-opt <STR_OPT> str option desc.
-rStrOpt|--r-str-opt <R_STR_OPT> restricted str option desc.
Allowed values are: Foo, Bar.
-intOpt|--int-opt <INT_OPT> int option desc.
-enumOpt|--verbosity <VERBOSITY> enum option desc.
Allowed values are: None, Normal, Extreme.
Expand All @@ -165,6 +175,10 @@ public class MyApp
[Option(ShortName = "strOpt", Description = "str option desc.")]
public string strOpt { get; set; }

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

[Option(ShortName = "intOpt", Description = "int option desc.")]
public int intOpt { get; set; }

Expand All @@ -174,7 +188,11 @@ public class MyApp
[Argument(0, Description = "string arg desc.")]
public string SomeStringArgument { get; set; }

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

[Argument(2, Description = "enum arg desc.")]
public SomeEnum SomeEnumArgument { get; set; }
}

Expand Down