urfave/cli argument placement prevents flag parsing

Viewed 39

I'm using urfave/cli to build a golang cli app. Below is an exact copy of one of the examples from the tutorial here.

func main() {
    app := &cli.App{
        Flags: []cli.Flag{
            &cli.StringFlag{
                Name:  "lang",
                Value: "english",
                Usage: "language for the greeting",
            },
        },
        Action: func(cCtx *cli.Context) error {
            name := "Nefertiti"
            if cCtx.NArg() > 0 {
                name = cCtx.Args().Get(0)
            }
            if cCtx.String("lang") == "spanish" {
                fmt.Println("Hola", name)
            } else {
                fmt.Println("Hello", name)
            }
            return nil
        },
    }

    if err := app.Run(os.Args); err != nil {
        log.Fatal(err)
    }
}

Running this code with the arguments at the end works fine and I get a spanish greeting with the argument I specified at the end. testrun -lang spanish aaa

If I put the argument first, none of the flags are parsed (flag defaults to english) and everything is read as an argument. testrun aaa -lang spanish

Is there no way to put arguments first before flags and still have the flags parsed?

0 Answers
Related