Skip to content

Commit

Permalink
Feature: Add required flag support
Browse files Browse the repository at this point in the history
  • Loading branch information
dearchap committed Nov 13, 2024
1 parent 3e89178 commit ce14d2a
Show file tree
Hide file tree
Showing 10 changed files with 1,158 additions and 1,126 deletions.
1 change: 1 addition & 0 deletions flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ type Flag struct {
DefValue string // default value (as text); for usage message
Hidden bool
Deprecated bool
Required bool
}
13 changes: 7 additions & 6 deletions gen/gcli/gcli.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ func GenerateTo(src []*sflags.Flag, dst *[]cli.Flag) {
aliases = append(aliases, srcFlag.Short)
}
*dst = append(*dst, &cli.GenericFlag{
Name: name,
EnvVars: []string{srcFlag.EnvName},
Aliases: aliases,
Hidden: srcFlag.Hidden,
Usage: srcFlag.Usage,
Value: srcFlag.Value,
Name: name,
EnvVars: []string{srcFlag.EnvName},
Aliases: aliases,
Hidden: srcFlag.Hidden,
Usage: srcFlag.Usage,
Value: srcFlag.Value,
Required: srcFlag.Required,
})
}
}
Expand Down
10 changes: 7 additions & 3 deletions gen/gcli/gcli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gcli
import (
"errors"
"io"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -12,7 +13,7 @@ import (
)

type cfg1 struct {
StringValue1 string
StringValue1 string `flag:",required"`
StringValue2 string `flag:"string-value-two s"`

CounterValue1 sflags.Counter
Expand Down Expand Up @@ -68,18 +69,21 @@ func TestParse(t *testing.T) {
StringValue1: "string_value1_value",
StringValue2: "",
},
args: []string{},
args: []string{},
expErr2: errors.New("required flag \"string-value1\" not set"),
},
{
name: "Test cfg1 short option",
cfg: &cfg1{
StringValue2: "string_value2_value",
},
expCfg: &cfg1{
StringValue1: "string_value1_value2",
StringValue2: "string_value2_value2",
},
args: []string{
"-s=string_value2_value2",
"--string-value1", "string_value1_value2",
},
},
{
Expand Down Expand Up @@ -142,7 +146,7 @@ func TestParse(t *testing.T) {
err = cliApp.Run(args)
if test.expErr2 != nil {
require.Error(t, err)
require.Equal(t, test.expErr2, err)
require.Equal(t, test.expErr2.Error(), strings.ToLower(err.Error()))
} else {
require.NoError(t, err)
}
Expand Down
1 change: 1 addition & 0 deletions gen/gcli/gcliv3.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func GenerateToV3(src []*sflags.Flag, dst *[]cli.Flag) {
Value: &value{
v: srcFlag.Value,
},
Required: srcFlag.Required,
})
}
}
Expand Down
10 changes: 7 additions & 3 deletions gen/gcli/gcliv3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"io"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -13,7 +14,7 @@ import (
)

type cfg2 struct {
StringValue1 string
StringValue1 string `flag:",required"`
StringValue2 string `flag:"string-value-two s"`

CounterValue1 sflags.Counter
Expand Down Expand Up @@ -69,18 +70,21 @@ func TestParseV3(t *testing.T) {
StringValue1: "string_value1_value",
StringValue2: "",
},
args: []string{},
args: []string{},
expErr2: errors.New("required flag \"string-value1\" not set"),
},
{
name: "Test cfg2 short option",
cfg: &cfg2{
StringValue2: "string_value2_value",
},
expCfg: &cfg2{
StringValue1: "string_value1_value2",
StringValue2: "string_value2_value2",
},
args: []string{
"-s=string_value2_value2",
"--string-value1", "string_value1_value2",
},
},
{
Expand Down Expand Up @@ -143,7 +147,7 @@ func TestParseV3(t *testing.T) {
err = cmd.Run(context.Background(), args)
if test.expErr2 != nil {
require.Error(t, err)
require.Equal(t, test.expErr2, err)
require.Equal(t, test.expErr2.Error(), strings.ToLower(err.Error()))
} else {
require.NoError(t, err)
}
Expand Down
3 changes: 3 additions & 0 deletions gen/gkingpin/gkingpin.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ func GenerateTo(src []*sflags.Flag, dst flagger) {
if srcFlag.Hidden {
flag.Hidden()
}
if srcFlag.Required {
flag.Required()
}
if srcFlag.Short != "" {
r, _ := utf8.DecodeRuneInString(srcFlag.Short)
if r != utf8.RuneError {
Expand Down
7 changes: 5 additions & 2 deletions gen/gkingpin/gkingpin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

type cfg1 struct {
StringValue1 string
StringValue1 string `flag:",required"`
StringValue2 string `flag:"string-value-two s"`

CounterValue1 sflags.Counter
Expand Down Expand Up @@ -67,18 +67,21 @@ func TestParse(t *testing.T) {
StringValue1: "string_value1_value",
StringValue2: "",
},
args: []string{},
args: []string{},
expErr2: errors.New("required flag(s) '--string-value1' not provided"),
},
{
name: "Test cfg1 short option",
cfg: &cfg1{
StringValue2: "string_value2_value",
},
expCfg: &cfg1{
StringValue1: "string_value1_value2",
StringValue2: "string_value2_value2",
},
args: []string{
"-s", "string_value2_value2",
"--string-value1", "string_value1_value2",
},
},
{
Expand Down
2 changes: 1 addition & 1 deletion parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func parseFlagTag(field reflect.StructField, opt opts) *Flag {
}
flag.Hidden = hasOption(flagTags[1:], "hidden")
flag.Deprecated = hasOption(flagTags[1:], "deprecated")

flag.Required = hasOption(flagTags[1:], "required")
}

if opt.prefix != "" && !ignoreFlagPrefix {
Expand Down
15 changes: 15 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func TestParseStruct(t *testing.T) {
Name4 *string
Name5 string `flag:"-"`
name6 string
Name7 int `flag:",required"`

Addr *net.TCPAddr

Expand Down Expand Up @@ -147,6 +148,13 @@ func TestParseStruct(t *testing.T) {
DefValue: "name_value4",
Value: newStringValue(simpleCfg.Name4),
},
{
Name: "name7",
EnvName: "NAME7",
Required: true,
DefValue: "0",
Value: newIntValue(&simpleCfg.Name7),
},
{
Name: "addr",
EnvName: "ADDR",
Expand Down Expand Up @@ -194,6 +202,13 @@ func TestParseStruct(t *testing.T) {
DefValue: "name_value4",
Value: newStringValue(simpleCfg.Name4),
},
{
Name: "name7",
EnvName: "PP|NAME7",
Required: true,
DefValue: "0",
Value: newIntValue(&simpleCfg.Name7),
},
{
Name: "addr",
EnvName: "PP|ADDR",
Expand Down
Loading

0 comments on commit ce14d2a

Please sign in to comment.