Skip to content

Commit

Permalink
adding custom flag to flagset
Browse files Browse the repository at this point in the history
Signed-off-by: Artem Bortnikov <[email protected]>
  • Loading branch information
BROngineer committed Jun 15, 2024
1 parent d264430 commit 5a4bcbe
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 18 deletions.
15 changes: 15 additions & 0 deletions flagset.go
Original file line number Diff line number Diff line change
Expand Up @@ -1095,3 +1095,18 @@ func (fs *FlagSet) GetCounter(name string) int {
f := fs.flagByName(name)
return flag.DerefOrDie[int](f.Value())
}

func CustomFlag[T any](fs *FlagSet, name string, opts ...flag.Option) {
f := flag.NewCustom[T](name, opts...)
fs.addFlag(f)
}

func GetCustomFlag[T any](fs *FlagSet, name string) T {
f := fs.flagByName(name)
return flag.DerefOrDie[T](f.Value())
}

func GetCustomFlagPtr[T any](fs *FlagSet, name string) *T {
f := fs.flagByName(name)
return flag.PtrOrDie[T](f.Value())
}
98 changes: 80 additions & 18 deletions flagset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package helium
import (
"errors"
"os"
"strconv"
"testing"
"time"

Expand Down Expand Up @@ -198,6 +199,12 @@ func assertParsedValue(t *testing.T, fs *FlagSet, r result) {
case "counter":
val := fs.GetCounter(r.flagName)
assert.Equal(t, r.flagValue, val)
case "custom":
val := GetCustomFlag[custom](fs, r.flagName)
assert.Equal(t, r.flagValue, val)
ptr := GetCustomFlagPtr[custom](fs, r.flagName)
require.NotNil(t, ptr)
assert.Equal(t, r.flagValue, *ptr)
default:
t.Fatalf("unknown flag type: %s", r.flagType)
}
Expand All @@ -222,13 +229,25 @@ type flagSetTest struct {
input []string
}

type custom struct {
field int
}

func parser(input string) (any, error) {
v, err := strconv.Atoi(input)
if err != nil {
return nil, err
}
return &custom{field: v}, nil
}

func TestFlagSet_Parse(t *testing.T) {
t.Parallel()
tests := []flagSetTest{
{
name: "parse",
flagSet: func() *FlagSet {
fs := &FlagSet{}
fs := NewFlagSet()
fs.String("sample-string")
fs.Bool("sample-bool")
return fs
Expand All @@ -245,7 +264,7 @@ func TestFlagSet_Parse(t *testing.T) {
{
name: "parse error",
flagSet: func() *FlagSet {
fs := &FlagSet{}
fs := NewFlagSet()
fs.String("sample-string")
fs.Bool("sample-bool")
return fs
Expand All @@ -260,7 +279,7 @@ func TestFlagSet_Parse(t *testing.T) {
{
name: "parse shorthand",
flagSet: func() *FlagSet {
fs := &FlagSet{}
fs := NewFlagSet()
fs.String("sample-string")
fs.Bool("sample-bool")
fs.Counter("sample-counter", flag.Shorthand("c"))
Expand All @@ -279,7 +298,7 @@ func TestFlagSet_Parse(t *testing.T) {
{
name: "parse stacked",
flagSet: func() *FlagSet {
fs := &FlagSet{}
fs := NewFlagSet()
fs.Bool("sample-bool-one", flag.Shorthand("o"))
fs.Bool("sample-bool-two", flag.Shorthand("t"))
fs.Counter("sample-counter", flag.Shorthand("c"))
Expand All @@ -298,7 +317,7 @@ func TestFlagSet_Parse(t *testing.T) {
{
name: "parse stacked with value",
flagSet: func() *FlagSet {
fs := &FlagSet{}
fs := NewFlagSet()
fs.Bool("sample-bool", flag.Shorthand("b"))
fs.Counter("sample-counter", flag.Shorthand("c"))
fs.Int("sample-int", flag.Shorthand("i"))
Expand All @@ -317,7 +336,7 @@ func TestFlagSet_Parse(t *testing.T) {
{
name: "parse error duplicate",
flagSet: func() *FlagSet {
fs := &FlagSet{}
fs := NewFlagSet()
fs.String("sample-string")
fs.Bool("sample-bool")
return fs
Expand All @@ -332,7 +351,7 @@ func TestFlagSet_Parse(t *testing.T) {
{
name: "parse slice duplicate",
flagSet: func() *FlagSet {
fs := &FlagSet{}
fs := NewFlagSet()
fs.String("sample-string")
fs.BoolSlice("sample-bool")
return fs
Expand All @@ -349,7 +368,7 @@ func TestFlagSet_Parse(t *testing.T) {
{
name: "parse unknown flag error",
flagSet: func() *FlagSet {
fs := &FlagSet{}
fs := NewFlagSet()
fs.String("sample-string")
return fs
},
Expand All @@ -363,7 +382,7 @@ func TestFlagSet_Parse(t *testing.T) {
{
name: "parse unknown shorthand error",
flagSet: func() *FlagSet {
fs := &FlagSet{}
fs := NewFlagSet()
fs.String("sample-string", flag.Shorthand("s"))
return fs
},
Expand All @@ -377,7 +396,7 @@ func TestFlagSet_Parse(t *testing.T) {
{
name: "parse unknown shorthand stacked error",
flagSet: func() *FlagSet {
fs := &FlagSet{}
fs := NewFlagSet()
fs.String("sample-string", flag.Shorthand("s"))
return fs
},
Expand All @@ -391,7 +410,7 @@ func TestFlagSet_Parse(t *testing.T) {
{
name: "parse stacked no value error",
flagSet: func() *FlagSet {
fs := &FlagSet{}
fs := NewFlagSet()
fs.String("sample-string", flag.Shorthand("s"))
return fs
},
Expand All @@ -405,7 +424,7 @@ func TestFlagSet_Parse(t *testing.T) {
{
name: "parse no value error",
flagSet: func() *FlagSet {
fs := &FlagSet{}
fs := NewFlagSet()
fs.String("sample-string", flag.Shorthand("s"))
return fs
},
Expand All @@ -419,7 +438,7 @@ func TestFlagSet_Parse(t *testing.T) {
{
name: "parse integers",
flagSet: func() *FlagSet {
fs := &FlagSet{}
fs := NewFlagSet()
fs.Int("sample-int", flag.Shorthand("a"))
fs.Int8("sample-int8", flag.Shorthand("b"))
fs.Int16("sample-int16", flag.Shorthand("c"))
Expand All @@ -442,7 +461,7 @@ func TestFlagSet_Parse(t *testing.T) {
{
name: "parse unsigned integers",
flagSet: func() *FlagSet {
fs := &FlagSet{}
fs := NewFlagSet()
fs.Uint("sample-uint", flag.Shorthand("a"))
fs.Uint8("sample-uint8", flag.Shorthand("b"))
fs.Uint16("sample-uint16", flag.Shorthand("c"))
Expand All @@ -465,7 +484,7 @@ func TestFlagSet_Parse(t *testing.T) {
{
name: "parse integer slices",
flagSet: func() *FlagSet {
fs := &FlagSet{}
fs := NewFlagSet()
fs.IntSlice("sample-int", flag.Shorthand("a"))
fs.Int8Slice("sample-int8", flag.Shorthand("b"))
fs.Int16Slice("sample-int16", flag.Shorthand("c"))
Expand Down Expand Up @@ -495,7 +514,7 @@ func TestFlagSet_Parse(t *testing.T) {
{
name: "parse unsigned integers",
flagSet: func() *FlagSet {
fs := &FlagSet{}
fs := NewFlagSet()
fs.UintSlice("sample-uint", flag.Shorthand("a"))
fs.Uint8Slice("sample-uint8", flag.Shorthand("b"))
fs.Uint16Slice("sample-uint16", flag.Shorthand("c"))
Expand All @@ -518,7 +537,7 @@ func TestFlagSet_Parse(t *testing.T) {
{
name: "parse floats",
flagSet: func() *FlagSet {
fs := &FlagSet{}
fs := NewFlagSet()
fs.Float32("sample-float32", flag.Shorthand("a"))
fs.Float64("sample-float64", flag.Shorthand("b"))
fs.Float32Slice("sample-float32-slice", flag.Shorthand("c"))
Expand All @@ -545,7 +564,7 @@ func TestFlagSet_Parse(t *testing.T) {
{
name: "parse string slice and duration",
flagSet: func() *FlagSet {
fs := &FlagSet{}
fs := NewFlagSet()
fs.Duration("sample-duration", flag.Shorthand("a"))
fs.StringSlice("sample-string", flag.Shorthand("b"), flag.Separator(";"))
fs.DurationSlice("sample-duration-slice", flag.Shorthand("c"), flag.Separator(" "))
Expand All @@ -565,6 +584,49 @@ func TestFlagSet_Parse(t *testing.T) {
"--sample-duration-slice", "1s", "2h",
},
},
{
name: "custom flag no parser error",
flagSet: func() *FlagSet {
fs := NewFlagSet()
CustomFlag[custom](fs, "sample")
return fs
},
expected: expected{
parsed: []result{},
err: true,
expectedErr: ferrors.ErrNoParserDefined,
},
input: []string{"--sample"},
},
{
name: "custom flag no error",
flagSet: func() *FlagSet {
fs := NewFlagSet()
CustomFlag[custom](fs, "sample", flag.CommandLineParser(parser))
return fs
},
expected: expected{
parsed: []result{
{flagName: "sample", flagValue: custom{field: 10}, flagType: "custom"},
},
err: false,
},
input: []string{"--sample", "10"},
},
{
name: "custom flag parse error",
flagSet: func() *FlagSet {
fs := NewFlagSet()
CustomFlag[custom](fs, "sample", flag.CommandLineParser(parser))
return fs
},
expected: expected{
parsed: []result{},
err: true,
expectedErr: ferrors.ErrParseFailed,
},
input: []string{"--sample", "invalid"},
},
}

for _, tc := range tests {
Expand Down

0 comments on commit 5a4bcbe

Please sign in to comment.