global parameters to commands using go-flags -


i'm using library https://github.com/jessevdk/go-flags

a command app might like:

ex list events

so have wrapper command

type excommand struct {     list list.listcommand `command:"list" description:"list events" subcommands-optional:"true"` } 

list command

type listcommand struct {     excommand excommand `command:"events" description:"list events"`      config string `short:"c" long:"config" description:"config file" optional:"yes"` } 

ex command

type eventscommand struct { }   func (c *listcommand) execute(args []string) error {     fmt.println("execute list")     _, val := range args {         fmt.println(val)     }     fmt.printf("c: %s\n", c.config)      return nil }  func (c *excommand) execute(args []string) error {     fmt.println("list events")     _, val := range args {         fmt.println(val)     }      return nil } 

so i'd do, have few options like

verbose config terse 

that global options can run on commands. can't seem figure out if there's way though library. have experience that? can add config each individual low level command, list in case eventscommand, seems i'm repeating myself adding @ each low level command instead of higher listcommand or excommand. in advance!

you can use newparser create new parser. you'll notice documentation first argument pointer struct "application options." i'll start explanation , follow working example. pretend have following struct contains application wide options:

type defaults struct {     verbose []bool `short:"v" long:"verbose" description:"show verbose debug information"`     terse   bool   `short:"t" long:"terse" description:"shows terse output"` } 

you can pass newparser function mentioned before so:

defaultoptions = defaults{} parser = flags.newparser(&defaultoptions, flags.default) 

your additional commands can added parser.addcommand function. example:

list := listcommand{} parser.addcommand("list", "lists something", "", &list) 

finish calling parser. parse , enjoy!

parser.parse() fmt.printf("verbose: %v\n", defaultoptions.verbose) fmt.printf("terse: %v\n", defaultoptions.terse) 

here's small fully-working example:

package main  import (     "fmt"     flags "github.com/jessevdk/go-flags" )  type defaults struct {     verbose []bool `short:"v" long:"verbose" description:"show verbose    debug information"`     terse   bool   `short:"t" long:"terse" description:"shows terse output"` }  type listcommand struct {     config string `short:"c" long:"config" description:"config file" optional:"yes"` }  func main() {     defaultoptions := defaults{}     listcmd := listcommand{}      parser := flags.newparser(&defaultoptions, flags.default)     parser.addcommand("list", "lists something", "", &listcmd)      parser.parse() } 

you can achieve you're asking including defaults struct anonymous struct in command structs. notice how referencing defaults type in listcommand struct not providing name field. allows me access fields of defaults if part of listcommand struct. following example code allows me provide verbose , terse flags when using <prog> list -h:

package main  import flags "github.com/jessevdk/go-flags"  type defaults struct {     verbose []bool `short:"v" long:"verbose" description:"show verbose    debug information"`     terse   bool   `short:"t" long:"terse" description:"shows terse output"` }  type listcommand struct {     config string `short:"c" long:"config" description:"config file" optional:"yes"`     defaults }  func main() {     listcmd := listcommand{}      parser := flags.newparser(nil, flags.default)     parser.addcommand("list", "lists something", "", &listcmd)      parser.parse() } 

Comments