Skip to content

Commit 0a2dcc2

Browse files
committed
check args
1 parent a4993ec commit 0a2dcc2

File tree

4 files changed

+26
-11
lines changed

4 files changed

+26
-11
lines changed

cmd/chat.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ var chatCmd = &cobra.Command{
2222
Use: "chat",
2323
Short: "Open ended chat with OpenAI",
2424
Long: ``,
25-
Args: cobra.ExactArgs(1), // Expect exactly one argument
25+
Args: func(cmd *cobra.Command, args []string) error {
26+
return checkArgs(args)
27+
},
2628
Run: func(cmd *cobra.Command, args []string) {
27-
prompt := args[0]
29+
2830
var err error
2931
if convo {
3032
for {

cmd/image.go

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ var imageCmd = &cobra.Command{
2525
Use: "image",
2626
Short: "Generate an image from a prompt",
2727
Long: ``,
28+
Args: func(cmd *cobra.Command, args []string) error {
29+
return checkArgs(args)
30+
},
2831
Run: func(cmd *cobra.Command, args []string) {
2932
createImage(prompt, imageFile)
3033
},

cmd/root.go

+14-9
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,7 @@ var rootCmd = &cobra.Command{
4141
Or whatever else you can think of. 🤔
4242
`,
4343
Args: func(cmd *cobra.Command, args []string) error {
44-
if convo && len(args) == 0 {
45-
// When --convo is used, no args are required
46-
return nil
47-
}
48-
// Otherwise, exactly one arg must be provided
49-
if len(args) != 1 {
50-
return fmt.Errorf("Prompt Required")
51-
}
52-
return nil
44+
return checkArgs(args)
5345
},
5446
Run: func(cmd *cobra.Command, args []string) {
5547
var prompt string
@@ -61,6 +53,19 @@ var rootCmd = &cobra.Command{
6153
},
6254
}
6355

56+
func checkArgs(args []string) error {
57+
if convo && len(args) == 0 {
58+
// When --convo is used, no args are required
59+
return nil
60+
}
61+
// Otherwise, exactly one arg must be provided
62+
if len(args) != 1 {
63+
return fmt.Errorf("Prompt Required")
64+
}
65+
prompt = args[0]
66+
return nil
67+
}
68+
6469
// Execute adds all child commands to the root command and sets flags appropriately.
6570
// This is called by main.main(). It only needs to happen once to the rootCmd.
6671
func Execute() {

cmd/tts.go

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package cmd
55

66
import (
77
"bytes"
8+
"fmt"
89
"io"
910
"os"
1011

@@ -21,6 +22,9 @@ var ttsCmd = &cobra.Command{
2122
Long: `OpenAI Text to Speech API - TTS
2223
You can use the TTS API to generate audio from text.
2324
`,
25+
Args: func(cmd *cobra.Command, args []string) error {
26+
return checkArgs(args)
27+
},
2428
Run: func(cmd *cobra.Command, args []string) {
2529
audio := tts(prompt)
2630
if audio != nil {
@@ -36,6 +40,7 @@ func init() {
3640

3741
func tts(text string) []byte {
3842
ai.Voice = voice
43+
fmt.Println("Generating audio...", text)
3944
audioData, err := ai.TTS(text)
4045
catchErr(err, "fatal")
4146
if audioFile != "" {

0 commit comments

Comments
 (0)