Skip to content

Commit 856868e

Browse files
committed
add completion also for -p or --port flag
1 parent 14d7e3d commit 856868e

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

cli/arguments/completion.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,20 @@ func GetInstallableLibs(toComplete string) []string {
160160
}
161161
return res
162162
}
163+
164+
// GetConnectedBoards is an helper function useful to autocomplete.
165+
// It returns a list of boards which are currently connected
166+
// Obviously it does not suggests network ports because of the timeout
167+
func GetConnectedBoards(toComplete string) []string {
168+
inst := instance.CreateAndInit() // TODO optimize this: it does not make sense to create an instance everytime
169+
170+
list, _ := board.List(&rpc.BoardListRequest{
171+
Instance: inst,
172+
})
173+
var res []string
174+
// transform the data structure for the completion
175+
for _, i := range list {
176+
res = append(res, i.Port.Address)
177+
}
178+
return res
179+
}

cli/arguments/port.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ type Port struct {
4242
// AddToCommand adds the flags used to set port and protocol to the specified Command
4343
func (p *Port) AddToCommand(cmd *cobra.Command) {
4444
cmd.Flags().StringVarP(&p.address, "port", "p", "", tr("Upload port address, e.g.: COM3 or /dev/ttyACM2"))
45+
cmd.RegisterFlagCompletionFunc("port", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
46+
return GetConnectedBoards(toComplete), cobra.ShellCompDirectiveDefault
47+
})
4548
cmd.Flags().StringVarP(&p.protocol, "protocol", "l", "", tr("Upload port protocol, e.g: serial"))
4649
cmd.RegisterFlagCompletionFunc("protocol", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
4750
return GetInstalledProtocols(toComplete), cobra.ShellCompDirectiveDefault

i18n/data/en.po

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,7 +1418,7 @@ msgstr "Low memory available, stability problems may occur."
14181418
msgid "Maintainer: %s"
14191419
msgstr "Maintainer: %s"
14201420

1421-
#: cli/arguments/port.go:49
1421+
#: cli/arguments/port.go:52
14221422
msgid "Max time to wait for port discovery, e.g.: 30s, 1m"
14231423
msgstr "Max time to wait for port discovery, e.g.: 30s, 1m"
14241424

@@ -2201,7 +2201,7 @@ msgstr "Upload port address, e.g.: COM3 or /dev/ttyACM2"
22012201
msgid "Upload port found on %s"
22022202
msgstr "Upload port found on %s"
22032203

2204-
#: cli/arguments/port.go:45
2204+
#: cli/arguments/port.go:48
22052205
msgid "Upload port protocol, e.g: serial"
22062206
msgstr "Upload port protocol, e.g: serial"
22072207

@@ -3031,7 +3031,7 @@ msgstr "pluggable discovery already added: %s"
30313031
msgid "port"
30323032
msgstr "port"
30333033

3034-
#: cli/arguments/port.go:125
3034+
#: cli/arguments/port.go:128
30353035
msgid "port not found: %[1]s %[2]s"
30363036
msgstr "port not found: %[1]s %[2]s"
30373037

i18n/rice-box.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)