diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 49ae3bbca..9ed77f930 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -29,7 +29,7 @@ jobs: run: go generate ./... - name: Test - run: go test -v ./... + run: go test -v ./... ./example-nonposix/... - name: "Check formatting" run: '[ "$(gofmt -d -s . | tee -a /dev/stderr)" = "" ]' diff --git a/carapace.go b/carapace.go index de806c1c4..152b6071d 100644 --- a/carapace.go +++ b/carapace.go @@ -87,12 +87,17 @@ func (c Carapace) Standalone() { c.cmd.CompletionOptions = cobra.CompletionOptions{ DisableDefaultCmd: true, } - // TODO probably needs to be done for each subcommand - // TODO still needed? - if c.cmd.Flag("help") != nil { - c.cmd.Flags().Bool("help", false, "skip") - c.cmd.Flag("help").Hidden = true - } + + c.PreRun(func(cmd *cobra.Command, args []string) { + if f := cmd.Flag("help"); f == nil { + cmd.Flags().Bool("help", false, "") + cmd.Flag("help").Hidden = true + } else if f.Annotations != nil { + if _, ok := f.Annotations[cobra.FlagSetByCobraAnnotation]; ok { + cmd.Flag("help").Hidden = true + } + } + }) c.cmd.SetHelpCommand(&cobra.Command{Hidden: true}) } diff --git a/example-nonposix/cmd/root_test.go b/example-nonposix/cmd/root_test.go index d6192ef79..909c5fa94 100644 --- a/example-nonposix/cmd/root_test.go +++ b/example-nonposix/cmd/root_test.go @@ -8,6 +8,17 @@ import ( "github.com/rsteube/carapace/pkg/style" ) +func TestStandalone(t *testing.T) { + sandbox.Package(t, "github.com/rsteube/carapace/example-nonposix")(func(s *sandbox.Sandbox) { + s.Run("--h"). + Expect(carapace.ActionValues(). + NoSpace('.')) + + s.Run("hel"). + Expect(carapace.ActionValues()) + }) +} + func TestRoot(t *testing.T) { sandbox.Package(t, "github.com/rsteube/carapace/example-nonposix")(func(s *sandbox.Sandbox) { s.Run("-delim-colon:"). @@ -70,7 +81,8 @@ func TestNargs(t *testing.T) { s.Run("--nargs-two", "nt1", "nt4", "--nargs-"). Expect(carapace.ActionValuesDescribed( - "--nargs-any", "Nargs"). + "--nargs-any", "Nargs", + "--nargs-two", "Nargs"). Style(style.Magenta). NoSpace('.'). Tag("flags")) diff --git a/internalActions.go b/internalActions.go index 5b5c87456..3bf2ec11f 100644 --- a/internalActions.go +++ b/internalActions.go @@ -85,6 +85,8 @@ func actionFlags(cmd *cobra.Command) Action { vals := make([]string, 0) flagSet.VisitAll(func(f *pflagfork.Flag) { switch { + case f.Hidden: + return // skip hidden flags case f.Deprecated != "": return // skip deprecated flags case f.Changed && !f.IsRepeatable():