Skip to content

Commit

Permalink
check args
Browse files Browse the repository at this point in the history
  • Loading branch information
seemywingz committed Dec 21, 2024
1 parent a4993ec commit 0a2dcc2
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
6 changes: 4 additions & 2 deletions cmd/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ var chatCmd = &cobra.Command{
Use: "chat",
Short: "Open ended chat with OpenAI",
Long: ``,
Args: cobra.ExactArgs(1), // Expect exactly one argument
Args: func(cmd *cobra.Command, args []string) error {
return checkArgs(args)
},
Run: func(cmd *cobra.Command, args []string) {
prompt := args[0]

var err error
if convo {
for {
Expand Down
3 changes: 3 additions & 0 deletions cmd/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ var imageCmd = &cobra.Command{
Use: "image",
Short: "Generate an image from a prompt",
Long: ``,
Args: func(cmd *cobra.Command, args []string) error {
return checkArgs(args)
},
Run: func(cmd *cobra.Command, args []string) {
createImage(prompt, imageFile)
},
Expand Down
23 changes: 14 additions & 9 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,7 @@ var rootCmd = &cobra.Command{
Or whatever else you can think of. 🤔
`,
Args: func(cmd *cobra.Command, args []string) error {
if convo && len(args) == 0 {
// When --convo is used, no args are required
return nil
}
// Otherwise, exactly one arg must be provided
if len(args) != 1 {
return fmt.Errorf("Prompt Required")
}
return nil
return checkArgs(args)
},
Run: func(cmd *cobra.Command, args []string) {
var prompt string
Expand All @@ -61,6 +53,19 @@ var rootCmd = &cobra.Command{
},
}

func checkArgs(args []string) error {
if convo && len(args) == 0 {
// When --convo is used, no args are required
return nil
}
// Otherwise, exactly one arg must be provided
if len(args) != 1 {
return fmt.Errorf("Prompt Required")
}
prompt = args[0]
return nil
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
Expand Down
5 changes: 5 additions & 0 deletions cmd/tts.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package cmd

import (
"bytes"
"fmt"
"io"
"os"

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

func tts(text string) []byte {
ai.Voice = voice
fmt.Println("Generating audio...", text)
audioData, err := ai.TTS(text)
catchErr(err, "fatal")
if audioFile != "" {
Expand Down

0 comments on commit 0a2dcc2

Please sign in to comment.