Command Flags

Flags accept optional or named parameters. Use BoolFlag, StringFlag, IntFlag. Read them with ctx.Bool(name), ctx.String(name), ctx.Int(name).

Defining flags

return command.New("greet", "Greet a user").
	BoolFlag("loud", "l", false, "Use exclamation marks").
	StringFlag("greeting", "g", "Hello", "Greeting message").
	IntFlag("repeat", "r", 1, "Repeat count").
	RunE(func(ctx *command.Ctx) error {
		loud := ctx.Bool("loud")
		greeting := ctx.String("greeting")
		repeat := ctx.Int("repeat")
		// ...
		return nil
	})

Flag types

  • BoolFlag(name, shorthand, default, usage)
  • StringFlag(name, shorthand, default, usage)
  • IntFlag(name, shorthand, default, usage)

Usage

go run . greet Alice --loud
go run . greet Alice -g "Hi" -r 3