Skip to content

Commit 9e2225a

Browse files
committed
factor out the programmer flag handling
1 parent 4f47c26 commit 9e2225a

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

cli/arguments/port.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ type Fqbn struct {
4646
fqbn string
4747
}
4848

49+
// Programmer contains the arguments result.
50+
// This is useful so all flags used by commands that need
51+
// this information are consistent with each other.
52+
type Programmer struct {
53+
programmer string
54+
}
55+
4956
// AddToCommand adds the flags used to set port and protocol to the specified Command
5057
func (p *Port) AddToCommand(cmd *cobra.Command) {
5158
cmd.Flags().StringVarP(&p.address, "port", "p", "", tr("Upload port address, e.g.: COM3 or /dev/ttyACM2"))
@@ -67,6 +74,14 @@ func (f *Fqbn) AddToCommand(cmd *cobra.Command) {
6774
})
6875
}
6976

77+
// AddToCommand adds the flags used to set the programmer to the specified Command
78+
func (p *Programmer) AddToCommand(cmd *cobra.Command) {
79+
cmd.Flags().StringVarP(&p.programmer, "programmer", "P", "", tr("Use the specified programmer to upload."))
80+
cmd.RegisterFlagCompletionFunc("programmer", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
81+
return GetInstalledProgrammers(), cobra.ShellCompDirectiveDefault
82+
})
83+
}
84+
7085
// GetFQBN returns the fqbn
7186
func (f *Fqbn) GetFQBN() string {
7287
return f.fqbn
@@ -77,6 +92,11 @@ func (f *Fqbn) SetFQBN(fqbn string) {
7792
f.fqbn = fqbn
7893
}
7994

95+
// GetProgrammer returns the programmer
96+
func (p *Programmer) GetProgrammer() string {
97+
return p.programmer
98+
}
99+
80100
// GetPortAddressAndProtocol returns only the port address and the port protocol
81101
// without any other port metadata obtained from the discoveries. This method allows
82102
// to bypass the discoveries unless the protocol is not specified: in this

cli/burnbootloader/burnbootloader.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ var (
3434
port arguments.Port
3535
verbose bool
3636
verify bool
37-
programmer string
37+
programmer arguments.Programmer
3838
dryRun bool
3939
tr = i18n.Tr
4040
)
@@ -52,12 +52,9 @@ func NewCommand() *cobra.Command {
5252

5353
fqbn.AddToCommand(burnBootloaderCommand)
5454
port.AddToCommand(burnBootloaderCommand)
55+
programmer.AddToCommand(burnBootloaderCommand)
5556
burnBootloaderCommand.Flags().BoolVarP(&verify, "verify", "t", false, tr("Verify uploaded binary after the upload."))
5657
burnBootloaderCommand.Flags().BoolVarP(&verbose, "verbose", "v", false, tr("Turns on verbose mode."))
57-
burnBootloaderCommand.Flags().StringVarP(&programmer, "programmer", "P", "", tr("Use the specified programmer to upload."))
58-
burnBootloaderCommand.RegisterFlagCompletionFunc("programmer", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
59-
return arguments.GetInstalledProgrammers(), cobra.ShellCompDirectiveDefault
60-
})
6158
burnBootloaderCommand.Flags().BoolVar(&dryRun, "dry-run", false, tr("Do not perform the actual upload, just log out actions"))
6259
burnBootloaderCommand.Flags().MarkHidden("dry-run")
6360

@@ -80,7 +77,7 @@ func run(command *cobra.Command, args []string) {
8077
Port: discoveryPort.ToRPC(),
8178
Verbose: verbose,
8279
Verify: verify,
83-
Programmer: programmer,
80+
Programmer: programmer.GetProgrammer(),
8481
DryRun: dryRun,
8582
}, os.Stdout, os.Stderr); err != nil {
8683
feedback.Errorf(tr("Error during Upload: %v"), err)

0 commit comments

Comments
 (0)