Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do boolean flags work #804

Closed
ThomasSchoenbeck opened this issue Feb 13, 2019 · 3 comments
Closed

How do boolean flags work #804

ThomasSchoenbeck opened this issue Feb 13, 2019 · 3 comments

Comments

@ThomasSchoenbeck
Copy link

ThomasSchoenbeck commented Feb 13, 2019

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

@markwylde
Copy link

markwylde commented Feb 16, 2019

Hi @ThomasSchoenbeck

BoolTFlag is for when you want to use --whatever=true, whereas BoolFlag is for --whatever.

The following works for me:

Using BoolFlag

package main

import (
	"fmt"
	"log"
	"os"

	"github.com/urfave/cli"
)

func main() {
	app := cli.NewApp()
	app.Flags = []cli.Flag{
		cli.BoolFlag{
			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)
		}
		fmt.Println("great, it is in the soup")
		return nil
	}

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

This outputs:

mark@Marks-MacBook-Pro: $ go run testtttt.go
great, it is in the soup
mark@Marks-MacBook-Pro: $ go run testtttt.go --ginger-crouton
it is not in the soup
exit status 86

Using BoolTFlag

package main

import (
	"fmt"
	"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.BoolT("ginger-crouton") {
			return cli.NewExitError("it is not in the soup", 86)
		}
		fmt.Println("great, it is in the soup")
		return nil
	}

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

This outputs:

mark@Marks-MacBook-Pro: $ go run testtttt.go
it is not in the soup
exit status 86
mark@Marks-MacBook-Pro: $ go run testtttt.go --ginger-crouton
it is not in the soup
exit status 86
mark@Marks-MacBook-Pro: $ go run testtttt.go --ginger-crouton=true
it is not in the soup
exit status 86
mark@Marks-MacBook-Pro: $ go run testtttt.go --ginger-crouton=false
great, it is in the soup

On a sidenote, if you're wondering why the BoolT defaults to true, have a look at the explanation from @jszwedko here.

👍 I agree it is a bit confusing which is why I believe it's removal in v2 will be an improvement.

It is specified in the godoc for the package (https://godoc.org/github.com/urfave/cli#BoolTFlag), but let me know if you think the documentation there could be improved (or open a PR 😄 ).

Gonna close this out, but thanks for opening this discussion!

@ThomasSchoenbeck
Copy link
Author

thanks @markwylde for the help. really appreciate the effort.

Got it to work with the normal bool flag thanks to your example.

@talentlessguy
Copy link

I don't know why but here a flag doesn't work:

main.go

app.Commands = []cli.Command{
		{
			Name:   "build",
			Usage:  "Compile Go to WebAssembly",
			Action: CompileToWASM,
			Flags: []cli.Flag{
				cli.BoolFlag{
					Name:  "native",
					Usage: "Compile with native Go compiler",
				},
			},
		},
},

build.go

// ...
func CompileToWASM(c *cli.Context) {

	cmd := exec.Command("tinygo", "build", "-o", "build/out.wasm", "./src")

	if c.Bool("native") {
		cmd = exec.Command("go", "build", "-o", "build/out.wasm", "./src/*.go")
	}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants