From 0a2dcc2b58f285025e10cc6f53141046c36daa5e Mon Sep 17 00:00:00 2001 From: seemywings Date: Sat, 21 Dec 2024 15:19:48 -0500 Subject: [PATCH] check args --- cmd/chat.go | 6 ++++-- cmd/image.go | 3 +++ cmd/root.go | 23 ++++++++++++++--------- cmd/tts.go | 5 +++++ 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/cmd/chat.go b/cmd/chat.go index 384d8cc..7ed18b7 100644 --- a/cmd/chat.go +++ b/cmd/chat.go @@ -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 { diff --git a/cmd/image.go b/cmd/image.go index b71e01a..39759bc 100644 --- a/cmd/image.go +++ b/cmd/image.go @@ -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) }, diff --git a/cmd/root.go b/cmd/root.go index dd8170c..fd52860 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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 @@ -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() { diff --git a/cmd/tts.go b/cmd/tts.go index b5d0e89..7435f70 100644 --- a/cmd/tts.go +++ b/cmd/tts.go @@ -5,6 +5,7 @@ package cmd import ( "bytes" + "fmt" "io" "os" @@ -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 { @@ -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 != "" {