Skip to content

Commit

Permalink
Merge pull request #798 from rsteube/fix-help-suppression
Browse files Browse the repository at this point in the history
standalone: fix suppression of help flag
  • Loading branch information
rsteube authored Jul 6, 2023
2 parents 16079eb + 6f3e64c commit 8f322de
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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)" = "" ]'
Expand Down
17 changes: 11 additions & 6 deletions carapace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
}

Expand Down
14 changes: 13 additions & 1 deletion example-nonposix/cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:").
Expand Down Expand Up @@ -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"))
Expand Down
2 changes: 2 additions & 0 deletions internalActions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down

0 comments on commit 8f322de

Please sign in to comment.