Skip to content

Commit d307325

Browse files
committed
error: fix flagname for shorthandonly
1 parent d3369c5 commit d307325

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

.github/workflows/go.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
- name: Set up Go
2323
uses: actions/setup-go@v2
2424
with:
25-
go-version: 1.19
25+
go-version: '1.25.1'
2626

2727
- name: Build
2828
run: go build -v ./...

errors.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const (
1414
flagNoSuchFlagMessage
1515
flagUnknownFlagMessage
1616
flagUnknownShorthandFlagMessage
17+
flagUnknownShorthandFlagMessageNonPosix
1718
)
1819

1920
// NotExistError is the error returned when trying to access a flag that
@@ -42,6 +43,9 @@ func (e *NotExistError) Error() string {
4243
case flagUnknownShorthandFlagMessage:
4344
c := rune(e.name[0])
4445
return fmt.Sprintf("unknown shorthand flag: %q in -%s", c, e.specifiedShorthands)
46+
47+
case flagUnknownShorthandFlagMessageNonPosix:
48+
return fmt.Sprintf("unknown shorthand flag: -%s", e.name)
4549
}
4650

4751
panic(fmt.Errorf("unknown flagNotExistErrorMessageType: %v", e.messageType))
@@ -109,7 +113,14 @@ func (e *InvalidValueError) Error() string {
109113
flag := e.flag
110114
var flagName string
111115
if flag.Shorthand != "" && flag.ShorthandDeprecated == "" {
112-
flagName = fmt.Sprintf("-%s, --%s", flag.Shorthand, flag.Name)
116+
flagName = fmt.Sprintf("-%s", flag.Shorthand)
117+
if flag.Mode != ShorthandOnly {
118+
if flag.Mode != NameAsShorthand {
119+
flagName = fmt.Sprintf("%s, --%s", flagName, flag.Name)
120+
} else {
121+
flagName = fmt.Sprintf("%s, -%s", flagName, flag.Name)
122+
}
123+
}
113124
} else {
114125
flagName = fmt.Sprintf("--%s", flag.Name)
115126
}

flag.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,9 +1200,8 @@ func (f *FlagSet) parseSingleShortArg(shorthands string, args []string, fn parse
12001200
} else {
12011201
// custom carapace-sh/carapace-pflag message for non-posix shorthand (chain disabled)
12021202
err = f.fail(&NotExistError{
1203-
name: name,
1204-
// specifiedShorthands: shorthands,
1205-
messageType: flagUnknownShorthandFlagMessage,
1203+
name: name,
1204+
messageType: flagUnknownShorthandFlagMessageNonPosix,
12061205
})
12071206
}
12081207
return

0 commit comments

Comments
 (0)