Skip to content

Commit 410ea2f

Browse files
committed
fix(completion): stop suggestions after -- (#77) (thanks @salmonumbrella)
1 parent dae8aa4 commit 410ea2f

4 files changed

Lines changed: 49 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- Auth: account manager upgrade respects managed services and skips Keep OAuth scopes. (#73) — thanks @salmonumbrella.
1010
- Classroom: normalize assignee updates + fix grade update masks. (#74) — thanks @salmonumbrella.
1111
- Classroom: scan pages when filtering coursework/materials by topic. (#73) — thanks @salmonumbrella.
12+
- CLI: enable shell completions and stop flag suggestions after `--`. (#77) — thanks @salmonumbrella.
1213

1314
### Build
1415

internal/cmd/completion_internal.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@ func completeWords(cword int, words []string) ([]string, error) {
4242
}
4343

4444
node := root
45+
terminatorIndex := -1
4546
for i := start; i < cword && i < len(words); {
4647
word := words[i]
4748
if word == "--" {
49+
terminatorIndex = i
4850
break
4951
}
5052
if strings.HasPrefix(word, "-") {
@@ -71,6 +73,14 @@ func completeWords(cword int, words []string) ([]string, error) {
7173
i++
7274
}
7375

76+
if terminatorIndex != -1 && cword >= terminatorIndex {
77+
return nil, nil
78+
}
79+
80+
if cword < len(words) && words[cword] == "--" {
81+
return nil, nil
82+
}
83+
7484
if cword > start && cword <= len(words) {
7585
prev := words[cword-1]
7686
if strings.HasPrefix(prev, "-") {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package cmd
2+
3+
import "testing"
4+
5+
func TestCompleteWordsStopsAfterTerminator(t *testing.T) {
6+
cases := []struct {
7+
name string
8+
cword int
9+
words []string
10+
}{
11+
{
12+
name: "current-is-terminator",
13+
cword: 1,
14+
words: []string{"gog", "--"},
15+
},
16+
{
17+
name: "after-terminator",
18+
cword: 3,
19+
words: []string{"gog", "auth", "--", "-"},
20+
},
21+
}
22+
23+
for _, tc := range cases {
24+
tc := tc
25+
t.Run(tc.name, func(t *testing.T) {
26+
got, err := completeWords(tc.cword, tc.words)
27+
if err != nil {
28+
t.Fatalf("completeWords: %v", err)
29+
}
30+
if len(got) != 0 {
31+
t.Fatalf("expected no suggestions, got %v", got)
32+
}
33+
})
34+
}
35+
}

internal/cmd/root.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const (
2424

2525
type RootFlags struct {
2626
Color string `help:"Color output: auto|always|never" default:"${color}"`
27-
Account string `help:"Account email for API commands (gmail/calendar/drive/docs/slides/contacts/tasks/people/sheets)"`
27+
Account string `help:"Account email for API commands (gmail/calendar/classroom/drive/docs/slides/contacts/tasks/people/sheets)"`
2828
JSON bool `help:"Output JSON to stdout (best for scripting)" default:"${json}"`
2929
Plain bool `help:"Output stable, parseable text to stdout (TSV; no colors)" default:"${plain}"`
3030
Force bool `help:"Skip confirmations for destructive commands"`
@@ -43,6 +43,7 @@ type CLI struct {
4343
Docs DocsCmd `cmd:"" help:"Google Docs (export via Drive)"`
4444
Slides SlidesCmd `cmd:"" help:"Google Slides"`
4545
Calendar CalendarCmd `cmd:"" help:"Google Calendar"`
46+
Classroom ClassroomCmd `cmd:"" help:"Google Classroom"`
4647
Gmail GmailCmd `cmd:"" aliases:"mail,email" help:"Gmail"`
4748
Contacts ContactsCmd `cmd:"" help:"Google Contacts"`
4849
Tasks TasksCmd `cmd:"" help:"Google Tasks"`
@@ -183,7 +184,7 @@ func newParser(description string) (*kong.Kong, *CLI, error) {
183184
}
184185

185186
func baseDescription() string {
186-
return "Google CLI for Gmail/Calendar/Drive/Contacts/Tasks/Sheets/Docs/Slides/People"
187+
return "Google CLI for Gmail/Calendar/Classroom/Drive/Contacts/Tasks/Sheets/Docs/Slides/People"
187188
}
188189

189190
func helpDescription() string {

0 commit comments

Comments
 (0)