Skip to content

Commit be3f4a5

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

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Diff for: cli/compile/compile.go

+20
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232

3333
"github.com/arduino/arduino-cli/cli/errorcodes"
3434
"github.com/arduino/arduino-cli/cli/instance"
35+
"github.com/arduino/arduino-cli/commands/board"
3536
"github.com/arduino/arduino-cli/commands/compile"
3637
"github.com/arduino/arduino-cli/commands/upload"
3738
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
@@ -84,6 +85,9 @@ func NewCommand() *cobra.Command {
8485

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

0 commit comments

Comments
 (0)