package main import ( "flag" "fmt" "os" "runtime/debug" ) // Flags supplied to the cli. type Flags struct { ConfigPath string Discover bool Verbose bool } // printVersion emits version and build information to stdout. func printVersion() { fmt.Printf("%s: %s (%s)\n", Name, Version, Mode) if Commit != "" { fmt.Printf(" commit: %s\n", Commit) } if Date != "" { fmt.Printf(" built: %s\n", Date) } // Without build stamps, a module-aware build still records the revision // it was built from, so fall back to what the toolchain embedded. if Commit == "" { if bi, ok := debug.ReadBuildInfo(); ok { for _, s := range bi.Settings { switch s.Key { case "vcs.revision": fmt.Printf(" commit: %s\n", s.Value) case "vcs.time": fmt.Printf(" built: %s\n", s.Value) } } } } } // ParseFlags parses the supplied command-line flags. func (a *App) ParseFlags() { app.flags = new(Flags) flag.Usage = func() { fmt.Printf(Name + ": " + Description + ".\n\nUsage:\n") flag.PrintDefaults() } // Print the version and exit when requested. var showVersion bool usage := "Print version information and quit" flag.BoolVar(&showVersion, "version", false, usage) flag.BoolVar(&showVersion, "v", false, usage+" (shorthand)") // Override the configuration path. usage = "Load configuration from `FILE`" flag.StringVar(&app.flags.ConfigPath, "config", "", usage) flag.StringVar(&app.flags.ConfigPath, "c", "", usage+" (shorthand)") // Query each configured device for its integration IDs and exit. usage = "Connect to each device, print its integration IDs, and exit" flag.BoolVar(&app.flags.Discover, "discover", false, usage) flag.BoolVar(&app.flags.Discover, "d", false, usage+" (shorthand)") // Force debug-level logging to the console, overriding the configured log level. usage = "Force debug-level logging to the console, overriding the config" flag.BoolVar(&app.flags.Verbose, "verbose", false, usage) flag.BoolVar(&app.flags.Verbose, "V", false, usage+" (shorthand)") flag.Parse() if showVersion { printVersion() os.Exit(0) } }