Support aliases for flags (#409)

Aliases are currently only supported for sub-commands, but they're
useful for flags as well. E.g., when migrating from an old flag name
to a new flag name, while still supporting the old value.
This commit is contained in:
Prashant Varanasi
2024-02-26 16:10:15 -08:00
committed by GitHub
parent 30e84613fe
commit fa9b636997
5 changed files with 50 additions and 4 deletions
+12 -3
View File
@@ -684,15 +684,24 @@ func flipBoolValue(value reflect.Value) error {
func (c *Context) parseFlag(flags []*Flag, match string) (err error) {
candidates := []string{}
for _, flag := range flags {
long := "--" + flag.Name
short := "-" + string(flag.Short)
neg := "--no-" + flag.Name
matched := long == match
candidates = append(candidates, long)
if flag.Short != 0 {
short := "-" + string(flag.Short)
matched = matched || (short == match)
candidates = append(candidates, short)
}
if short != match && long != match && !(match == neg && flag.Tag.Negatable) {
for _, alias := range flag.Aliases {
alias = "--" + alias
matched = matched || (alias == match)
candidates = append(candidates, alias)
}
neg := "--no-" + flag.Name
if !matched && !(match == neg && flag.Tag.Negatable) {
continue
}
// Found a matching flag.