Skip to content

InitDefaultHelpFlag is not i18n awareΒ #2359

@decentral1se

Description

@decentral1se

Follows #2301.

I have a bit of a situation where translation of long flags is desired but short flags is not.

If you have something like this in your CLI:

	rootCmd.PersistentFlags().BoolVarP(
		&internal.Help,
		i18n.G("help"),
		"h",  // not translated
		false,
		i18n.G("help for myCli"),
	)

And you translate "help" to "ayuda". Then in InitDefaultHelpFlag πŸ‘‡

cobra/command.go

Line 1221 in 61968e8

if c.Flags().Lookup(helpFlagName) == nil {

helpFlagName is no longer "help" and the code attempts to set c.Flags().BoolP(helpFlagName, "h", false, usage). However, -h is already set and we see an explosion stacktrace:

panic: unable to redefine 'h' shorthand in "myCli" flagset: it's already used for "ayuda" flag

This is quite an edge-case but just another symptom of cobra not supporting i18n natively.

Just wanted to report that in.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions