-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Comments
BoolTFlag is for when you want to use The following works for me: Using BoolFlagpackage 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 BoolTFlagpackage 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.
|
thanks @markwylde for the help. really appreciate the effort. Got it to work with the normal bool flag thanks to your example. |
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")
} |
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
my tests
The text was updated successfully, but these errors were encountered: