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

Fix bool flag not working without OnTrue #2

Merged
merged 2 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
name: Test

on:
pull_request:
branches:
- main
push:
branches:
- main

jobs:
test:
name: Test
runs-on: ubuntu-20.04
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Run tests
shell: bash
run: |
go test
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
BSD 2-Clause License

Copyright (c) 2023, 2024, Nicholas Gasior
Copyright (c) 2023, 2024, Mikolaj Gasior

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
Expand Down
38 changes: 0 additions & 38 deletions Makefile

This file was deleted.

9 changes: 6 additions & 3 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,8 @@ func (c *CLI) processOnTrue(cmd *Cmd, fs []string, nflags map[string]interface{}
continue
}

c.parsedFlags[name] = "false"
if *(nflags[name]).(*bool) == true || *(aflags[cmd.flags[name].alias]).(*bool) == true {
c.parsedFlags[name] = "true"
// OnTrue is called when a flag is true
if *(nflags[name]).(*bool) || *(aflags[cmd.flags[name].alias]).(*bool) {
cmd.flags[name].options.onTrue(cmd)
}
}
Expand All @@ -243,6 +242,10 @@ func (c *CLI) processFlags(cmd *Cmd, fs []string, nflags map[string]interface{},
flag := cmd.flags[name]

if flag.valueType == TypeBool {
c.parsedFlags[name] = "false"
if *(nflags[name]).(*bool) || *(aflags[cmd.flags[name].alias]).(*bool) {
c.parsedFlags[name] = "true"
}
continue
}

Expand Down
10 changes: 9 additions & 1 deletion cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,18 @@ func TestCLI(t *testing.T) {
c := NewCLI("Example", "App", "Author <[email protected]>")
cmd1 := c.AddCmd("cmd1", "Prints out a string", func(c *CLI) int {
fmt.Fprintf(f, "TESTVALUE:%s%s\n\n", c.Flag("tekst"), c.Flag("alphanumdots"))
if c.Flag("bool") == "true" {
fmt.Fprintf(f, "BOOL:true")
}
return 2
})
cmd1.AddFlag("tekst", "t", "Text", "Text to print", TypeString, IsRequired)
cmd1.AddFlag("alphanumdots", "a", "Alphanum with dots", "Can have dots", TypeAlphanumeric, AllowDots)
cmd1.AddFlag("make-required", "r", "", "Make alphanumdots required", TypeBool, 0, OnTrue(func(c *Cmd) {
c.flags["alphanumdots"].flags = c.flags["alphanumdots"].flags | IsRequired
}))
// Boolean should work fine even when the optional OnTrue is not passed
cmd1.AddFlag("bool", "b", "", "Bool value", TypeBool, 0)

os.Args = []string{"test", "cmd1"}
got := c.Run()
Expand Down Expand Up @@ -66,7 +71,7 @@ func TestCLI(t *testing.T) {
t.Errorf("CLI.Run() should have returned 1 instead of %d", got)
}

os.Args = []string{"test", "cmd1", "--tekst", "Tekst123", "--alphanumdots", "aZ0.9"}
os.Args = []string{"test", "cmd1", "--tekst", "Tekst123", "--alphanumdots", "aZ0.9", "-b"}
got = c.Run()
if got != 2 {
t.Errorf("CLI.Run() should have returned 2 instead of %d", got)
Expand All @@ -85,4 +90,7 @@ func TestCLI(t *testing.T) {
if !strings.Contains(string(b), "TESTVALUE:Tekst123aZ0.9") {
t.Errorf("Cmd handler failed to work")
}
if !strings.Contains(string(b), "BOOL:true") {
t.Errorf("Cmd handler failed to work")
}
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/mikolajgs/broccli

go 1.22.1
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package broccli

const VERSION = "2.0.0"
const VERSION = "2.1.0"