Skip to content

Commit 5ba71b9

Browse files
adding checkLength function (#134)
1 parent 3a59aa5 commit 5ba71b9

File tree

1 file changed

+34
-61
lines changed

1 file changed

+34
-61
lines changed

cmd/validateargs.go

Lines changed: 34 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,25 @@
11
package cmd
22

33
import (
4-
"flag"
54
"fmt"
65
"github.com/digitalghost-dev/poke-cli/styling"
7-
"os"
86
)
97

10-
func handleHelpFlag(args []string) {
11-
if len(args) == 3 && (args[2] == "-h" || args[2] == "--help") {
12-
flag.Usage()
13-
14-
if flag.Lookup("test.v") == nil {
15-
os.Exit(0)
16-
}
8+
// checkLength checks if the number of arguments is lower than the max value
9+
func checkLength(args []string, max int) error {
10+
if len(args) > max {
11+
errMessage := styling.ErrorBorder.Render(
12+
styling.ErrorColor.Render("Error!") + "\nToo many arguments",
13+
)
14+
return fmt.Errorf("%s", errMessage)
1715
}
16+
return nil
1817
}
1918

2019
// ValidateAbilityArgs validates the command line arguments
2120
func ValidateAbilityArgs(args []string) error {
22-
handleHelpFlag(args)
23-
24-
if len(args) > 4 {
25-
errMessage := styling.ErrorBorder.Render(styling.ErrorColor.Render("Error!"), "\nToo many arguments")
26-
return fmt.Errorf("%s", errMessage)
21+
if err := checkLength(args, 4); err != nil {
22+
return err
2723
}
2824

2925
if len(args) == 2 {
@@ -36,11 +32,8 @@ func ValidateAbilityArgs(args []string) error {
3632

3733
// ValidateMoveArgs validates the command line arguments
3834
func ValidateMoveArgs(args []string) error {
39-
handleHelpFlag(args)
40-
41-
if len(args) > 4 {
42-
errMessage := styling.ErrorBorder.Render(styling.ErrorColor.Render("Error!"), "\nToo many arguments")
43-
return fmt.Errorf("%s", errMessage)
35+
if err := checkLength(args, 3); err != nil {
36+
return err
4437
}
4538

4639
if len(args) == 2 {
@@ -53,30 +46,23 @@ func ValidateMoveArgs(args []string) error {
5346

5447
// ValidateNaturesArgs validates the command line arguments
5548
func ValidateNaturesArgs(args []string) error {
56-
handleHelpFlag(args)
57-
58-
if len(args) > 3 {
59-
errMessage := styling.ErrorBorder.Render(styling.ErrorColor.Render("Error!"), "\nToo many arguments")
60-
return fmt.Errorf("%s", errMessage)
49+
if err := checkLength(args, 3); err != nil {
50+
return err
6151
}
6252

6353
// Check if there are exactly 3 arguments and the third argument is neither '-h' nor '--help'
6454
// If true, return an error message since only '-h' and '--help' are allowed after 'types'
65-
if len(args) == 3 && (args[2] != "-h" && args[2] != "--help") {
66-
errorTitle := styling.ErrorColor.Render("Error!")
67-
errorString := "\nThe only currently available options\nafter [natures] command are '-h' or '--help'"
68-
finalErrorMessage := errorTitle + errorString
69-
renderedError := styling.ErrorBorder.Render(finalErrorMessage)
70-
return fmt.Errorf("%s", renderedError)
55+
if len(args) == 3 && args[2] != "-h" && args[2] != "--help" {
56+
errMsg := styling.ErrorColor.Render("Error!") +
57+
"\nThe only currently available options\nafter <natures> command are '-h' or '--help'"
58+
return fmt.Errorf("%s", styling.ErrorBorder.Render(errMsg))
7159
}
7260

7361
return nil
7462
}
7563

7664
// ValidatePokemonArgs validates the command line arguments
7765
func ValidatePokemonArgs(args []string) error {
78-
handleHelpFlag(args)
79-
8066
// Check if the number of arguments is less than 3
8167
if len(args) < 3 {
8268
errMessage := styling.ErrorBorder.Render(
@@ -88,13 +74,8 @@ func ValidatePokemonArgs(args []string) error {
8874
return fmt.Errorf("%s", errMessage)
8975
}
9076

91-
// Check if there are too many arguments
92-
if len(args) > 7 {
93-
errMessage := styling.ErrorBorder.Render(
94-
styling.ErrorColor.Render("Error!"),
95-
"\nToo many arguments",
96-
)
97-
return fmt.Errorf("%s", errMessage)
77+
if err := checkLength(args, 7); err != nil {
78+
return err
9879
}
9980

10081
// Validate each argument after the Pokémon's name
@@ -131,41 +112,33 @@ func ValidatePokemonArgs(args []string) error {
131112

132113
// ValidateSearchArgs validates the command line arguments
133114
func ValidateSearchArgs(args []string) error {
134-
if len(args) > 3 {
135-
errMessage := styling.ErrorBorder.Render(styling.ErrorColor.Render("Error!"), "\nToo many arguments")
136-
return fmt.Errorf("%s", errMessage)
115+
if err := checkLength(args, 3); err != nil {
116+
return err
137117
}
138118

139119
// Check if there are exactly 3 arguments and the third argument is neither '-h' nor '--help'
140-
// If true, return an error message since only '-h' and '--help' are allowed after 'types'
141-
if len(args) == 3 && (args[2] != "-h" && args[2] != "--help") {
142-
errorTitle := styling.ErrorColor.Render("Error!")
143-
errorString := "\nThe only currently available options\nafter [search] command are '-h' or '--help'"
144-
finalErrorMessage := errorTitle + errorString
145-
renderedError := styling.ErrorBorder.Render(finalErrorMessage)
146-
return fmt.Errorf("%s", renderedError)
120+
// If true, return an error message since only '-h' and '--help' are allowed after <search>
121+
if len(args) == 3 && args[2] != "-h" && args[2] != "--help" {
122+
errMsg := styling.ErrorColor.Render("Error!") +
123+
"\nThe only currently available options\nafter <search> command are '-h' or '--help'"
124+
return fmt.Errorf("%s", styling.ErrorBorder.Render(errMsg))
147125
}
148126

149127
return nil
150128
}
151129

152130
// ValidateTypesArgs validates the command line arguments
153131
func ValidateTypesArgs(args []string) error {
154-
handleHelpFlag(args)
155-
156-
if len(args) > 3 {
157-
errMessage := styling.ErrorBorder.Render(styling.ErrorColor.Render("Error!"), "\nToo many arguments")
158-
return fmt.Errorf("%s", errMessage)
132+
if err := checkLength(args, 3); err != nil {
133+
return err
159134
}
160135

161136
// Check if there are exactly 3 arguments and the third argument is neither '-h' nor '--help'
162-
// If true, return an error message since only '-h' and '--help' are allowed after 'types'
163-
if len(args) == 3 && (args[2] != "-h" && args[2] != "--help") {
164-
errorTitle := styling.ErrorColor.Render("Error!")
165-
errorString := "\nThe only currently available options\nafter [types] command are '-h' or '--help'"
166-
finalErrorMessage := errorTitle + errorString
167-
renderedError := styling.ErrorBorder.Render(finalErrorMessage)
168-
return fmt.Errorf("%s", renderedError)
137+
// If true, return an error message since only '-h' and '--help' are allowed after <types>
138+
if len(args) == 3 && args[2] != "-h" && args[2] != "--help" {
139+
errMsg := styling.ErrorColor.Render("Error!") +
140+
"\nThe only currently available options\nafter <types> command are '-h' or '--help'"
141+
return fmt.Errorf("%s", styling.ErrorBorder.Render(errMsg))
169142
}
170143

171144
return nil

0 commit comments

Comments
 (0)