BeforeExecute() & Parameter Validation
The CommandBase
class provides several methods that can be overridden. Check the class documentation for more information. One of the most important methods is BeforeExecute
.
In the BeforeExecute
method you can check if there is an unresolved property and/or you can provide dynamic default values.
The HashSet<string> unresolvedProperties
contains all annotated properties which have not been resolved. For example, imagine, you have defined am optional Option called "EndDate"
. If this Option is not provided in the command-line the Property DateTime EndDate
would be: DateTime.DefautlValue
, and this is probably not what you wanted.
You can catch this in the BeforeExecute
method and check if any Property (Contains( nameof(ps.EndDate))
!!) ****is unresolved - has not got a value from command-line - neither explicitly provided nor static default value:
Error Handling
Use the errors.AddError()
method to collect errors message. Commander
will check for any error before it executes the Command and terminates the application gracefully, by printing out all error messages you collected. Collecting error messages is much more convenient the throwing Exceptions, because an Exception normally stop after the first error occurred.
Last updated