package main

import (
	"fmt"
	"os"

	"github.com/spf13/cobra"
)

var verbose bool

func main() {
	// Root command
	rootCmd := &cobra.Command{
		Use:   "myapp",
		Short: "MyApp is a sample CLI application",
		Long:  `A demonstration of building CLI applications with Cobra.`,
	}

	// Add global flags
	rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose output")

	// Greet subcommand
	greetCmd := &cobra.Command{
		Use:   "greet [name]",
		Short: "Greet a user",
		Args:  cobra.ExactArgs(1),
		Run: func(cmd *cobra.Command, args []string) {
			name := args[0]
			if verbose {
				fmt.Printf("Preparing greeting for %s...\n", name)
			}
			fmt.Printf("Hello, %s!\n", name)
		},
	}

	// Version subcommand
	versionCmd := &cobra.Command{
		Use:   "version",
		Short: "Print the version number",
		Run: func(cmd *cobra.Command, args []string) {
			fmt.Println("MyApp v1.0.0")
		},
	}

	// Add subcommands to root
	rootCmd.AddCommand(greetCmd)
	rootCmd.AddCommand(versionCmd)

	if err := rootCmd.Execute(); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
}
