Skip to content

Commit

Permalink
Change slice type's show.
Browse files Browse the repository at this point in the history
deparcated the slice type prefix'[]'

Signed-off-by: jokemanfire <[email protected]>
  • Loading branch information
jokemanfire committed Jan 13, 2025
1 parent 0cb6639 commit 3043a47
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 36 deletions.
5 changes: 2 additions & 3 deletions flag_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,11 @@ func (f *FlagBase[T, C, V]) GetFlagType() string {
if ty == nil {
return ""
}
// if it is a Slice, then return the slice type. Will nested slices be used in the future?
// if it is a Slice, then return the slice's inner type. Will nested slices be used in the future?
if ty.Kind() == reflect.Slice {
elemType := ty.Elem()
return "[]" + elemType.Name()
return elemType.Name()
}
// TODO. Hashmap is a bit difficult to judge, it will be fixed in the future
return ty.Name()
}

Expand Down
66 changes: 33 additions & 33 deletions flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,12 +459,12 @@ func TestFlagStringifying(t *testing.T) {
{
name: "float64-slice-flag",
fl: &FloatSliceFlag{Name: "pizzas"},
expected: "--pizzas []float64 [ --pizzas []float64 ]\t",
expected: "--pizzas float64 [ --pizzas float64 ]\t",
},
{
name: "float64-slice-flag-with-default-text",
fl: &FloatSliceFlag{Name: "pepperonis", DefaultText: "shaved"},
expected: "--pepperonis []float64 [ --pepperonis []float64 ]\t(default: shaved)",
expected: "--pepperonis float64 [ --pepperonis float64 ]\t(default: shaved)",
},
{
name: "generic-flag",
Expand All @@ -489,7 +489,7 @@ func TestFlagStringifying(t *testing.T) {
{
name: "int-slice-flag",
fl: &IntSliceFlag{Name: "pencils"},
expected: "--pencils []int64 [ --pencils []int64 ]\t",
expected: "--pencils int64 [ --pencils int64 ]\t",
},
{
name: "int-slice-flag-with-default-text",
Expand All @@ -499,7 +499,7 @@ func TestFlagStringifying(t *testing.T) {
{
name: "uint-slice-flag",
fl: &UintSliceFlag{Name: "pencils"},
expected: "--pencils []uint64 [ --pencils []uint64 ]\t",
expected: "--pencils uint64 [ --pencils uint64 ]\t",
},
{
name: "uint-slice-flag-with-default-text",
Expand All @@ -519,12 +519,12 @@ func TestFlagStringifying(t *testing.T) {
{
name: "uint64-slice-flag",
fl: &UintSliceFlag{Name: "drawers"},
expected: "--drawers []uint64 [ --drawers []uint64 ]\t",
expected: "--drawers uint64 [ --drawers uint64 ]\t",
},
{
name: "uint64-slice-flag-with-default-text",
fl: &UintSliceFlag{Name: "handles", DefaultText: "-2"},
expected: "--handles []uint64 [ --handles []uint64 ]\t(default: -2)",
expected: "--handles uint64 [ --handles uint64 ]\t(default: -2)",
},
{
name: "string-flag",
Expand All @@ -539,12 +539,12 @@ func TestFlagStringifying(t *testing.T) {
{
name: "string-slice-flag",
fl: &StringSliceFlag{Name: "meow-sounds"},
expected: "--meow-sounds []string [ --meow-sounds []string ]\t",
expected: "--meow-sounds string [ --meow-sounds string ]\t",
},
{
name: "string-slice-flag-with-default-text",
fl: &StringSliceFlag{Name: "moo-sounds", DefaultText: "awoo"},
expected: "--moo-sounds []string [ --moo-sounds []string ]\t(default: awoo)",
expected: "--moo-sounds string [ --moo-sounds string ]\t(default: awoo)",
},
{
name: "timestamp-flag",
Expand Down Expand Up @@ -724,11 +724,11 @@ var stringSliceFlagTests = []struct {
value []string
expected string
}{
{"foo", nil, []string{}, "--foo []string [ --foo []string ]\t"},
{"f", nil, []string{}, "-f []string [ -f []string ]\t"},
{"f", nil, []string{"Lipstick"}, "-f []string [ -f []string ]\t(default: \"Lipstick\")"},
{"test", nil, []string{"Something"}, "--test []string [ --test []string ]\t(default: \"Something\")"},
{"dee", []string{"d"}, []string{"Inka", "Dinka", "dooo"}, "--dee []string, -d []string [ --dee []string, -d []string ]\t(default: \"Inka\", \"Dinka\", \"dooo\")"},
{"foo", nil, []string{}, "--foo string [ --foo string ]\t"},
{"f", nil, []string{}, "-f string [ -f string ]\t"},
{"f", nil, []string{"Lipstick"}, "-f string [ -f string ]\t(default: \"Lipstick\")"},
{"test", nil, []string{"Something"}, "--test string [ --test string ]\t(default: \"Something\")"},
{"dee", []string{"d"}, []string{"Inka", "Dinka", "dooo"}, "--dee string, -d string [ --dee string, -d string ]\t(default: \"Inka\", \"Dinka\", \"dooo\")"},
}

func TestStringSliceFlagHelpOutput(t *testing.T) {
Expand Down Expand Up @@ -1010,9 +1010,9 @@ var intSliceFlagTests = []struct {
value []int64
expected string
}{
{"heads", nil, []int64{}, "--heads []int64 [ --heads []int64 ]\t"},
{"H", nil, []int64{}, "-H []int64 [ -H []int64 ]\t"},
{"H", []string{"heads"}, []int64{9, 3}, "-H []int64, --heads []int64 [ -H []int64, --heads []int64 ]\t(default: 9, 3)"},
{"heads", nil, []int64{}, "--heads int64 [ --heads int64 ]\t"},
{"H", nil, []int64{}, "-H int64 [ -H int64 ]\t"},
{"H", []string{"heads"}, []int64{9, 3}, "-H int64, --heads int64 [ -H int64, --heads int64 ]\t(default: 9, 3)"},
}

func TestIntSliceFlagHelpOutput(t *testing.T) {
Expand Down Expand Up @@ -1131,13 +1131,13 @@ var uintSliceFlagTests = []struct {
value []uint64
expected string
}{
{"heads", nil, []uint64{}, "--heads []uint64 [ --heads []uint64 ]\t"},
{"H", nil, []uint64{}, "-H []uint64 [ -H []uint64 ]\t"},
{"heads", nil, []uint64{}, "--heads uint64 [ --heads uint64 ]\t"},
{"H", nil, []uint64{}, "-H uint64 [ -H uint64 ]\t"},
{
"heads",
[]string{"H"},
[]uint64{2, 17179869184},
"--heads []uint64, -H []uint64 [ --heads []uint64, -H []uint64 ]\t(default: 2, 17179869184)",
"--heads uint64, -H uint64 [ --heads uint64, -H uint64 ]\t(default: 2, 17179869184)",
},
}

Expand Down Expand Up @@ -1276,13 +1276,13 @@ var uint64SliceFlagTests = []struct {
value []uint64
expected string
}{
{"heads", nil, []uint64{}, "--heads []uint64 [ --heads []uint64 ]\t"},
{"H", nil, []uint64{}, "-H []uint64 [ -H []uint64 ]\t"},
{"heads", nil, []uint64{}, "--heads uint64 [ --heads uint64 ]\t"},
{"H", nil, []uint64{}, "-H uint64 [ -H uint64 ]\t"},
{
"heads",
[]string{"H"},
[]uint64{2, 17179869184},
"--heads []uint64, -H []uint64 [ --heads []uint64, -H []uint64 ]\t(default: 2, 17179869184)",
"--heads uint64, -H uint64 [ --heads uint64, -H uint64 ]\t(default: 2, 17179869184)",
},
}

Expand Down Expand Up @@ -1467,13 +1467,13 @@ var float64SliceFlagTests = []struct {
value []float64
expected string
}{
{"heads", nil, []float64{}, "--heads []float64 [ --heads []float64 ]\t"},
{"H", nil, []float64{}, "-H []float64 [ -H []float64 ]\t"},
{"heads", nil, []float64{}, "--heads float64 [ --heads float64 ]\t"},
{"H", nil, []float64{}, "-H float64 [ -H float64 ]\t"},
{
"heads",
[]string{"H"},
[]float64{0.1234, -10.5},
"--heads []float64, -H []float64 [ --heads []float64, -H []float64 ]\t(default: 0.1234, -10.5)",
"--heads float64, -H float64 [ --heads float64, -H float64 ]\t(default: 0.1234, -10.5)",
},
}

Expand Down Expand Up @@ -2664,25 +2664,25 @@ func TestFlagDefaultValue(t *testing.T) {
name: "stringSlice",
flag: &StringSliceFlag{Name: "flag", Value: []string{"default1", "default2"}},
toParse: []string{"--flag", "parsed"},
expect: `--flag []string [ --flag []string ] (default: "default1", "default2")`,
expect: `--flag string [ --flag string ] (default: "default1", "default2")`,
},
{
name: "float64Slice",
flag: &FloatSliceFlag{Name: "flag", Value: []float64{1.1, 2.2}},
toParse: []string{"--flag", "13.3"},
expect: `--flag []float64 [ --flag []float64 ] (default: 1.1, 2.2)`,
expect: `--flag float64 [ --flag float64 ] (default: 1.1, 2.2)`,
},
{
name: "intSlice",
flag: &IntSliceFlag{Name: "flag", Value: []int64{1, 2}},
toParse: []string{"--flag", "13"},
expect: `--flag []int64 [ --flag []int64 ] (default: 1, 2)`,
expect: `--flag int64 [ --flag int64 ] (default: 1, 2)`,
},
{
name: "uintSlice",
flag: &UintSliceFlag{Name: "flag", Value: []uint64{1, 2}},
toParse: []string{"--flag", "13"},
expect: `--flag []uint64 [ --flag []uint64 ] (default: 1, 2)`,
expect: `--flag uint64 [ --flag uint64 ] (default: 1, 2)`,
},
{
name: "string",
Expand Down Expand Up @@ -2737,7 +2737,7 @@ func TestFlagDefaultValueWithEnv(t *testing.T) {
name: "stringSlice",
flag: &StringSliceFlag{Name: "flag", Value: []string{"default1", "default2"}, Sources: EnvVars("ssflag")},
toParse: []string{"--flag", "parsed"},
expect: `--flag []string [ --flag []string ] (default: "default1", "default2")` + withEnvHint([]string{"ssflag"}, ""),
expect: `--flag string [ --flag string ] (default: "default1", "default2")` + withEnvHint([]string{"ssflag"}, ""),
environ: map[string]string{
"ssflag": "some-other-env_value",
},
Expand All @@ -2746,7 +2746,7 @@ func TestFlagDefaultValueWithEnv(t *testing.T) {
name: "float64Slice",
flag: &FloatSliceFlag{Name: "flag", Value: []float64{1.1, 2.2}, Sources: EnvVars("fsflag")},
toParse: []string{"--flag", "13.3"},
expect: `--flag []float64 [ --flag []float64 ] (default: 1.1, 2.2)` + withEnvHint([]string{"fsflag"}, ""),
expect: `--flag float64 [ --flag float64 ] (default: 1.1, 2.2)` + withEnvHint([]string{"fsflag"}, ""),
environ: map[string]string{
"fsflag": "20304.222",
},
Expand All @@ -2755,7 +2755,7 @@ func TestFlagDefaultValueWithEnv(t *testing.T) {
name: "intSlice",
flag: &IntSliceFlag{Name: "flag", Value: []int64{1, 2}, Sources: EnvVars("isflag")},
toParse: []string{"--flag", "13"},
expect: `--flag []int64 [ --flag []int64 ] (default: 1, 2)` + withEnvHint([]string{"isflag"}, ""),
expect: `--flag int64 [ --flag int64 ] (default: 1, 2)` + withEnvHint([]string{"isflag"}, ""),
environ: map[string]string{
"isflag": "101",
},
Expand All @@ -2764,7 +2764,7 @@ func TestFlagDefaultValueWithEnv(t *testing.T) {
name: "uintSlice",
flag: &UintSliceFlag{Name: "flag", Value: []uint64{1, 2}, Sources: EnvVars("uisflag")},
toParse: []string{"--flag", "13"},
expect: `--flag []uint64 [ --flag []uint64 ] (default: 1, 2)` + withEnvHint([]string{"uisflag"}, ""),
expect: `--flag uint64 [ --flag uint64 ] (default: 1, 2)` + withEnvHint([]string{"uisflag"}, ""),
environ: map[string]string{
"uisflag": "3",
},
Expand Down

0 comments on commit 3043a47

Please sign in to comment.