README

autoflags

Package autoflags provides a convenient way of exposing struct fields as command line flags. Exposed fields should have flag tag attached: flag:"flagName,usage string".

For examples see documentation.

Expand ▾ Collapse ▴

Documentation

Overview

    Package autoflags provides a convenient way of exposing struct fields as command line flags. Exposed fields should have special tag attached:

    var config = struct {
    	Name    string `flag:"name,user name"`
    	Age     uint   `flag:"age"`
    	Married bool   // this won't be exposed
    }{
    	// default values
    	Name: "John Doe",
    	Age:  34,
    }
    

    After declaring your flags and their default values as above, just register flags with autoflags.Define and call flag.Parse() as usual:

    autoflags.Define(&config)
    flag.Parse()
    

    Now config struct has its fields populated from command line flags. Call the program with flags to override default values:

    progname -name "Jane Roe" -age 29
    

    Package autoflags understands all basic types supported by flag's package xxxVar functions: int, int64, uint, uint64, float64, bool, string, time.Duration. Types implementing flag.Value interface are also supported. Attaching non-empty `flag` tag to field of unsupported type would result in panic.

    Index

    Examples

    Constants

    This section is empty.

    Variables

    This section is empty.

    Functions

    func Define

    func Define(config interface{})

      Define takes pointer to struct and declares flags for its flag-tagged fields. Valid tags have one of the following formats:

      `flag:"flagname"`
      `flag:"flagname,usage string"`
      

      Define would panic if given unsupported/invalid argument (anything but non-nil pointer to struct) or if any config attribute with `flag` tag is of type unsupported by the flag package (consider implementing flag.Value interface for such attributes).

      func DefineFlagSet

      func DefineFlagSet(fs *flag.FlagSet, config interface{})

        DefineFlagSet takes pointer to struct and declares flags for its flag-tagged fields on given FlagSet. Valid tags have one of the following formats:

        `flag:"flagname"`
        `flag:"flagname,usage string"`
        

        DefineFlagSet would panic if given unsupported/invalid config argument (anything but non-nil pointer to struct) or if any config attribute with `flag` tag is of type unsupported by the flag package (consider implementing flag.Value interface for such attrubutes).

        Example
        Output:
        
        
        before parsing flags:
        {Name:John Doe Age:34 Married:false}
        
        after parsing flags:
        {Name:Jane Roe Age:29 Married:false}
        

        func Parse

        func Parse(config interface{})

          Parse is a shortcut for:

          autoflags.Define(&args)
          flag.Parse()
          

          Types

          This section is empty.

          Source Files