From fa7996453f3fdee2f43ff53e8b651934554e95be Mon Sep 17 00:00:00 2001 From: Vito Castellano Date: Mon, 29 Dec 2025 15:32:34 +0100 Subject: [PATCH] feat(cli): add --version and --help flags Add -v/--version flag to display version information. Add custom --help message with usage examples. Improve CLI user experience with clear documentation. --- cmd/bbrew/main.go | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/cmd/bbrew/main.go b/cmd/bbrew/main.go index 4dd2aba..e4d9ab4 100644 --- a/cmd/bbrew/main.go +++ b/cmd/bbrew/main.go @@ -9,10 +9,32 @@ import ( ) func main() { - // Parse command line flags + // Define flags brewfilePath := flag.String("f", "", "Path to Brewfile (show only packages from this Brewfile)") + showVersion := flag.Bool("v", false, "Show version information") + flag.Bool("version", false, "Show version information") + + // Custom usage message + flag.Usage = func() { + fmt.Fprintf(os.Stderr, "Bold Brew - A TUI for Homebrew package management\n\n") + fmt.Fprintf(os.Stderr, "Usage: bbrew [options]\n\n") + fmt.Fprintf(os.Stderr, "Options:\n") + fmt.Fprintf(os.Stderr, " -f Path to Brewfile (show only packages from this Brewfile)\n") + fmt.Fprintf(os.Stderr, " -v, --version Show version information\n") + fmt.Fprintf(os.Stderr, " -h, --help Show this help message\n") + fmt.Fprintf(os.Stderr, "\nExamples:\n") + fmt.Fprintf(os.Stderr, " bbrew Launch the TUI with all packages\n") + fmt.Fprintf(os.Stderr, " bbrew -f ~/Brewfile Launch with packages from Brewfile\n") + } + flag.Parse() + // Handle --version flag (check both -v and --version) + if *showVersion || isFlagPassed("version") { + fmt.Printf("Bold Brew %s\n", services.AppVersion) + os.Exit(0) + } + // Validate Brewfile path if provided if *brewfilePath != "" { if _, err := os.Stat(*brewfilePath); os.IsNotExist(err) { @@ -42,3 +64,14 @@ func main() { log.Fatalf("Application error: %v", err) } } + +// isFlagPassed checks if a flag was explicitly passed on the command line. +func isFlagPassed(name string) bool { + found := false + flag.Visit(func(f *flag.Flag) { + if f.Name == name { + found = true + } + }) + return found +}