Skip to content

Required field in Flag not work as expected with Name with only one character #1253

@Gerrard-YNWA

Description

@Gerrard-YNWA

my urfave/cli version is

master branch

Checklist

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

Dependency Management

  • My project is using go modules.
  • My project is using vendoring.
  • My project is automatically downloading the latest version.
  • I am unsure of what my dependency management setup is.

Describe the bug

Setting Required field to true in Flags* will not worked while the Name field in flag only contains only one character.

To reproduce

package main

import (
	"fmt"
	"os"

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

func main() {
	app := cli.NewApp()
	app.Flags = []cli.Flag{
		&cli.StringFlag{Name: "c", Value: "config.yaml", Usage: "specify config file", Required: true},
	}
	app.Action = func(c *cli.Context) error {
		fmt.Println(c.String("c"))
		return nil
	}

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

Observed behavior

When specified the Name field to "c", and run program will exit normally with a config.yaml output. When I Changed Name field to "conf" program exit with help message and a Required flag "conf" not set output which is as expected.

It seems the checkRequiredFlags function in https://github.com/urfave/cli/blob/master/context.go#L256 filtered the key with length 1.

Expected behavior

I Want to know whether this behavior designed deliberately or not? If is, what's the purpose and can we clarify in the doc? If not, shall I correct this?

What I expect is Name field with only one character in flag should also work with checkRequiredFlags

Want to fix this yourself?

yes

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/v2relates to / is being considered for v2kind/bugdescribes or fixes a bugpinnedshould not be marked stalestatus/triagemaintainers still need to look into this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions