Skip to content

Commit 62c0874

Browse files
returning correct return code for function calls (#150)
1 parent 02ce831 commit 62c0874

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

cli.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,23 +94,28 @@ func runCLI(args []string) int {
9494
return 2
9595
}
9696

97-
commands := map[string]func(){
97+
remainingArgs := mainFlagSet.Args()
98+
99+
commands := map[string]func() int{
98100
"ability": utils.HandleCommandOutput(ability.AbilityCommand),
99101
"move": utils.HandleCommandOutput(move.MoveCommand),
100102
"natures": utils.HandleCommandOutput(natures.NaturesCommand),
101103
"pokemon": utils.HandleCommandOutput(pokemon.PokemonCommand),
102104
"types": utils.HandleCommandOutput(types.TypesCommand),
103-
"search": search.SearchCommand,
105+
"search": func() int {
106+
search.SearchCommand()
107+
return 0
108+
},
104109
}
105110

106111
cmdArg := ""
107-
if len(os.Args) >= 2 {
108-
cmdArg = os.Args[1]
112+
if len(remainingArgs) >= 1 {
113+
cmdArg = remainingArgs[0]
109114
}
110115
cmdFunc, exists := commands[cmdArg]
111116

112117
switch {
113-
case len(os.Args) < 2:
118+
case len(remainingArgs) == 0 && !*latestFlag && !*shortLatestFlag && !*currentVersionFlag && !*shortCurrentVersionFlag:
114119
mainFlagSet.Usage()
115120
return 1
116121
case *latestFlag || *shortLatestFlag:
@@ -120,13 +125,11 @@ func runCLI(args []string) int {
120125
currentVersion()
121126
return 0
122127
case exists:
123-
cmdFunc()
124-
return 0
128+
return cmdFunc()
125129
default:
126-
command := os.Args[1]
127130
errMessage := styling.ErrorBorder.Render(
128131
styling.ErrorColor.Render("Error!"),
129-
fmt.Sprintf("\n\t%-15s", fmt.Sprintf("'%s' is not a valid command.\n", command)),
132+
fmt.Sprintf("\n\t%-15s", fmt.Sprintf("'%s' is not a valid command.\n", cmdArg)),
130133
styling.StyleBold.Render("\nCommands:"),
131134
fmt.Sprintf("\n\t%-15s %s", "ability", "Get details about an ability"),
132135
fmt.Sprintf("\n\t%-15s %s", "move", "Get details about a move"),
@@ -138,7 +141,6 @@ func runCLI(args []string) int {
138141
)
139142
output.WriteString(errMessage)
140143

141-
// This would typically be returned in a function or passed to something else
142144
fmt.Println(output.String())
143145

144146
return 1

cmd/utils/output.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@ import (
55
"os"
66
)
77

8-
// HandleCommandOutput wraps a function that returns (string, error) into a no-arg function
9-
// that prints the output to stdout or stderr depending on whether an error occurred.
10-
func HandleCommandOutput(fn func() (string, error)) func() {
11-
return func() {
8+
// HandleCommandOutput takes a function that returns (string, error) and wraps it in a no-argument
9+
// function that writes the returned string to stdout if there's no error, or to stderr if there is.
10+
// It returns an exit code: 0 on success, 1 on error.
11+
func HandleCommandOutput(fn func() (string, error)) func() int {
12+
return func() int {
1213
output, err := fn()
1314
if err != nil {
1415
fmt.Fprintln(os.Stderr, output)
15-
return
16+
return 1
1617
}
1718
fmt.Println(output)
19+
return 0
1820
}
1921
}

0 commit comments

Comments
 (0)