Skip to content

Commit f6acb3c

Browse files
committed
add dynamic completion for compile -b command to list available board
1 parent d4d77e4 commit f6acb3c

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

cli/compile/compile.go

+20
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131

3232
"github.com/arduino/arduino-cli/cli/errorcodes"
3333
"github.com/arduino/arduino-cli/cli/instance"
34+
"github.com/arduino/arduino-cli/commands/board"
3435
"github.com/arduino/arduino-cli/commands/compile"
3536
"github.com/arduino/arduino-cli/commands/upload"
3637
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
@@ -83,6 +84,9 @@ func NewCommand() *cobra.Command {
8384

8485
command.Flags().StringVarP(&fqbn, "fqbn", "b", "", tr("Fully Qualified Board Name, e.g.: arduino:avr:uno"))
8586
command.MarkFlagRequired("fqbn")
87+
command.RegisterFlagCompletionFunc("fqbn", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
88+
return getBoards(toComplete), cobra.ShellCompDirectiveDefault
89+
})
8690
command.Flags().BoolVar(&showProperties, "show-properties", false, tr("Show all build properties used instead of compiling."))
8791
command.Flags().BoolVar(&preprocess, "preprocess", false, tr("Print preprocessed code to stdout instead of compiling."))
8892
command.Flags().StringVar(&buildCachePath, "build-cache-path", "", tr("Builds of 'core.a' are saved into this path to be cached and reused."))
@@ -273,3 +277,19 @@ func (r *compileResult) String() string {
273277
// The output is already printed via os.Stdout/os.Stdin
274278
return ""
275279
}
280+
281+
func getBoards(toComplete string) []string {
282+
// from listall.go TODO optimize
283+
inst := instance.CreateAndInit()
284+
285+
list, _ := board.ListAll(context.Background(), &rpc.BoardListAllRequest{
286+
Instance: inst,
287+
SearchArgs: nil,
288+
IncludeHiddenBoards: false,
289+
})
290+
var res []string
291+
for _, i := range list.GetBoards() {
292+
res = append(res, i.Fqbn)
293+
}
294+
return res
295+
}

0 commit comments

Comments
 (0)