-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat: add framework flag aliases and unified IM pagination #2146
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e32d7cb
feat: add framework flag aliases and unified IM pagination
liangshuo-1 20f71c0
fix: align alias attribution and pagination contracts
liangshuo-1 8353309
fix: align alias contracts and documentation
liangshuo-1 ccfa4b8
test: remove environment-dependent contact bot e2e
liangshuo-1 ae480ed
test: restore contact bot e2e
liangshuo-1 a33b091
docs: reduce IM pagination guidance noise
liangshuo-1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| // Copyright (c) 2026 Lark Technologies Pte. Ltd. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package flagalias | ||
|
|
||
| import ( | ||
| "errors" | ||
|
|
||
| "github.com/spf13/pflag" | ||
| ) | ||
|
|
||
| // InvalidValueAttribution identifies the canonical flag and the long-form | ||
| // spelling that supplied a value which pflag could not convert. Names do not | ||
| // include leading dashes. | ||
| type InvalidValueAttribution struct { | ||
| Canonical string | ||
| Source string | ||
| } | ||
|
|
||
| // InvalidValueAttributionOf resolves a typed pflag conversion error for a flag | ||
| // managed by Bind. Ordinary pflags return ok=false so installing aliases does | ||
| // not broaden the root error contract for unrelated commands. | ||
| // | ||
| // pflag's InvalidValueError does not report whether a canonical flag with a | ||
| // shorthand was supplied as -x or --long. That ambiguous case also returns | ||
| // ok=false; an alias spelling remains unambiguous because Bind records it | ||
| // before value conversion. | ||
| func InvalidValueAttributionOf(err error) (InvalidValueAttribution, bool) { | ||
| var invalidValue *pflag.InvalidValueError | ||
| if !errors.As(err, &invalidValue) || invalidValue == nil { | ||
| return InvalidValueAttribution{}, false | ||
| } | ||
| flag := invalidValue.GetFlag() | ||
| if flag == nil || len(Aliases(flag)) == 0 { | ||
| return InvalidValueAttribution{}, false | ||
| } | ||
|
|
||
| canonical := flag.Name | ||
| source := Source(flag) | ||
| if source == "" || (source == canonical && flag.Shorthand != "") { | ||
| return InvalidValueAttribution{}, false | ||
| } | ||
| return InvalidValueAttribution{Canonical: canonical, Source: source}, true | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| // Copyright (c) 2026 Lark Technologies Pte. Ltd. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package flagalias | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| func TestInvalidValueAttributionOf(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| args []string | ||
| managed bool | ||
| shorthand bool | ||
| wrap bool | ||
| want InvalidValueAttribution | ||
| wantOK bool | ||
| }{ | ||
| { | ||
| name: "alias separate value", | ||
| args: []string{"--page-size", "bad"}, | ||
| managed: true, | ||
| want: InvalidValueAttribution{Canonical: "limit", Source: "page-size"}, | ||
| wantOK: true, | ||
| }, | ||
| { | ||
| name: "alias equals value", | ||
| args: []string{"--page-size=bad"}, | ||
| managed: true, | ||
| want: InvalidValueAttribution{Canonical: "limit", Source: "page-size"}, | ||
| wantOK: true, | ||
| }, | ||
| { | ||
| name: "canonical separate value", | ||
| args: []string{"--limit", "bad"}, | ||
| managed: true, | ||
| want: InvalidValueAttribution{Canonical: "limit", Source: "limit"}, | ||
| wantOK: true, | ||
| }, | ||
| { | ||
| name: "canonical equals value", | ||
| args: []string{"--limit=bad"}, | ||
| managed: true, | ||
| want: InvalidValueAttribution{Canonical: "limit", Source: "limit"}, | ||
| wantOK: true, | ||
| }, | ||
| { | ||
| name: "alias fails after canonical", | ||
| args: []string{"--limit=10", "--page-size=bad"}, | ||
| managed: true, | ||
| want: InvalidValueAttribution{Canonical: "limit", Source: "page-size"}, | ||
| wantOK: true, | ||
| }, | ||
| { | ||
| name: "canonical fails after alias", | ||
| args: []string{"--page-size=10", "--limit=bad"}, | ||
| managed: true, | ||
| want: InvalidValueAttribution{Canonical: "limit", Source: "limit"}, | ||
| wantOK: true, | ||
| }, | ||
| { | ||
| name: "ordinary flag", | ||
| args: []string{"--limit=bad"}, | ||
| wantOK: false, | ||
| }, | ||
| { | ||
| name: "shorthand source is ambiguous", | ||
| args: []string{"-l", "bad"}, | ||
| managed: true, | ||
| shorthand: true, | ||
| wantOK: false, | ||
| }, | ||
| { | ||
| name: "alias remains exact when canonical has shorthand", | ||
| args: []string{"--page-size=bad"}, | ||
| managed: true, | ||
| shorthand: true, | ||
| want: InvalidValueAttribution{Canonical: "limit", Source: "page-size"}, | ||
| wantOK: true, | ||
| }, | ||
| { | ||
| name: "wrapped pflag error", | ||
| args: []string{"--page-size=bad"}, | ||
| managed: true, | ||
| wrap: true, | ||
| want: InvalidValueAttribution{Canonical: "limit", Source: "page-size"}, | ||
| wantOK: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, test := range tests { | ||
| t.Run(test.name, func(t *testing.T) { | ||
| cmd := &cobra.Command{Use: "demo"} | ||
| if test.shorthand { | ||
| cmd.Flags().IntP("limit", "l", 10, "") | ||
| } else { | ||
| cmd.Flags().Int("limit", 10, "") | ||
| } | ||
| if test.managed { | ||
| if err := Bind(cmd, []Spec{{Canonical: "limit", Aliases: []string{"page-size"}}}); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| } | ||
| parseErr := cmd.ParseFlags(test.args) | ||
| if parseErr == nil { | ||
| t.Fatal("ParseFlags() succeeded, want invalid integer error") | ||
| } | ||
| if test.wrap { | ||
| parseErr = fmt.Errorf("parse flags: %w", parseErr) | ||
| } | ||
|
|
||
| got, ok := InvalidValueAttributionOf(parseErr) | ||
| if ok != test.wantOK || got != test.want { | ||
| t.Fatalf("InvalidValueAttributionOf() = (%+v, %v), want (%+v, %v)", got, ok, test.want, test.wantOK) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestInvalidValueAttributionOfRejectsOtherErrors(t *testing.T) { | ||
| if got, ok := InvalidValueAttributionOf(errors.New("flag needs an argument: --limit")); ok { | ||
| t.Fatalf("InvalidValueAttributionOf() = (%+v, true), want no attribution", got) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.