normalize flag names so that underscores are interchangeable with hyphens

This commit is contained in:
Kovid Goyal 2022-08-17 15:46:15 +05:30
parent 2d466f343d
commit 2ca8ae8e5f
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 5 additions and 2 deletions

View File

@ -30,7 +30,7 @@ class Option:
short = flags[0][1:]
long = flags[-1][2:]
if not long:
raise SystemExit(f'No long flag for {x} with flags {flags}')
raise TypeError(f'No long flag for {x} with flags {flags}')
self.short, self.long = short, long
self.usage = serialize_as_go_string(x['help'].strip())
self.type = x['type']
@ -70,7 +70,7 @@ class Option:
return f'cli.ChoicesP({base}, "{self.long}", "{self.short}", "{self.usage}", {cx})'
return f'cli.Choices({base}, "{self.long}", "{self.usage}", {cx})'
else:
raise KeyError(f'Unknown type of CLI option: {self.type}')
raise TypeError(f'Unknown type of CLI option: {self.type} for {self.long}')
def build_go_code(name: str, cmd: RemoteCommand, seq: OptionSpecSeq, template: str) -> str:

View File

@ -402,6 +402,9 @@ func Init(root *cobra.Command) {
root.SetHelpFunc(show_help)
root.SetHelpCommand(&cobra.Command{Hidden: true})
root.CompletionOptions.DisableDefaultCmd = true
root.SetGlobalNormalizationFunc(func(fs *pflag.FlagSet, name string) pflag.NormalizedName {
return pflag.NormalizedName(strings.ReplaceAll(name, "_", "-"))
})
}
func Execute(root *cobra.Command) error {