Skip to content

Create a CommandOption that is required #1568

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

You must be logged in to vote

Sorry, I misunderstood. All options are optional by default. If you want to enforce an option and make the option required, the best way is to override the Validate(CommandContext context, TSettings settings) on either the setting itself or the command.

public class MyCommand : Command<MyCommand.Settings>
{
    public sealed class Settings : CommandSettings
    {
        [CommandOption("-p|--projectId")]
        public string? ProjectId { get; set; }

        // You can validate the settings directly
        public override Validate(CommandContext context) 
        {
            if (ProjectID == null) {
                return ValidationResult.Error("Project ID id required");
            }

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@MrHinsh
Comment options

@patriksvensson
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
2 participants