enbas/internal/flag/boolptrvalue.go
Dan Anglin b558c5adff
All checks were successful
Tests / test (pull_request) Successful in 16s
REUSE Compliance Check / check (push) Successful in 5s
fix: fixed the BoolPtrValue in internal flag pkg
- Fixed the BoolPtrValue type in the internal flag package by adding the
  IsBoolFlag() bool method to indicate to the command-line parser that
  the flag is a boolean flag.
- Added unit tests for the BoolPtrValue type.

PR: #56
2024-08-20 00:55:05 +01:00

38 lines
594 B
Go

package flag
import (
"fmt"
"strconv"
)
type BoolPtrValue struct {
Value *bool
}
func NewBoolPtrValue() BoolPtrValue {
return BoolPtrValue{
Value: nil,
}
}
func (b BoolPtrValue) String() string {
if b.Value == nil {
return "NOT SET"
}
return strconv.FormatBool(*b.Value)
}
func (b *BoolPtrValue) Set(value string) error {
boolVar, err := strconv.ParseBool(value)
if err != nil {
return fmt.Errorf("unable to parse %q as a boolean value: %w", value, err)
}
b.Value = new(bool)
*b.Value = boolVar
return nil
}
func (b *BoolPtrValue) IsBoolFlag() bool { return true }