Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions command_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package cli

import (
"context"
"testing"

"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type nestedFlagBlock struct {
Address string `desc:"listen address" mapstructure:"address"`
Port int `desc:"listen port" mapstructure:"port"`
}

type inheritFlagsConfig struct {
Visible string `desc:"a visible flag" mapstructure:"visible"`
Hidden nestedFlagBlock `flag:",hidden" mapstructure:"hidden"`
Deprecated nestedFlagBlock `flag:",deprecated" mapstructure:"deprecated"`
}

// TestNewCommand_NestedFlagsInheritParentTags pins the behaviour consumers
// rely on to keep whole config blocks out of --help: a hidden or deprecated
// tag on a struct field applies to every flag generated from its fields.
func TestNewCommand_NestedFlagsInheritParentTags(t *testing.T) {
t.Parallel()

cmd := NewCommand(&cobra.Command{Use: "test"}, func(context.Context, *inheritFlagsConfig) error { return nil }, &inheritFlagsConfig{})

tests := []struct {
flag string
wantHidden bool
wantDeprecated bool
}{
{flag: "visible"},
{flag: "hidden-address", wantHidden: true},
{flag: "hidden-port", wantHidden: true},
{flag: "deprecated-address", wantDeprecated: true},
{flag: "deprecated-port", wantDeprecated: true},
}
for _, tt := range tests {
t.Run(tt.flag, func(t *testing.T) {
t.Parallel()

f := cmd.Flags().Lookup(tt.flag)
require.NotNil(t, f, "flag --%s was not registered", tt.flag)
assert.Equal(t, tt.wantHidden, f.Hidden, "hidden")
assert.Equal(t, tt.wantDeprecated, f.Deprecated != "", "deprecated")
})
}
}
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ module github.com/dioad/cli

go 1.25.0

replace github.com/urfave/sflags => github.com/patdowney/sflags v0.4.2-patdowney

require (
github.com/dioad/config v0.1.0
github.com/mitchellh/go-homedir v1.1.0
Expand All @@ -12,7 +10,7 @@ require (
github.com/spf13/pflag v1.0.10
github.com/spf13/viper v1.21.0
github.com/stretchr/testify v1.12.1
github.com/urfave/sflags v0.4.1
github.com/urfave/sflags v0.4.2-0.20250718033027-b88b11ddd16a
gopkg.in/natefinch/lumberjack.v2 v2.2.1
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ github.com/mattn/go-isatty v0.0.22 h1:j8l17JJ9i6VGPUFUYoTUKPSgKe/83EYU2zBC7YNKMw
github.com/mattn/go-isatty v0.0.22/go.mod h1:ZXfXG4SQHsB/w3ZeOYbR0PrPwLy+n6xiMrJlRFqopa4=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/patdowney/sflags v0.4.2-patdowney h1:EKBusbuHXXq3+cxQS7dIcOVxxuJq2qXdqvjWB8ZyH18=
github.com/patdowney/sflags v0.4.2-patdowney/go.mod h1:NCIz2mBC+woyrkl88PeiKAuQUKJdEre2Y4at5SreAeU=
github.com/pelletier/go-toml/v2 v2.3.1 h1:MYEvvGnQjeNkRF1qUuGolNtNExTDwct51yp7olPtrEc=
github.com/pelletier/go-toml/v2 v2.3.1/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
Expand All @@ -51,6 +49,8 @@ github.com/stretchr/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWD
github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg=
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
github.com/urfave/sflags v0.4.2-0.20250718033027-b88b11ddd16a h1:+5ZNCBFsB5BpcX/mPAtqlCvSiryv6Ed2D4L4IS5G55Q=
github.com/urfave/sflags v0.4.2-0.20250718033027-b88b11ddd16a/go.mod h1:NCIz2mBC+woyrkl88PeiKAuQUKJdEre2Y4at5SreAeU=
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw=
go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg=
Expand Down