Command-line-api: Custom validation of arguments

Created on 26 Mar 2019  路  4Comments  路  Source: dotnet/command-line-api

At which place do I implement custom validation for the CLI arguments? For example I got one argument that is not optional and has to be set, or I want to validate an connection string which is provided through the CLI.

Currently I'm creating and using a command handler like that:
rootCommand.Handler = CommandHandler.Create<ProgramArguments>(arguments =>

Hereby the CLI arguments are used to create the ProgramArguments class. But when I throw an Exception in the constructor of that class it's not handled anywhere.

It seems that it's also not possible to catch that exception when wrapping return rootCommand.InvokeAsync(args, new SystemConsole()).Result; in a try catch block.

needs documentation

Most helpful comment

Hi - I'd like to use a custom validator to enforce some restrictions on options, but am having trouble implementing it. Wondering if either of you could provide some sample code? One simple use case that I have involves options for supplying a username and password. These are optional, but if one is populated, both should be. Thanks in advance for any help you can provide!

All 4 comments

When you call Command.InvokeAsync the UseDefaults method is called internally, which includes UseExceptionHandler. This is why the exception is getting caught. I'd recommend against using exceptions for validation in any case.

You can validate during parsing by calling Argument.AddValidator. This API needs work but the current behavior is that if you return a string from the validator delegate, it signals an error and the returned string is used as the error message. If it's valid, you should return null. The SymbolResult passed to Command.Argument.AddValidator will allow you to inspect the OptionResult instances and, for example, enforce that options are required or mutually exclusive.

We want to improve this API and feedback is welcome.

Thanks for your detailed reply, @jonsequitur.
I agree that using exceptions to implement a validation are not a good practice. I just tried to figure out how your implementation was/is intended :D

Anyways I figured out to use Argument.AddValidator as you mentioned. It's a bit cumbersome but it seems to work. Maybe also found a bug, will take a look into this and open a separate issue if I'm right.

Did you think about to use the validation concept of System.ComponentModel.DataAnnotations?

The possibility of doing parser configuration, validation, etc. using attributes is being explored. The core System.CommandLine library is intended to be fairly unopinionated about these kinds of approaches but will contain the foundation for them, particularly in System.CommandLine.Binding.

Hi - I'd like to use a custom validator to enforce some restrictions on options, but am having trouble implementing it. Wondering if either of you could provide some sample code? One simple use case that I have involves options for supplying a username and password. These are optional, but if one is populated, both should be. Thanks in advance for any help you can provide!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DualBrain picture DualBrain  路  3Comments

KathleenDollard picture KathleenDollard  路  4Comments

jonsequitur picture jonsequitur  路  6Comments

adamsitnik picture adamsitnik  路  5Comments

divinebovine picture divinebovine  路  3Comments