Skip to content

Commit

Permalink
add dynamic completion for compile -b command to list available board
Browse files Browse the repository at this point in the history
  • Loading branch information
umbynos committed Sep 3, 2021
1 parent 9bda0d5 commit 59f1a35
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions cli/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (

"github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/instance"
"github.com/arduino/arduino-cli/commands/board"
"github.com/arduino/arduino-cli/commands/compile"
"github.com/arduino/arduino-cli/commands/upload"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
Expand Down Expand Up @@ -84,6 +85,9 @@ func NewCommand() *cobra.Command {

command.Flags().StringVarP(&fqbn, "fqbn", "b", "", tr("Fully Qualified Board Name, e.g.: arduino:avr:uno"))
command.MarkFlagRequired("fqbn")
command.RegisterFlagCompletionFunc("fqbn", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return getBoards(toComplete), cobra.ShellCompDirectiveDefault
})
command.Flags().BoolVar(&showProperties, "show-properties", false, tr("Show all build properties used instead of compiling."))
command.Flags().BoolVar(&preprocess, "preprocess", false, tr("Print preprocessed code to stdout instead of compiling."))
command.Flags().StringVar(&buildCachePath, "build-cache-path", "", tr("Builds of 'core.a' are saved into this path to be cached and reused."))
Expand Down Expand Up @@ -274,3 +278,19 @@ func (r *compileResult) String() string {
// The output is already printed via os.Stdout/os.Stdin
return ""
}

func getBoards(toComplete string) []string {
// from listall.go TODO optimize
inst := instance.CreateAndInit()

list, _ := board.ListAll(context.Background(), &rpc.BoardListAllRequest{
Instance: inst,
SearchArgs: nil,
IncludeHiddenBoards: false,
})
var res []string
for _, i := range list.GetBoards() {
res = append(res, i.Fqbn)
}
return res
}

0 comments on commit 59f1a35

Please sign in to comment.