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: 4 comments 9 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.
-
|
The word "option" is a bit unfortunate for what it provides and how it is used in CLIs. And from that perspective the "by design" argument makes sense. But as others mention, the requested functionality is really useful in CLIs and is pretty widely adopted., I think the term "flag" is better than "option" here. Not sure if this helps the discussion, but relying too much on the semantic meaning of "option" is not the best argument imo. |
Beta Was this translation helpful? Give feedback.
-
|
We will be adding required options in the next release. |
Beta Was this translation helpful? Give feedback.
We will be adding required options in the next release.