@@ -12,6 +12,12 @@ import (
12
12
"golang.org/x/exp/slices"
13
13
)
14
14
15
+ var binariesSupportedPrograms = []string {
16
+ search .ProgramCe ,
17
+ search .ProgramEe ,
18
+ search .ProgramTt ,
19
+ }
20
+
15
21
// NewBinariesCmd creates binaries command.
16
22
func NewBinariesCmd () * cobra.Command {
17
23
var binariesCmd = & cobra.Command {
@@ -42,6 +48,7 @@ You will need to choose version using arrow keys in your console.
42
48
internalSwitchModule , args )
43
49
util .HandleCmdErr (cmd , err )
44
50
},
51
+ Args : cobra .MatchAll (cobra .MaximumNArgs (2 ), binariesSwitchValidateArgs ),
45
52
}
46
53
var listCmd = & cobra.Command {
47
54
Use : "list" ,
@@ -57,44 +64,42 @@ You will need to choose version using arrow keys in your console.
57
64
return binariesCmd
58
65
}
59
66
67
+ // binariesSwitchValidateArgs validates non-flag arguments of 'binaries switch' command.
68
+ func binariesSwitchValidateArgs (cmd * cobra.Command , args []string ) error {
69
+ if len (args ) > 0 {
70
+ if ! slices .Contains (binariesSupportedPrograms , args [0 ]) {
71
+ return fmt .Errorf ("not supported program: %s" , args [0 ])
72
+ }
73
+ }
74
+ return nil
75
+ }
76
+
60
77
// internalSwitchModule is a switch module.
61
78
func internalSwitchModule (cmdCtx * cmdcontext.CmdCtx , args []string ) error {
62
79
if ! isConfigExist (cmdCtx ) {
63
80
return errNoConfig
64
81
}
65
82
var switchCtx binary.SwitchCtx
66
- supportedPrograms := []string {search .ProgramCe , search .ProgramEe , search .ProgramTt }
67
-
68
83
var err error
69
- switch len (args ) {
70
- case 2 :
71
- switchCtx .Version = args [1 ]
72
- switchCtx .ProgramName = args [0 ]
73
- if ! slices .Contains (supportedPrograms , switchCtx .ProgramName ) {
74
- return fmt .Errorf ("not supported program: %s" , switchCtx .ProgramName )
75
- }
76
- case 1 :
84
+
85
+ if len (args ) > 0 {
77
86
switchCtx .ProgramName = args [0 ]
78
- if ! slices .Contains (supportedPrograms , switchCtx .ProgramName ) {
79
- return fmt .Errorf ("not supported program: %s" , switchCtx .ProgramName )
80
- }
81
- switchCtx .Version , err = binary .ChooseVersion (cliOpts .Env .BinDir , switchCtx .ProgramName )
82
- if err != nil {
83
- return err
84
- }
85
- case 0 :
86
- switchCtx .ProgramName , err = binary .ChooseProgram (supportedPrograms )
87
+ } else {
88
+ switchCtx .ProgramName , err = binary .ChooseProgram (binariesSupportedPrograms )
87
89
if err != nil {
88
90
return err
89
91
}
92
+ }
93
+
94
+ if len (args ) > 1 {
95
+ switchCtx .Version = args [1 ]
96
+ } else {
90
97
switchCtx .Version , err = binary .ChooseVersion (cliOpts .Env .BinDir , switchCtx .ProgramName )
91
98
if err != nil {
92
99
return err
93
100
}
94
- default :
95
- return fmt .Errorf ("invalid number of arguments" )
96
-
97
101
}
102
+
98
103
switchCtx .BinDir = cliOpts .Env .BinDir
99
104
switchCtx .IncDir = cliOpts .Env .IncludeDir
100
105
0 commit comments