Skip to content

v3: can't reuse command with flags #2007

@qwerty287

Description

@qwerty287

Checklist

  • Are you running the latest v3 release? The list of releases is here.
  • Did you check the manual for your release? The v3 manual is here.
  • Did you perform a search about this feature? Here's the GitHub guide about searching.

Since a few alpha versions (started at least with alpha9.1), it seems to be no longer possible to reuse a command if you have a default on a flag. If you set the flag value once, it will be set to all following executions as well, even though it should use the default value if the flag is not specified.

To reproduce, check out this:

package main

import (
	"fmt"
	"log"
	"context"

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

func main() {
	cmd := &cli.Command{
		Flags: []cli.Flag{
			&cli.StringFlag{
				Name:  "lang",
				Value: "english",
				Usage: "language for the greeting",
			},
		},
		Action: func(ctx context.Context, cmd *cli.Command) error {
			fmt.Println(cmd.String("lang"))
			return nil
		},
	}

        // should output "y"
	if err := cmd.Run(context.Background(), []string{"x", "--lang", "y"}); err != nil {
		log.Fatal(err)
	}
	
        // should output "english", but outputs "y"
	if err := cmd.Run(context.Background(), []string{"x"}); err != nil {
		log.Fatal(err)
	}
}

This might sound like a really special usecase, but we reuse commands in our testing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/v3relates to / is being considered for v3status/triagemaintainers still need to look into this

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions