question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Option attribute based upon enum does not allow to override validation behaviour

See original GitHub issue

Maybe not a bug, but I have this enum for an option, and when provided with an invalid enum, I do see the standard error message. However, I want to be able to provide or:

  • my custom validation message
  • ShowHelp()

Please note that in my case I just run CommandLineApplication.Execute<GeneratorCommand>(args); so I have to access to the CommandLineApplication instance to subscribe to ValidationErrorHandler

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
stefandevocommented, Jul 6, 2018

Great! Thanks for this quick feedback!

0reactions
natemcmastercommented, Jul 6, 2018

Not from inside the parsers. There is no application in the context of IValueParser.Parse. The expected way to implement this API is to parse or throw FormatException.

If you want to call ShowHelp, I’d recommend catching CommandParsingException at the top level. Give this a try:

var app = new CommandLineApplication<GeneratorCommand>();
app.Conventions.UseDefaultConventions();
try
{
   app.Execute(args);
}
catch (CommandParsingException ex)
{
  Console.Error.WriteLine(ex.Message);
  app.ShowHelp();
}

As you can see in the implementation, CommandLineApplication.Execute<T>(args) doesn’t do much more than this. https://github.com/natemcmaster/CommandLineUtils/blob/ecfc8bc3992f9ee899115622dcb851d0274f3557/src/CommandLineUtils/CommandLineApplication.Execute.cs#L51-L64

Read more comments on GitHub >

github_iconTop Results From Across the Web

Make validation fail for a certain enum value with asp.net mvc
Enums are basically integers so you can use the Range attribute to validate ... return new ValidationResult("This value is not allowed"); ...
Read more >
TypeScript string enums, and when and how to use them
No matter how much you know about TypeScript enums, this guide covers all the best practices for using string-based enums in production.
Read more >
enum — Support for enumerations
An enumeration with the values STRICT , CONFORM , EJECT , and KEEP which allows for more fine-grained control over how invalid values...
Read more >
How to write custom converters for JSON serialization - .NET
To override the default behavior of a built-in converter. For example, you might want DateTime values to be represented by mm/dd/yyyy format ...
Read more >
Define Enum Fields and Suggested Values in the UI
Learn how to define enums and suggested values for string fields in the Experience Platform user interface.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found