How do I make required options? #538
-
I need some "options" to be required. This is different from the provided example in #217 Which does provide an example of an option with a required value, but the option itself is not required. How do I make a command line "option" required? By this I mean that if the option is not provided by the user then this is a validation error. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
You could set a default value on the option and then validate that the value is not the default. But, if the option is not optional, I personally would rather use an argument and denote that as required, by using the angle-bracket-syntax like so: [CommandArgument(0, "<firstName>")]
public string FirstName { get; set; } |
Beta Was this translation helpful? Give feedback.
-
@koliyo Options is by its nature never required (therefore the name). You could add a custom validation step to your command such as: public sealed class MySettings : CommandSettings
{
[CommandOption("--foo <VALUE>")]
public string Foo { get; set; }
public override ValidationResult Validate()
{
if (string.IsNullOrWhiteSpace(Foo))
{
return ValidationResult.Error("Foo is required");
}
return base.Validate();
}
} |
Beta Was this translation helpful? Give feedback.
@koliyo Options is by its nature never required (therefore the name). You could add a custom validation step to your command such as: