Skip to content

How do I make required options? #538

Answered by patriksvensson
koliyo asked this question in Q&A
Discussion options

You must be logged in to vote

@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();
    }
}

Replies: 2 comments 5 replies

Comment options

You must be logged in to vote
1 reply
@koliyo
Comment options

Comment options

You must be logged in to vote
4 replies
@koliyo
Comment options

@rhnatiuk
Comment options

@patriksvensson
Comment options

@lucasshiva
Comment options

Answer selected by patriksvensson
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
5 participants