README

PkgGoDev Go Report Card

Go Flag Utils

This is a Go library that provides a few types you can use with flag.Var().

Sample Code

There are detailed examples for each type on pkg.go.dev, but here's a quick example:

// Default to red
var color flagutils.RGB{
	R: 0xff,
}
flag.Var(
	&color,
	"color",
	"The color to use",
)
var featureA, featureB flagutils.OneOf
flagutils.GroupOneOf(&featureA, &featureB)
flag.Var(
	&featureA,
	"featureA",
	"Run feature A, unsets featureB",
)
flag.Var(
	&featureB,
	"featureB",
	"Run feature B, unsets featureA",
)
flag.Parse()
switch {
case featureA.Bool:
	// Run feature A
case featureB.Bool:
	// Run feature B
}

License

BSD License.

Documentation

Overview

    Package flagutils provides a few types you can use with flag.Var().

    Please refer to the examples to see more details.

    Index

    Examples

    Constants

    This section is empty.

    Variables

    This section is empty.

    Functions

    func GroupAllOneOf

    func GroupAllOneOf(fs *flag.FlagSet)

      GroupAllOneOf group all OneOf flag values inside the FlagSet together.

      It finds all OneOf values in the FlagSet, then calls GroupOneOf.

      func GroupOneOf

      func GroupOneOf(values ...*OneOf)

        GroupOneOf groups OneOf values together.

        Please note that in case of regrouping, it only affects the values in the new group. For example say you have a previous group:

        var a, b, c OneOf
        GroupOneOf(&a, &b, &c)
        

        Then you regroup a and b:

        GroupOneOf(&a, &b)
        

        After the second GroupOneOf, a and b have the group info of each other, but c still records the group info of all of them. You will need to also regroup c with itself to complete the regrouping:

        GroupOneOf(&c)
        

        Types

        type OneOf

        type OneOf struct {
        	Bool bool
        	// contains filtered or unexported fields
        }

          OneOf is a flag type that works like bool, but can be grouped together.

          When you group several OneOf values together, (via GroupOneOf or GroupAllOneOf) whenever one of them is set to true, the rest of the group will be set to false automatically.

          It implements flag.Getter interface.

          Example

            This example demostrates how to use OneOf in your program.

            Output:
            
            

            func (*OneOf) Get

            func (b *OneOf) Get() interface{}

              Get implements flag.Getter.

              func (*OneOf) Group

              func (b *OneOf) Group() []*OneOf

                Group returns a copy of the group b is in.

                func (*OneOf) IsBoolFlag

                func (b *OneOf) IsBoolFlag() bool

                  IsBoolFlag implements flag.Value.

                  func (*OneOf) Set

                  func (b *OneOf) Set(s string) (err error)

                    Set implements flag.Value.

                    func (*OneOf) String

                    func (b *OneOf) String() string

                    type RGB

                    type RGB color.RGBA

                      RGB is a flag type that can be used for RGB colors.

                      It converts between string format "rrggbb" (no leading "#") and RGB color.

                      It implements color.Color and flag.Getter interfaces.

                      Example

                        This example demostrates how to use RGB in your program.

                        Output:
                        
                        0000ff
                        0 0 65535 0
                        

                        func (*RGB) Get

                        func (c *RGB) Get() interface{}

                          Get implements flag.Getter.

                          func (*RGB) RGBA

                          func (c *RGB) RGBA() (uint32, uint32, uint32, uint32)

                            RGBA implements color.Color.

                            func (*RGB) Set

                            func (c *RGB) Set(s string) error

                              Set implements flag.Value.

                              func (*RGB) String

                              func (c *RGB) String() string

                              type RGBA

                              type RGBA color.RGBA

                                RGBA is a flag type that can be used for RGBA colors.

                                It converts between string format "rrggbbaa" (no leading "#") and RGBA color.

                                It implements color.Color and flag.Getter interfaces.

                                Example

                                  This example demostrates how to use RGBA in your program.

                                  Output:
                                  
                                  ff000080
                                  65535 0 0 32896
                                  

                                  func (*RGBA) Get

                                  func (c *RGBA) Get() interface{}

                                    Get implements flag.Getter.

                                    func (*RGBA) RGBA

                                    func (c *RGBA) RGBA() (uint32, uint32, uint32, uint32)

                                      RGBA implements color.Color.

                                      func (*RGBA) Set

                                      func (c *RGBA) Set(s string) error

                                        Set implements flag.Value.

                                        func (*RGBA) String

                                        func (c *RGBA) String() string