-
Notifications
You must be signed in to change notification settings - Fork 479
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
How to get Error message? #513
Comments
New guy here who is trying out library for first time! I also wanted to do the same thing. :) err.Tag is an ErrorType Flags enum. if(err.Tag.HasFlag(ErrorType.BadVerbSelectedError)) Unfortunately, it looks like you will have to enumerate every single possible ErrorType to handle all cases. Also, it doesn't let you get the exact error details. Only the error type. The library seems a bit lacking in this regard. Seems weird to have an Errors collection and not have more details in them. |
Damn, it was really harder than it should have been. Had to do this:
|
Nice approach that also worked for me, but I noticed that the error list is actually not needed inside the static method. Also, I think that an extension method would be cleaner in this case: public static ParserResult<T> ThrowOnParseError<T>(this ParserResult<T> result)
{
if (!(result is NotParsed<T>))
{
// Case with no errors needs to be detected explicitly, otherwise the .Select line will throw an InvalidCastException
return result;
}
var builder = SentenceBuilder.Create();
var errorMessages = HelpText.RenderParsingErrorsTextAsLines(result, builder.FormatError, builder.FormatMutuallyExclusiveSetErrors, 1);
var excList = errorMessages.Select(msg => new ArgumentException(msg)).ToList();
if (excList.Any())
{
throw new AggregateException(excList);
}
return result;
} That way, you can keep using the fluent API: Parser.Default.ParseArguments<Args>(args)
.WithParsed(result => Console.WriteLine(result.Name))
.ThrowOnParseError()
.Etc() |
The [Pr #634) exposes the Errors to the base class, available in develop branch and will be released in v2.9. var result = Parser.Default.ParseArguments<Options>(args);
var errors= result.Errors; |
My snippet:
I would like to throw errors as ArgumentException with message being the same message that gets printed when error occurs. For example, when option -n is missing, this text is printed:
I would like to throw ArgumentException with "Required option 'n, name' is missing." being its message.
I looked into HelpText, but im not sure if it's what i need.
The text was updated successfully, but these errors were encountered: