Skip to content
Discussion options

You must be logged in to vote

The root command's Action handles running the root command itself. When you select show conf, the handler that runs is that subcommand's Action; the parent Action functions aren't called along the way.

For this range check, put the validation on your root IntFlag:

&cli.IntFlag{
    Name:    "debug",
    Aliases: []string{"D"},
    Value:   0,
    Action: func(ctx context.Context, cmd *cli.Command, value int) error {
        if value < 0 || value > 5 {
            return fmt.Errorf("--debug must be in the range 0..5")
        }
        return nil
    },
},

This uses context and fmt from the standard library. An error returned by the flag action stops execution before the selected command's A…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by stevewi-niteflyte
Comment options

You must be logged in to vote
0 replies
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