Skip to content

Commit e36f6aa

Browse files
committed
Unified argument management. Converted login flags and board attach flags into arguments.
1 parent 2e77b6a commit e36f6aa

File tree

21 files changed

+145
-193
lines changed

21 files changed

+145
-193
lines changed

commands/board/attach.go

Lines changed: 71 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@
3030
package board
3131

3232
import (
33+
"fmt"
3334
"net/url"
3435
"os"
36+
"regexp"
3537
"time"
3638

3739
discovery "github.com/arduino/board-discovery"
@@ -44,42 +46,32 @@ import (
4446
"github.com/spf13/cobra"
4547
)
4648

49+
var validSerialBoardURIRegexp = regexp.MustCompile("(serial|tty)://.+")
50+
var validNetworkBoardURIRegexp = regexp.MustCompile("(http(s)?|(tc|ud)p)://[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}:[0-9]{1,5}")
51+
4752
func init() {
4853
command.AddCommand(attachCommand)
49-
attachCommand.Flags().StringVar(&attachFlags.boardURI, "board", "", "The URI of the board to connect.")
5054
attachCommand.Flags().StringVar(&attachFlags.boardFlavour, "flavour", "default", "The Name of the CPU flavour, it is required for some boards (e.g. Arduino Nano).")
51-
attachCommand.Flags().StringVar(&attachFlags.sketchName, "sketch", "", "The Name of the sketch to attach the board to.")
5255
attachCommand.Flags().StringVar(&attachFlags.searchTimeout, "timeout", "5s", "The timeout of the search of connected devices, try to high it if your board is not found (e.g. to 10s).")
5356
}
5457

5558
var attachFlags struct {
56-
boardURI string // The URI of the board to attach: can be serial:// tty:// http:// https:// tcp:// udp:// referring to the validBoardURIRegexp variable.
5759
boardFlavour string // The flavour of the chipset of the cpu of the connected board, if not specified it is set to "default".
58-
sketchName string // The name of the sketch to attach to the board.
59-
fromPath string // The Path of the file to import and attach to the board.
6060
searchTimeout string // Expressed in a parsable duration, is the timeout for the list and attach commands.
6161
}
6262

6363
var attachCommand = &cobra.Command{
64-
Use: "attach --sketch=[SKETCH-NAME] --board=[BOARD]",
65-
Short: "Attaches a board to a sketch.",
66-
Long: "Attaches a board to a sketch.",
67-
Example: "" +
68-
"arduino board attach --board serial:///dev/tty/ACM0 \\\n" +
69-
" --sketch sketchName # Attaches a sketch to a board.",
70-
Run: runAttachCommand,
64+
Use: "attach sketchName boardURI",
65+
Short: "Attaches a sketch to a board.",
66+
Long: "Attaches a sketch to a board. Provide sketch name and a board URI to connect.",
67+
Example: "arduino board attach sketchName serial:///dev/tty/ACM0",
68+
Args: cobra.ExactArgs(2),
69+
Run: runAttachCommand,
7170
}
7271

7372
func runAttachCommand(cmd *cobra.Command, args []string) {
74-
if attachFlags.sketchName == "" {
75-
formatter.PrintErrorMessage("No sketch name provided.")
76-
os.Exit(commands.ErrBadCall)
77-
}
78-
79-
if attachFlags.boardURI == "" {
80-
formatter.PrintErrorMessage("No board URI provided.")
81-
os.Exit(commands.ErrBadCall)
82-
}
73+
sketchName := args[0]
74+
boardURI := args[1]
8375

8476
duration, err := time.ParseDuration(attachFlags.searchTimeout)
8577
if err != nil {
@@ -112,13 +104,13 @@ func runAttachCommand(cmd *cobra.Command, args []string) {
112104

113105
ss := sketches.Find(homeFolder)
114106

115-
sketch, exists := ss[attachFlags.sketchName]
107+
sketch, exists := ss[sketchName]
116108
if !exists {
117109
formatter.PrintErrorMessage("Cannot find specified sketch in the Sketchbook.")
118110
os.Exit(commands.ErrGeneric)
119111
}
120112

121-
deviceURI, err := url.Parse(attachFlags.boardURI)
113+
deviceURI, err := url.Parse(boardURI)
122114
if err != nil {
123115
formatter.PrintError(err, "The provided Device URL is not in a valid format.")
124116
os.Exit(commands.ErrBadCall)
@@ -127,17 +119,18 @@ func runAttachCommand(cmd *cobra.Command, args []string) {
127119
var findBoardFunc func(boards.Boards, *discovery.Monitor, *url.URL) *boards.Board
128120
var Type string
129121

130-
if validSerialBoardURIRegexp.Match([]byte(attachFlags.boardURI)) {
122+
if validSerialBoardURIRegexp.Match([]byte(boardURI)) {
131123
findBoardFunc = findSerialConnectedBoard
132124
Type = "serial"
133-
} else if validNetworkBoardURIRegexp.Match([]byte(attachFlags.boardURI)) {
125+
} else if validNetworkBoardURIRegexp.Match([]byte(boardURI)) {
134126
findBoardFunc = findNetworkConnectedBoard
135127
Type = "network"
136128
} else {
137129
formatter.PrintErrorMessage("Invalid device port type provided. Accepted types are: serial://, tty://, http://, https://, tcp://, udp://.")
138130
os.Exit(commands.ErrBadCall)
139131
}
140132

133+
// TODO: Handle the case when no board is found.
141134
board := findBoardFunc(bs, monitor, deviceURI)
142135

143136
sketch.Metadata.CPU = sketches.MetadataCPU{
@@ -151,3 +144,56 @@ func runAttachCommand(cmd *cobra.Command, args []string) {
151144
}
152145
formatter.PrintResult("BOARD ATTACHED.")
153146
}
147+
148+
// findSerialConnectedBoard find the board which is connected to the specified URI via serial port, using a monitor and a set of Boards
149+
// for the matching.
150+
func findSerialConnectedBoard(bs boards.Boards, monitor *discovery.Monitor, deviceURI *url.URL) *boards.Board {
151+
found := false
152+
location := deviceURI.Path
153+
var serialDevice discovery.SerialDevice
154+
for _, device := range monitor.Serial() {
155+
if device.Port == location {
156+
// Found the device !
157+
found = true
158+
serialDevice = *device
159+
}
160+
}
161+
if !found {
162+
formatter.PrintErrorMessage("No Supported board has been found at the specified board URI.")
163+
return nil
164+
}
165+
166+
board := bs.ByVidPid(serialDevice.VendorID, serialDevice.ProductID)
167+
if board == nil {
168+
formatter.PrintErrorMessage("No Supported board has been found, try either install new cores or check your board URI.")
169+
os.Exit(commands.ErrGeneric)
170+
}
171+
172+
formatter.Print("SUPPORTED BOARD FOUND:")
173+
formatter.Print(board.String())
174+
return board
175+
}
176+
177+
// findNetworkConnectedBoard find the board which is connected to the specified URI on the network, using a monitor and a set of Boards
178+
// for the matching.
179+
func findNetworkConnectedBoard(bs boards.Boards, monitor *discovery.Monitor, deviceURI *url.URL) *boards.Board {
180+
found := false
181+
182+
var networkDevice discovery.NetworkDevice
183+
184+
for _, device := range monitor.Network() {
185+
if device.Address == deviceURI.Host &&
186+
fmt.Sprint(device.Port) == deviceURI.Port() {
187+
// Found the device !
188+
found = true
189+
networkDevice = *device
190+
}
191+
}
192+
if !found {
193+
formatter.PrintErrorMessage("No Supported board has been found at the specified board URI, try either install new cores or check your board URI.")
194+
os.Exit(commands.ErrGeneric)
195+
}
196+
197+
formatter.Print("SUPPORTED BOARD FOUND:")
198+
return bs.ByID(networkDevice.Name)
199+
}

commands/board/list.go

Lines changed: 6 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ package board
22

33
import (
44
"fmt"
5-
"net/url"
65
"os"
7-
"regexp"
86
"time"
97

108
"github.com/bcmi-labs/arduino-cli/commands"
@@ -30,21 +28,17 @@ var listFlags struct {
3028
}
3129

3230
var listCommand = &cobra.Command{
33-
Use: "list",
34-
Run: runListCommand,
31+
Use: "list",
32+
Short: "List connected boards.",
33+
Long: "Detects and displays a list of connected boards to the current computer.",
34+
Example: "arduino board list --timeout 10s",
35+
Args: cobra.NoArgs,
36+
Run: runListCommand,
3537
}
3638

37-
var validSerialBoardURIRegexp = regexp.MustCompile("(serial|tty)://.+")
38-
var validNetworkBoardURIRegexp = regexp.MustCompile("(http(s)?|(tc|ud)p)://[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}:[0-9]{1,5}")
39-
4039
// runListCommand detects and lists the connected arduino boards
4140
// (either via serial or network ports).
4241
func runListCommand(cmd *cobra.Command, args []string) {
43-
if len(args) > 0 {
44-
formatter.PrintErrorMessage("Not accepting additional arguments.")
45-
os.Exit(commands.ErrBadCall)
46-
}
47-
4842
packageFolder, err := common.GetDefaultPkgFolder()
4943
if err != nil {
5044
formatter.PrintError(err, "Cannot Parse Board Index file.")
@@ -95,56 +89,3 @@ func runListCommand(cmd *cobra.Command, args []string) {
9589
//monitor.Stop() //If called will slow like 1sec the program to close after print, with the same result (tested).
9690
// it closes ungracefully, but at the end of the command we can't have races.
9791
}
98-
99-
// findSerialConnectedBoard find the board which is connected to the specified URI via serial port, using a monitor and a set of Boards
100-
// for the matching.
101-
func findSerialConnectedBoard(bs boards.Boards, monitor *discovery.Monitor, deviceURI *url.URL) *boards.Board {
102-
found := false
103-
location := deviceURI.Path
104-
var serialDevice discovery.SerialDevice
105-
for _, device := range monitor.Serial() {
106-
if device.Port == location {
107-
// Found the device !
108-
found = true
109-
serialDevice = *device
110-
}
111-
}
112-
if !found {
113-
formatter.PrintErrorMessage("No Supported board has been found at the specified board URI.")
114-
return nil
115-
}
116-
117-
board := bs.ByVidPid(serialDevice.VendorID, serialDevice.ProductID)
118-
if board == nil {
119-
formatter.PrintErrorMessage("No Supported board has been found, try either install new cores or check your board URI.")
120-
os.Exit(commands.ErrGeneric)
121-
}
122-
123-
formatter.Print("SUPPORTED BOARD FOUND:")
124-
formatter.Print(board.String())
125-
return board
126-
}
127-
128-
// findNetworkConnectedBoard find the board which is connected to the specified URI on the network, using a monitor and a set of Boards
129-
// for the matching.
130-
func findNetworkConnectedBoard(bs boards.Boards, monitor *discovery.Monitor, deviceURI *url.URL) *boards.Board {
131-
found := false
132-
133-
var networkDevice discovery.NetworkDevice
134-
135-
for _, device := range monitor.Network() {
136-
if device.Address == deviceURI.Host &&
137-
fmt.Sprint(device.Port) == deviceURI.Port() {
138-
// Found the device !
139-
found = true
140-
networkDevice = *device
141-
}
142-
}
143-
if !found {
144-
formatter.PrintErrorMessage("No Supported board has been found at the specified board URI, try either install new cores or check your board URI.")
145-
os.Exit(commands.ErrGeneric)
146-
}
147-
148-
formatter.Print("SUPPORTED BOARD FOUND:")
149-
return bs.ByID(networkDevice.Name)
150-
}

commands/compile/compile.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ var command = &cobra.Command{
8383
func run(cmd *cobra.Command, args []string) {
8484
logrus.Info("Executing `arduino compile`")
8585
isCorrectSyntax := true
86+
// TODO: Check if sketch exists.
8687
sketchName := args[0]
8788
var packageName string
8889
if flags.fullyQualifiedBoardName == "" {

commands/config/config.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@
3030
package config
3131

3232
import (
33-
"github.com/sirupsen/logrus"
34-
35-
"github.com/bcmi-labs/arduino-cli/common/formatter"
3633
"github.com/spf13/cobra"
3734
)
3835

@@ -45,15 +42,4 @@ var command = &cobra.Command{
4542
Use: "config",
4643
Short: "Arduino Configuration Commands.",
4744
Example: "arduino config init",
48-
Run: run,
49-
}
50-
51-
func run(cmd *cobra.Command, args []string) {
52-
logrus.Info("Executing `arduino config`")
53-
formatter.PrintErrorMessage("No subcommand specified.")
54-
if formatter.IsCurrentFormat("text") {
55-
logrus.Warn("Showing help message")
56-
cmd.Help()
57-
}
58-
logrus.Info("Done")
5945
}

commands/config/init.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ var initCommand = &cobra.Command{
5757
Example: "" +
5858
"arduino config init # Creates a config file by asking questions to the user into the default location.\n" +
5959
"arduino config init --default # Creates a config file with default configuration into default location.",
60-
Run: runInitCommand,
60+
Args: cobra.NoArgs,
61+
Run: runInitCommand,
6162
}
6263

6364
func runInitCommand(cmd *cobra.Command, args []string) {
@@ -90,6 +91,6 @@ func runInitCommand(cmd *cobra.Command, args []string) {
9091
// It does not have much sense to use it in JSON formatting, though.
9192
func configsFromQuestions() configs.Configs {
9293
ret := configs.Default()
93-
//Set of questions here
94+
// Set of questions here.
9495
return ret
9596
}

commands/core/download.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,16 @@ var downloadCommand = &cobra.Command{
4949
Use: "download [PACKAGER:ARCH[=VERSION]](S)",
5050
Short: "Downloads one or more cores and corresponding tool dependencies.",
5151
Long: "Downloads one or more cores and corresponding tool dependencies.",
52-
Run: runDownloadCommand,
5352
Example: "" +
5453
"arduino core download arduino:samd # to download the latest version of arduino SAMD core.\n" +
5554
"arduino core download arduino:samd=1.6.9 # for a specific version (in this case 1.6.9).",
55+
Args: cobra.MinimumNArgs(1),
56+
Run: runDownloadCommand,
5657
}
5758

5859
func runDownloadCommand(cmd *cobra.Command, args []string) {
5960
logrus.Info("Executing `arduino core download`")
6061

61-
if len(args) < 1 {
62-
formatter.PrintErrorMessage("No core specified for download command.")
63-
os.Exit(commands.ErrBadCall)
64-
}
65-
6662
logrus.Info("Getting packages status context")
6763
status, err := getPackagesStatusContext()
6864
if err != nil {

commands/core/install.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,16 @@ var installCommand = &cobra.Command{
5151
Use: "install [PACKAGER:ARCH[=VERSION]](S)",
5252
Short: "Installs one or more cores and corresponding tool dependencies.",
5353
Long: "Installs one or more cores and corresponding tool dependencies.",
54-
Run: runInstallCommand,
5554
Example: "" +
5655
"arduino core install arduino:samd # to download the latest version of arduino SAMD core.\n" +
5756
"arduino core install arduino:samd=1.6.9 # for a specific version (in this case 1.6.9).",
57+
Args: cobra.MinimumNArgs(1),
58+
Run: runInstallCommand,
5859
}
5960

6061
func runInstallCommand(cmd *cobra.Command, args []string) {
6162
logrus.Info("Executing `arduino core download`")
6263

63-
if len(args) < 1 {
64-
formatter.PrintErrorMessage("No core specified for download command.")
65-
os.Exit(commands.ErrBadCall)
66-
}
67-
6864
logrus.Info("Getting packages status context")
6965
status, err := getPackagesStatusContext()
7066
if err != nil {

commands/core/list.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ var listCommand = &cobra.Command{
4949
Short: "Shows the list of installed cores.",
5050
Long: "Shows the list of installed cores.\n" +
5151
"With -v tag (up to 2 times) can provide more verbose output.",
52-
Run: runListCommand,
5352
Example: "arduino core list -v # for a medium verbosity level.",
53+
Args: cobra.NoArgs,
54+
Run: runListCommand,
5455
}
5556

5657
func runListCommand(cmd *cobra.Command, args []string) {

commands/core/version.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ var versionCommand = &cobra.Command{
4242
Use: "version",
4343
Short: "Shows version number of arduino core package.",
4444
Long: `Shows version number of arduino core package which is installed on your system.`,
45-
Run: version.Command.Run,
4645
Example: version.Command.Example,
46+
Args: version.Command.Args,
47+
Run: version.Command.Run,
4748
}

commands/lib/download.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,15 @@ var downloadCommand = &cobra.Command{
4949
Use: "download [LIBRARY_NAME(S)]",
5050
Short: "Downloads one or more libraries without installing them.",
5151
Long: "Downloads one or more libraries without installing them.",
52-
Run: runDownloadCommand,
5352
Example: "" +
5453
"arduino lib download YoutubeApi # for the latest version.\n" +
5554
"arduino lib download YoutubeApi@1.0.0 # for a specific version (in this case 1.0.0).",
55+
Args: cobra.MinimumNArgs(1),
56+
Run: runDownloadCommand,
5657
}
5758

5859
func runDownloadCommand(cmd *cobra.Command, args []string) {
5960
logrus.Info("Executing `arduino lib download`")
60-
if len(args) < 1 {
61-
formatter.PrintErrorMessage("No library specified for download command.")
62-
os.Exit(commands.ErrBadCall)
63-
}
6461

6562
logrus.Info("Getting Libraries status context")
6663
status, err := getLibStatusContext()

0 commit comments

Comments
 (0)