Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use of obsolete v1 aliases goes undetected #1103

Open
3 tasks done
TomOnTime opened this issue Mar 31, 2020 · 8 comments
Open
3 tasks done

Use of obsolete v1 aliases goes undetected #1103

TomOnTime opened this issue Mar 31, 2020 · 8 comments
Assignees
Labels
area/v2 relates to / is being considered for v2 kind/bug describes or fixes a bug
Milestone

Comments

@TomOnTime
Copy link
Contributor

TomOnTime commented Mar 31, 2020

my urfave/cli version is

2.2.0

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.

Describe the bug

There is no warning if old-style v1 aliases are used. The alias simply doesn't exist.

To reproduce

Create a flag using the v1 method:

cli.StringFlag{
  Name: "config, cfg"
}

Observed behavior

The flag --config works; --cfg does not.

Expected behavior

The system should output an error or warning.

Additional context

Ideally if Name includes a space or a comma, the code should output an
message about "Old style alias being used. Please see docs/migrate-v1-to-v2.md"
then the program should exit.

Want to fix this yourself?

no

Run go version and paste its output here

go1.14.1

Run go env and paste its output here

GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/qw/qp8v2j353wz7q57_jymyxj0h0000gn/T/go-build854616664=/tmp/go-build -gno-record-gcc-switches -fno-common"
@TomOnTime TomOnTime added area/v2 relates to / is being considered for v2 kind/bug describes or fixes a bug status/triage maintainers still need to look into this labels Mar 31, 2020
@coilysiren
Copy link
Member

@TomOnTime can you add a title to the issue?

@TomOnTime TomOnTime changed the title v2 bug: ( your bug title goes here ) v2 bug: Use of obsolete v1 aliases goes undetected May 5, 2020
@stale
Copy link

stale bot commented Aug 3, 2020

This issue or PR has been automatically marked as stale because it has not had recent activity. Please add a comment bumping this if you're still interested in it's resolution! Thanks for your help, please let us know if you need anything else.

@stale stale bot added the status/stale stale due to the age of it's last update label Aug 3, 2020
@stale
Copy link

stale bot commented Sep 2, 2020

Closing this as it has become stale.

@stale stale bot closed this as completed Sep 2, 2020
@coilysiren coilysiren reopened this Jan 27, 2021
@stale
Copy link

stale bot commented Jan 27, 2021

This issue or PR has been bumped and is no longer marked as stale! Feel free to bump it again in the future, if it's still relevant.

@stale stale bot removed the status/stale stale due to the age of it's last update label Jan 27, 2021
@stale
Copy link

stale bot commented Jun 2, 2021

This issue or PR has been automatically marked as stale because it has not had recent activity. Please add a comment bumping this if you're still interested in it's resolution! Thanks for your help, please let us know if you need anything else.

@stale stale bot added the status/stale stale due to the age of it's last update label Jun 2, 2021
@stale
Copy link

stale bot commented Jul 4, 2021

Closing this as it has become stale.

@stale stale bot closed this as completed Jul 4, 2021
@meatballhat meatballhat reopened this Apr 22, 2022
@meatballhat meatballhat removed the status/stale stale due to the age of it's last update label Apr 22, 2022
@meatballhat meatballhat changed the title v2 bug: Use of obsolete v1 aliases goes undetected Use of obsolete v1 aliases goes undetected Apr 23, 2022
@meatballhat meatballhat added this to the Release 2.4.x milestone Apr 23, 2022
@meatballhat meatballhat self-assigned this Apr 24, 2022
@meatballhat
Copy link
Member

From what I can tell, at the time of v1 ➡️ v2 we made the call to silently trim right rather than break everyone's Name values 🙁

cli/flag.go

Lines 262 to 274 in 6033c00

func flagNames(name string, aliases []string) []string {
var ret []string
for _, part := range append([]string{name}, aliases...) {
// v1 -> v2 migration warning zone:
// Strip off anything after the first found comma or space, which
// *hopefully* makes it a tiny bit more obvious that unexpected behavior is
// caused by using the v1 form of stringly typed "Name".
ret = append(ret, commaWhitespace.ReplaceAllString(part, ""))
}
return ret
}

Given that we don't really have a compile-time or type-enforced way of preventing v1 style values, I think I'd like to incorporate some validation feedback as part of reworking Before/After/"lifecycle hooks" (see #1273).

@meatballhat meatballhat removed the status/triage maintainers still need to look into this label May 5, 2022
@wxiaoguang
Copy link
Contributor

wxiaoguang commented Aug 2, 2023

It's not that difficult but I think the framework should report such low-level mistakes to developers.

Without framework support, I have to do:

func reflectGet(v any, fieldName string) any {
	e := reflect.ValueOf(v).Elem()
	return e.FieldByName(fieldName).Interface()
}

// https://cli.urfave.org/migrate-v1-to-v2/#flag-aliases-are-done-differently
// Sadly v2 doesn't warn you if a comma is in the name. (https://github.com/urfave/cli/issues/1103)
func checkCommandFlags(c any) bool {
	var cmds []*cli.Command
	if app, ok := c.(*cli.App); ok {
		cmds = app.Commands
	} else {
		cmds = c.(*cli.Command).Subcommands
	}
	ok := true
	for _, cmd := range cmds {
		for _, flag := range cmd.Flags {
			flagName := reflectGet(flag, "Name").(string)
			if strings.Contains(flagName, ",") {
				ok = false
				log.Error("cli.Flag can't have comma in its Name: %q, use Aliases instead", flagName)
			}
		}
		if !checkCommandFlags(cmd) {
			ok = false
		}
	}
	return ok
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/v2 relates to / is being considered for v2 kind/bug describes or fixes a bug
Projects
None yet
Development

No branches or pull requests

4 participants