Skip to content

Commit e20ddab

Browse files
committed
refactor function that checks for flags conflicts
1 parent 9a4d50e commit e20ddab

File tree

3 files changed

+32
-20
lines changed

3 files changed

+32
-20
lines changed

cli/arguments/port.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ package arguments
1818
import (
1919
"fmt"
2020
"net/url"
21+
"os"
2122
"time"
2223

2324
"github.com/arduino/arduino-cli/arduino/discovery"
2425
"github.com/arduino/arduino-cli/arduino/sketch"
26+
"github.com/arduino/arduino-cli/cli/errorcodes"
2527
"github.com/arduino/arduino-cli/cli/feedback"
2628
"github.com/arduino/arduino-cli/commands"
2729
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
@@ -189,3 +191,13 @@ func (p *Port) GetPort(instance *rpc.Instance, sk *sketch.Sketch) (*discovery.Po
189191
}
190192
}
191193
}
194+
195+
// CheckFlagsConflicts is a helper function useful to report errors when 2 conflicting flags are used
196+
func CheckFlagsConflicts(command *cobra.Command, firstFlagName, secondFlagName string) {
197+
flag1, _ := command.Flags().GetString(firstFlagName)
198+
flag2, _ := command.Flags().GetString(secondFlagName)
199+
if flag1 != "" && flag2 != "" {
200+
feedback.Errorf(tr("Can't use both --%s and --%s flags at the same time.", firstFlagName, secondFlagName))
201+
os.Exit(errorcodes.ErrBadArgument)
202+
}
203+
}

cli/config/init.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package config
1818
import (
1919
"os"
2020

21+
"github.com/arduino/arduino-cli/cli/arguments"
2122
"github.com/arduino/arduino-cli/cli/errorcodes"
2223
"github.com/arduino/arduino-cli/cli/feedback"
2324
"github.com/arduino/arduino-cli/configuration"
@@ -46,7 +47,10 @@ func initInitCommand() *cobra.Command {
4647
" " + os.Args[0] + " config init --dest-dir /home/user/MyDirectory\n" +
4748
" " + os.Args[0] + " config init --dest-file /home/user/MyDirectory/my_settings.yaml",
4849
Args: cobra.NoArgs,
49-
Run: runInitCommand,
50+
PreRun: func(cmd *cobra.Command, args []string) {
51+
arguments.CheckFlagsConflicts(cmd, "dest-file", "dest-dir")
52+
},
53+
Run: runInitCommand,
5054
}
5155
initCommand.Flags().StringVar(&destDir, "dest-dir", "", tr("Sets where to save the configuration file."))
5256
initCommand.Flags().StringVar(&destFile, "dest-file", "", tr("Sets where to save the configuration file."))
@@ -55,10 +59,6 @@ func initInitCommand() *cobra.Command {
5559
}
5660

5761
func runInitCommand(cmd *cobra.Command, args []string) {
58-
if destFile != "" && destDir != "" {
59-
feedback.Errorf(tr("Can't use both --dest-file and --dest-dir flags at the same time."))
60-
os.Exit(errorcodes.ErrGeneric)
61-
}
6262

6363
var configFileAbsPath *paths.Path
6464
var absPath *paths.Path

i18n/data/en.po

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,9 @@ msgstr "Can't open sketch"
272272
msgid "Can't set multiple values in key %v"
273273
msgstr "Can't set multiple values in key %v"
274274

275-
#: cli/config/init.go:59
276-
msgid "Can't use both --dest-file and --dest-dir flags at the same time."
277-
msgstr "Can't use both --dest-file and --dest-dir flags at the same time."
275+
#: cli/arguments/port.go:200
276+
msgid "Can't use both --%s and --%s flags at the same time."
277+
msgstr "Can't use both --%s and --%s flags at the same time."
278278

279279
#: cli/config/add.go:59
280280
#: cli/config/delete.go:70
@@ -475,7 +475,7 @@ msgstr "Create a new Sketch"
475475
msgid "Creates a zip file containing all sketch files."
476476
msgstr "Creates a zip file containing all sketch files."
477477

478-
#: cli/config/init.go:42
478+
#: cli/config/init.go:43
479479
msgid "Creates or updates the configuration file in the data directory or custom directory with the current configuration settings."
480480
msgstr "Creates or updates the configuration file in the data directory or custom directory with the current configuration settings."
481481

@@ -1086,7 +1086,7 @@ msgstr "Force run of post-install scripts (if the CLI is not running interactive
10861086
msgid "Force skip of post-install scripts (if the CLI is running interactively)."
10871087
msgstr "Force skip of post-install scripts (if the CLI is running interactively)."
10881088

1089-
#: cli/arguments/port.go:71
1089+
#: cli/arguments/port.go:73
10901090
#: cli/debug/debug.go:60
10911091
#: cli/monitor/monitor.go:63
10921092
#: cli/upload/upload.go:56
@@ -1437,7 +1437,7 @@ msgstr "Low memory available, stability problems may occur."
14371437
msgid "Maintainer: %s"
14381438
msgstr "Maintainer: %s"
14391439

1440-
#: cli/arguments/port.go:66
1440+
#: cli/arguments/port.go:68
14411441
msgid "Max time to wait for port discovery, e.g.: 30s, 1m"
14421442
msgstr "Max time to wait for port discovery, e.g.: 30s, 1m"
14431443

@@ -1628,7 +1628,7 @@ msgstr "OutputRate in Null monitor must be a float64"
16281628
msgid "Override a build property with a custom value. Can be used multiple times for multiple properties."
16291629
msgstr "Override a build property with a custom value. Can be used multiple times for multiple properties."
16301630

1631-
#: cli/config/init.go:53
1631+
#: cli/config/init.go:57
16321632
msgid "Overwrite existing config file."
16331633
msgstr "Overwrite existing config file."
16341634

@@ -1869,8 +1869,8 @@ msgstr "Server responded with: %s"
18691869
msgid "Sets a setting value."
18701870
msgstr "Sets a setting value."
18711871

1872-
#: cli/config/init.go:51
1873-
#: cli/config/init.go:52
1872+
#: cli/config/init.go:55
1873+
#: cli/config/init.go:56
18741874
msgid "Sets where to save the configuration file."
18751875
msgstr "Sets where to save the configuration file."
18761876

@@ -2251,15 +2251,15 @@ msgstr "Upload Arduino sketches."
22512251
msgid "Upload Arduino sketches. This does NOT compile the sketch prior to upload."
22522252
msgstr "Upload Arduino sketches. This does NOT compile the sketch prior to upload."
22532253

2254-
#: cli/arguments/port.go:58
2254+
#: cli/arguments/port.go:60
22552255
msgid "Upload port address, e.g.: COM3 or /dev/ttyACM2"
22562256
msgstr "Upload port address, e.g.: COM3 or /dev/ttyACM2"
22572257

22582258
#: commands/upload/upload.go:409
22592259
msgid "Upload port found on %s"
22602260
msgstr "Upload port found on %s"
22612261

2262-
#: cli/arguments/port.go:62
2262+
#: cli/arguments/port.go:64
22632263
msgid "Upload port protocol, e.g: serial"
22642264
msgstr "Upload port protocol, e.g: serial"
22652265

@@ -2288,7 +2288,7 @@ msgstr "Usage:"
22882288
msgid "Use %s for more information about a command."
22892289
msgstr "Use %s for more information about a command."
22902290

2291-
#: cli/arguments/port.go:79
2291+
#: cli/arguments/port.go:81
22922292
msgid "Use the specified programmer to upload."
22932293
msgstr "Use the specified programmer to upload."
22942294

@@ -2401,11 +2401,11 @@ msgstr "Website: %s"
24012401
msgid "When specified, VID/PID specific build properties are used, if board supports them."
24022402
msgstr "When specified, VID/PID specific build properties are used, if board supports them."
24032403

2404-
#: cli/config/init.go:41
2404+
#: cli/config/init.go:42
24052405
msgid "Writes current configuration to a configuration file."
24062406
msgstr "Writes current configuration to a configuration file."
24072407

2408-
#: cli/config/init.go:44
2408+
#: cli/config/init.go:45
24092409
msgid "Writes current configuration to the configuration file in the data directory."
24102410
msgstr "Writes current configuration to the configuration file in the data directory."
24112411

@@ -3105,7 +3105,7 @@ msgstr "pluggable discovery already added: %s"
31053105
msgid "port"
31063106
msgstr "port"
31073107

3108-
#: cli/arguments/port.go:188
3108+
#: cli/arguments/port.go:190
31093109
msgid "port not found: %[1]s %[2]s"
31103110
msgstr "port not found: %[1]s %[2]s"
31113111

0 commit comments

Comments
 (0)