Skip to content

Commit e1d2c0b

Browse files
committed
factor out the fqbn flag handling
1 parent e394b76 commit e1d2c0b

File tree

2 files changed

+48
-8
lines changed

2 files changed

+48
-8
lines changed

cli/arguments/fqbn.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to license@arduino.cc.
15+
16+
package arguments
17+
18+
import "github.com/spf13/cobra"
19+
20+
// Fqbn contains the fqbn flag data.
21+
// This is useful so all flags used by commands that need
22+
// this information are consistent with each other.
23+
type Fqbn struct {
24+
fqbn string
25+
}
26+
27+
// AddToCommand adds the flags used to set fqbn to the specified Command
28+
func (f *Fqbn) AddToCommand(cmd *cobra.Command) {
29+
cmd.Flags().StringVarP(&f.fqbn, "fqbn", "b", "", tr("Fully Qualified Board Name, e.g.: arduino:avr:uno"))
30+
cmd.RegisterFlagCompletionFunc("fqbn", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
31+
return GetInstalledBoards(), cobra.ShellCompDirectiveDefault
32+
})
33+
}
34+
35+
// String returns the fqbn
36+
func (f *Fqbn) String() string {
37+
return f.fqbn
38+
}
39+
40+
// Set sets the fqbn
41+
func (f *Fqbn) Set(fqbn string) {
42+
f.fqbn = fqbn
43+
}

cli/board/details.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ import (
3333

3434
var (
3535
showFullDetails bool
36-
fqbn string
3736
listProgrammers bool
37+
fqbn arguments.Fqbn
3838
)
3939

4040
func initDetailsCommand() *cobra.Command {
@@ -47,11 +47,8 @@ func initDetailsCommand() *cobra.Command {
4747
Run: runDetailsCommand,
4848
}
4949

50+
fqbn.AddToCommand(detailsCommand)
5051
detailsCommand.Flags().BoolVarP(&showFullDetails, "full", "f", false, tr("Show full board details"))
51-
detailsCommand.Flags().StringVarP(&fqbn, "fqbn", "b", "", tr("Fully Qualified Board Name, e.g.: arduino:avr:uno"))
52-
detailsCommand.RegisterFlagCompletionFunc("fqbn", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
53-
return arguments.GetInstalledBoards(), cobra.ShellCompDirectiveDefault
54-
})
5552
detailsCommand.Flags().BoolVarP(&listProgrammers, "list-programmers", "", false, tr("Show list of available programmers"))
5653
// detailsCommand.MarkFlagRequired("fqbn") // enable once `board details <fqbn>` is removed
5754

@@ -62,13 +59,13 @@ func runDetailsCommand(cmd *cobra.Command, args []string) {
6259
inst := instance.CreateAndInit()
6360

6461
// remove once `board details <fqbn>` is removed
65-
if fqbn == "" && len(args) > 0 {
66-
fqbn = args[0]
62+
if fqbn.String() == "" && len(args) > 0 {
63+
fqbn.Set(args[0])
6764
}
6865

6966
res, err := board.Details(context.Background(), &rpc.BoardDetailsRequest{
7067
Instance: inst,
71-
Fqbn: fqbn,
68+
Fqbn: fqbn.String(),
7269
})
7370

7471
if err != nil {

0 commit comments

Comments
 (0)