natemcmaster / CommandLineUtils

Command line parsing and utilities for .NET

Home Page:https://natemcmaster.github.io/CommandLineUtils/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Exception thrown when attempting to generate help text with custom IValidator present

adamsjdavid-work opened this issue · comments

When upgrading from V3.0 to V3.1, we experience test failures around the -h | --help flag. Namely, in our testing environment, running the help command is throwing an exception inside the McMaster Utility.

As part of our creation workflow, we have a custom implementation of IValidator called 'ExistingDirectoryOrTarFileValidator'.

private class ExistingDirectoryOrTarFileValidator : IValidator {[removed for brevity]}

It is added to an IValidationBuilder with the .Use(IValidator) method.

public static IValidationBuilder IfExistingFileThenHasTarExtension(this IValidationBuilder builder) { builder.Use(new ExistingDirectoryOrTarFileValidator()); return builder; }

This builder is used when creating a command option.

var inputOption = app.Option<string>( "-i|--input", "A directory, or a *.tar archive, containing all source files to be compiled.", CommandOptionType.MultipleValue) .IsRequired() .Accepts(p => p.ExistingFileOrDirectory() .IfExistingFileThenHasTarExtension());

Exception:

Unable to cast object of type 'ExistingDirectoryOrTarFileValidator' to type 'McMaster.Extensions.CommandLineUtils.Validation.AttributeValidator'.

Stack Trace:

at System.Linq.Enumerable.<CastIterator>d__63``1.MoveNext() in /_/src/System.Linq/src/System/Linq/Cast.cs:line 52 at McMaster.Extensions.CommandLineUtils.HelpText.DefaultHelpTextGenerator.GenerateOptions(CommandLineApplication application, TextWriter output, IReadOnlyList``1 visibleOptions, Int32 firstColumnWidth) at McMaster.Extensions.CommandLineUtils.HelpText.DefaultHelpTextGenerator.GenerateBody(CommandLineApplication application, TextWriter output) at McMaster.Extensions.CommandLineUtils.HelpText.DefaultHelpTextGenerator.Generate(CommandLineApplication application, TextWriter output) at McMaster.Extensions.CommandLineUtils.CommandLineProcessor.ProcessOption(OptionArgument arg) at McMaster.Extensions.CommandLineUtils.CommandLineProcessor.ProcessNext() at McMaster.Extensions.CommandLineUtils.CommandLineProcessor.Process() at McMaster.Extensions.CommandLineUtils.CommandLineApplication.Parse(String[] args) at McMaster.Extensions.CommandLineUtils.CommandLineApplication.<ExecuteAsync>d__157.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() in /_/src/System.Private.CoreLib/shared/System/Runtime/ExceptionServices/ExceptionDispatchInfo.cs:line 63 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) in /_/src/System.Private.CoreLib/shared/System/Runtime/CompilerServices/TaskAwaiter.cs:line 180 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) in /_/src/System.Private.CoreLib/shared/System/Runtime/CompilerServices/TaskAwaiter.cs:line 151 at System.Runtime.CompilerServices.TaskAwaiter``1.GetResult() in /_/src/System.Private.CoreLib/shared/System/Runtime/CompilerServices/TaskAwaiter.cs:line 369 at McMaster.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args) at [Removed for privacy].Program.Main(String[] args) in D:\repos\[removed for privacy]\Program.cs:line 255

This bug appears to have been introduced in #369.

I think it has been fixed by 042f4a9

Hey,

first of all thank you for all the work that has been done to make this library available to us! :)

I had a similar issue with v3.1.0.
I installed 4.0.0-beta.74 and the problem is gone!

Thanks!

Thanks for confirming it works.