-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathvalidateargs.go
More file actions
125 lines (112 loc) · 3.77 KB
/
Copy pathvalidateargs.go
File metadata and controls
125 lines (112 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package utils
import (
"fmt"
"strings"
"github.com/digitalghost-dev/poke-cli/styling"
)
type Validator struct {
MaxArgs int
CmdName string
RequireName bool
HasFlags bool
}
// checkLength checks if the number of arguments is lower than the max value. Helper Function.
func checkLength(args []string, max int) error {
if len(args) > max {
errMessage := styling.ErrorBorder.Render(
styling.ErrorColor.Render("✖ Error!") + "\nToo many arguments",
)
return fmt.Errorf("%s", errMessage)
}
return nil
}
// checkNoOtherOptions checks if there are exactly 3 arguments and the third argument is neither '-h' nor '--help'
func checkNoOtherOptions(args []string, max int, commandName string) error {
if len(args) == max && args[2] != "-h" && args[2] != "--help" {
errMsg := styling.ErrorColor.Render("✖ Error!") +
"\nThe only available options after the\n" + "<" + commandName + "> command are '-h' or '--help'"
return fmt.Errorf("%s", styling.ErrorBorder.Render(errMsg))
}
return nil
}
func ValidateArgs(args []string, v Validator) error {
if err := checkLength(args, v.MaxArgs); err != nil {
return err
}
if v.RequireName && len(args) == 2 {
errMessage := styling.ErrorBorder.Render(
styling.ErrorColor.Render("✖ Error!"),
fmt.Sprintf("\nPlease declare a(n) %s's name after the <%s> command", v.CmdName, v.CmdName),
fmt.Sprintf("\nRun 'poke-cli %s -h' for more details", v.CmdName),
"\nerror: insufficient arguments",
)
return fmt.Errorf("%s", errMessage)
}
if !v.HasFlags && !v.RequireName {
if err := checkNoOtherOptions(args, v.MaxArgs, v.CmdName); err != nil {
return err
}
}
return nil
}
// ValidatePokemonArgs validates the command line arguments
func ValidatePokemonArgs(args []string) error {
// Check if the number of arguments is less than 3
if len(args) < 3 {
errMessage := styling.ErrorBorder.Render(
styling.ErrorColor.Render("✖ Error!"),
"\nPlease declare a Pokémon's name after the <pokemon> command",
"\nRun 'poke-cli pokemon -h' for more details",
"\nerror: insufficient arguments",
)
return fmt.Errorf("%s", errMessage)
}
if err := checkLength(args, 8); err != nil {
return err
}
printImageFlagError := func() error {
msg := styling.ErrorBorder.Render(
styling.ErrorColor.Render("✖ Error!") +
"\nThe image flag (-i or --image) requires a non-empty value.\nValid sizes are: lg, md, sm.",
)
return fmt.Errorf("%s", msg)
}
for _, arg := range args {
switch {
case strings.HasPrefix(arg, "-i=") && strings.TrimPrefix(arg, "-i=") == "":
return printImageFlagError()
case strings.HasPrefix(arg, "--image=") && strings.TrimPrefix(arg, "--image=") == "":
return printImageFlagError()
case strings.HasPrefix(arg, "-image=") && strings.TrimPrefix(arg, "-image=") == "":
return printImageFlagError()
}
}
// Validate each argument after the Pokémon's name
if len(args) > 3 {
for _, arg := range args[3:] {
// Check for an empty flag after Pokémon's name
if arg == "-" || arg == "--" {
errorTitle := styling.ErrorColor.Render("✖ Error!")
errorString := fmt.Sprintf(
"\nEmpty flag '%s'.\nPlease specify valid flag(s).",
arg,
)
finalErrorMessage := errorTitle + errorString
renderedError := styling.ErrorBorder.Render(finalErrorMessage)
return fmt.Errorf("%s", renderedError)
}
// Check if the argument after Pokémon's name is an attempted flag
if arg[0] != '-' {
errorTitle := styling.ErrorColor.Render("✖ Error!")
errorString := fmt.Sprintf(
"\nInvalid argument '%s'.\nOnly flags are allowed after declaring a Pokémon's name",
arg,
)
finalErrorMessage := errorTitle + errorString
renderedError := styling.ErrorBorder.Render(finalErrorMessage)
return fmt.Errorf("%s", renderedError)
}
}
}
return nil
}