Skip to content

Commit 47821ae

Browse files
committed
refactor function that checks for flags conflicts
1 parent d1a41f8 commit 47821ae

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

cli/arguments/arguments.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,25 @@
1515

1616
package arguments
1717

18-
import "github.com/arduino/arduino-cli/i18n"
18+
import (
19+
"os"
20+
"strings"
21+
22+
"github.com/arduino/arduino-cli/cli/errorcodes"
23+
"github.com/arduino/arduino-cli/cli/feedback"
24+
"github.com/arduino/arduino-cli/i18n"
25+
"github.com/spf13/cobra"
26+
)
1927

2028
var tr = i18n.Tr
29+
30+
// CheckFlagsConflicts is a helper function useful to report errors when more than one conflicting flag is used
31+
func CheckFlagsConflicts(command *cobra.Command, flagNames ...string) {
32+
for _, flagName := range flagNames {
33+
if !command.Flag(flagName).Changed {
34+
return
35+
}
36+
}
37+
feedback.Errorf(tr("Can't use %s flags at the same time.", "--"+strings.Join(flagNames, " "+tr("and")+" --")))
38+
os.Exit(errorcodes.ErrBadArgument)
39+
}

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

test/test_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def test_init_dest_flag_with_overwrite_flag(run_command, working_dir):
144144
def test_init_dest_and_config_file_flags(run_command, working_dir):
145145
result = run_command(["config", "init", "--dest-file", "some_other_path", "--dest-dir", "some_path"])
146146
assert result.failed
147-
assert "Can't use both --dest-file and --dest-dir flags at the same time." in result.stderr
147+
assert "Can't use --dest-file and --dest-dir flags at the same time." in result.stderr
148148

149149

150150
def test_init_config_file_flag_absolute_path(run_command, working_dir):

0 commit comments

Comments
 (0)