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.
This commit is contained in:
Vito Castellano 2025-12-29 15:32:34 +01:00
commit fa7996453f
No known key found for this signature in database
GPG key ID: E13085DB38BC5819

View file

@ -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> 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
}