Skip to content

Instantly share code, notes, and snippets.

@madflojo
Last active December 29, 2020 07:51
Show Gist options
  • Save madflojo/5a71e12e48758d6af09dc73a2754f924 to your computer and use it in GitHub Desktop.
Save madflojo/5a71e12e48758d6af09dc73a2754f924 to your computer and use it in GitHub Desktop.
Example of a command-line projects main() function.
// main runs the command-line parsing and validations. This function will also start the application logic execution.
func main() {
// Parse command-line arguments
var opts options
args, err := flags.ParseArgs(&opts, os.Args[1:])
if err != nil {
os.Exit(1)
}
// Convert to internal config
cfg := config.New()
cfg.Verbose = opts.Verbose
// more taking command line options and putting them into a config struct.
if opts.Pass {
// ask the user for a password
}
// Run the App
err = app.Run(cfg)
if err != nil {
// do stuff
os.Exit(1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment