-
-
Notifications
You must be signed in to change notification settings - Fork 257
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
Consider renaming the API ThrowOnUnexpectedArgument #88
Comments
Can you provide repro steps? I have a guess, but it's hard to tell without knowing what is being passed in as |
Im calling Below are some relevant code snippets: private void ConfigureCommand(CommandLineApplication command, IServiceCollection services)
{
_services = services;
command.Description = Description;
command.HelpOption("-?|-h|--help");
ConfigureCommandInternal(command);
command.OnExecute(() => OnExecute(command));
} protected override void ConfigureCommandInternal(CommandLineApplication command)
{
// Register sub-commands
RegisterSubCommand<SubCommand1>(command);
RegisterSubCommand<SubCommand2>(command);
RegisterSubCommand<SubCommand3>(command);
RegisterSubCommand<SubCommand4>(command);
} public CommandLineApplication RegisterSubCommand<TCommand>(TCommand com, CommandLineApplication root) where TCommand : CommandBase
{
return root.Command(com.Name.ToLower(), c => com.ConfigureCommand(c, _services));
}
public CommandLineApplication RegisterSubCommand<TCommand>(CommandLineApplication root) where TCommand : CommandBase, new()
{
return RegisterSubCommand(new TCommand(), root);
} |
Try changing this: public CommandLineApplication RegisterSubCommand<TCommand>(TCommand com, CommandLineApplication root) where TCommand : CommandBase
{
- return root.Command(com.Name.ToLower(), c => com.ConfigureCommand(c, _services));
+ return root.Command(com.Name.ToLower(),
+ c => com.ConfigureCommand(c, _services),
+ throwOnUnexpectedArg: false);
} |
This comment has been minimized.
This comment has been minimized.
Thanks, @natemcmaster, that was indeed the issue. However I think there might be another bug: If I use throwOnUnexpectedArg: false. Then My OnValidationError Handler never get's called, and the app returns 0 rather than non-zero. I ended up just catching the ComandParsingException myself in order to write a useful message, eat the error, and return -1. |
What kind of validators have you configured? The app should return By the way, each subcommand is a new instance of CommandLineApplication, each with its own ValidationErrorHandler. Are you invoking a subcommand, but expecting the validation error handler on the root CommandLineApplication object to be called? |
I see. Thanks for your help. I guess the issue I have with this feature is that it doesn't exit execution onValidationError. |
I think there is probably a bug here somewhere, I'm just not sure what it is. So far, I've identified at least two things that are not clear about the behavior of this API
Does this describe your issues so far? I'm not sure what you meant by the most recent comment about "it doesn't exit execution onValidationError." |
Yes those two describe issues I've seen so far. Here's what I mean by it doesn't exit execution onValidationError. If I set throwOnUnexpectedArgs to false for the root and all sub-commands, and I specify an OnValidation() handler for each command, and my handler returns 3 (non-zero), then I would expect the app to exit after the onValidation handler is executed and the args are found to be invalid. Instead it proceeds to execute my app. For example. If I call To give you some context, this issue started when I tried to stop bad arguments from printing stack traces. I want bad arguments to fail the execution, but not print out exception stack traces. |
Oh, I see. Ok, so maybe this explanation will help. What ThrowOnUnexpectedArgs:false means to the parser is "parse until you hit an unrecognized argument or option. Then, but everything else into the CommandLineApplication.RemainingArguments property". For example:
This behavior was added before this library every had support for validation on arguments. I realize now why this behavior isn't obvious. It's probably time to reconsider what the "throwOnUnexpectedArgs" value means. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Please comment if you believe this should remain open, otherwise it will be closed in 7 days. |
Stale bot closed this, but I'd like to revisit this in 3.0. Reopening |
I am trying to make my app not throw an exception if the user enters an invalid command/option/argument.
I set ThrowOnUnexpectedArgument to false in the constructor of the CommandLineApplication. I also set a ValidationErrorHandler function.
However, an exception is still thrown.
The text was updated successfully, but these errors were encountered: