github.com/gobuffalo/buffalo-cli/v2@v2.0.0-alpha.15.0.20200919213536-a7350c8e6799/cli/cmds/build/flags.go (about)

     1  package build
     2  
     3  import (
     4  	"io"
     5  
     6  	"github.com/gobuffalo/buffalo-cli/v2/internal/flagger"
     7  	"github.com/gobuffalo/plugins/plugflag"
     8  	"github.com/spf13/pflag"
     9  )
    10  
    11  func (cmd *Cmd) PrintFlags(w io.Writer) error {
    12  	flags := cmd.Flags()
    13  	flags.SetOutput(w)
    14  	flags.PrintDefaults()
    15  	return nil
    16  }
    17  
    18  // Flags returns a defined set of flags for this command.
    19  // It imports flags provided by plugins that use either
    20  // the `Flagger` or `Pflagger` interfaces. Flags provided
    21  // by plugins will have their shorthand ("-x") flag stripped
    22  // and the name ("--some-flag") of the flag will be
    23  // prefixed with the plugin's name ("--xyz-some-flag")
    24  func (cmd *Cmd) Flags() *pflag.FlagSet {
    25  	if cmd.flags != nil {
    26  		return cmd.flags
    27  	}
    28  
    29  	flags := pflag.NewFlagSet(cmd.PluginName(), pflag.ContinueOnError)
    30  
    31  	flags.BoolVarP(&cmd.help, "help", "h", false, "print this help")
    32  	flags.BoolVarP(&cmd.verbose, "verbose", "v", false, "print debugging information")
    33  	flags.BoolVarP(&cmd.static, "static", "s", false, "build a static binary using  --ldflags '-linkmode external -extldflags \"-static\"'")
    34  
    35  	flags.StringVar(&cmd.ldFlags, "ldflags", "", "set any ldflags to be passed to the go build")
    36  	flags.StringVar(&cmd.mod, "mod", "", "-mod flag for go build")
    37  	flags.StringVarP(&cmd.bin, "output", "o", cmd.bin, "set the name of the binary [default: bin/<module name>]")
    38  	flags.StringVarP(&cmd.environment, "environment", "", "development", "set the environment for the binary")
    39  	flags.StringVarP(&cmd.tags, "tags", "t", "", "compile with specific build tags")
    40  
    41  	plugs := cmd.ScopedPlugins()
    42  
    43  	for _, p := range plugs {
    44  		switch t := p.(type) {
    45  		case Flagger:
    46  			for _, f := range plugflag.Clean(p, t.BuildFlags()) {
    47  				flags.AddGoFlag(f)
    48  			}
    49  		case Pflagger:
    50  			for _, f := range flagger.CleanPflags(p, t.BuildFlags()) {
    51  				flags.AddFlag(f)
    52  			}
    53  		}
    54  	}
    55  
    56  	cmd.flags = flags
    57  	return cmd.flags
    58  }