urfave/cli

[v3] `Required` does not work with `Persistent`

zx8 opened this issue · 2 comments

zx8 commented

Info:

  • Using v3.0.0-alpha6
  • Works if Required is set to false

Code

package main

import (
    "context"
    "fmt"
    "log"
    "os"

    "github.com/urfave/cli/v3"
)

func main() {
    cmd := &cli.Command{
        Name: "example",
        Flags: []cli.Flag{
            &cli.StringFlag{
                Name:       "foo",
                Persistent: true,
                Required:   true,
            },
            &cli.StringFlag{
                Name:       "bar",
                Persistent: true,
                Required:   true,
            },
        },
        Commands: []*cli.Command{
            {
                Name: "a",
                Commands: []*cli.Command{
                    {
                        Name: "b",
                        Action: func(ctx context.Context, c *cli.Command) error {
                            fmt.Println(c.String("foo"), c.String("bar"))
                            return nil
                        },
                    },
                },
            },
        },
    }

    if err := cmd.Run(context.Background(), os.Args); err != nil {
        log.Fatal(err)
    }
}

Expected

$ go run . --foo=foo a --bar=bar b
foo bar

Actual

$ go run . --foo=foo a --bar=bar b
NAME:
   example - A new cli application

USAGE:
   example [command [command options]] [arguments...]

COMMANDS:
   a
   help, h  Shows a list of commands or help for one command

OPTIONS:
   --foo value
   --bar value
   --help, -h   show help (default: false)
2023/12/08 13:45:41 Required flag "bar" not set
exit status 1

@zx8 Try the PR fix

zx8 commented

@dearchap Confirmed the linked PR fixes the issue. 👍