Skip to content

How do boolean flags work #804

@ThomasSchoenbeck

Description

@ThomasSchoenbeck

Hi there,

i just tried to follow one of your examples, specifically the "ginger-crouton" example for the bool flag.

what i don't understand is why this example doesn't work for me. I could not find any other documentation on the bool flags, so right now i don't know how to figure out how to design and use bool flags.

Do you have another example or some other documentation at hand?

The code

package main

import (
  "log"
  "os"

  "github.com/urfave/cli"
)

func main() {
  app := cli.NewApp()
  app.Flags = []cli.Flag{
    cli.BoolTFlag{
      Name:  "ginger-crouton",
      Usage: "is it in the soup?",
    },
  }
  app.Action = func(ctx *cli.Context) error {
    if !ctx.Bool("ginger-crouton") {
      return cli.NewExitError("it is not in the soup", 86)
    }
    return nil
  }

  err := app.Run(os.Args)
  if err != nil {
    log.Fatal(err)
  }
}

my tests
image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions