From b705ad045ca7b24d1b3a5a6677ad547575a67984 Mon Sep 17 00:00:00 2001 From: Mikolaj Gasior Date: Tue, 16 Jul 2024 20:57:56 +0200 Subject: [PATCH 1/2] Fix bool flag not working properly without the OnTrue option #1 --- .github/workflows/test.yml | 23 +++++++++++++++++++++++ LICENSE | 2 +- Makefile | 38 -------------------------------------- cli.go | 9 ++++++--- cli_test.go | 10 +++++++++- go.mod | 3 +++ 6 files changed, 42 insertions(+), 43 deletions(-) create mode 100644 .github/workflows/test.yml delete mode 100644 Makefile create mode 100644 go.mod diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..5f0ef30 --- /dev/null +++ b/.github/workflows/test.yml @@ -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 diff --git a/LICENSE b/LICENSE index b2dc711..df0cf7d 100644 --- a/LICENSE +++ b/LICENSE @@ -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: diff --git a/Makefile b/Makefile deleted file mode 100644 index 8897b99..0000000 --- a/Makefile +++ /dev/null @@ -1,38 +0,0 @@ -VERSION?=$$(cat version.go | grep VERSION | cut -d"=" -f2 | sed 's/"//g') -GOFMT_FILES?=$$(find . -name '*.go') -PROJECT_BIN?=go-broccli -PROJECT_SRC?=github.com/nicholasgasior/go-broccli - -default: build - -guard-%: - @ if [ "${${*}}" = "" ]; then \ - echo "Environment variable $* not set"; \ - exit 1; \ - fi - -fmt: - gofmt -w $(GOFMT_FILES) - -fmtcheck: - @ gofmt_files=$$(gofmt -l $(GOFMT_FILES)); \ - if [[ -n $${gofmt_files} ]]; then \ - echo "The following files fail gofmt:"; \ - echo "$${gofmt_files}"; \ - echo "Run \`make fmt\` to fix this."; \ - exit 1; \ - fi - -build: guard-GOPATH - mkdir -p $$GOPATH/bin/linux - mkdir -p $$GOPATH/bin/darwin - GOOS=linux GOARCH=amd64 go build -v -o $$GOPATH/bin/linux/${PROJECT_BIN} $$GOPATH/src/${PROJECT_SRC}/*.go - GOOS=darwin GOARCH=amd64 go build -v -o $$GOPATH/bin/darwin/${PROJECT_BIN} $$GOPATH/src/${PROJECT_SRC}/*.go - -test: - go test - - -.NOTPARALLEL: - -.PHONY: test fmt build diff --git a/cli.go b/cli.go index bcdb267..69a9dd6 100644 --- a/cli.go +++ b/cli.go @@ -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) } } @@ -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 } diff --git a/cli_test.go b/cli_test.go index 1d92520..092530b 100644 --- a/cli_test.go +++ b/cli_test.go @@ -28,6 +28,9 @@ func TestCLI(t *testing.T) { c := NewCLI("Example", "App", "Author ") 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) @@ -35,6 +38,8 @@ func TestCLI(t *testing.T) { 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() @@ -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) @@ -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") + } } diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..83a6646 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/mikolajgs/broccli + +go 1.22.1 From 69ef9c5d8263fe15597cb70b3165fc3d18043ffe Mon Sep 17 00:00:00 2001 From: Mikolaj Gasior Date: Tue, 16 Jul 2024 20:58:21 +0200 Subject: [PATCH 2/2] Bump version number --- version.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version.go b/version.go index 50698e3..c85a880 100644 --- a/version.go +++ b/version.go @@ -1,3 +1,3 @@ package broccli -const VERSION = "2.0.0" +const VERSION = "2.1.0"