Skip to content

Commit

Permalink
spec: support hidden
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Jul 10, 2023
1 parent 008b117 commit 1dce090
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
16 changes: 16 additions & 0 deletions internal/pflagfork/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/rsteube/carapace/pkg/style"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

Expand Down Expand Up @@ -125,6 +126,13 @@ func (f Flag) Style() string {
}
}

func (f Flag) Required() bool {
if annotation := f.Annotations[cobra.BashCompOneRequiredFlag]; len(annotation) == 1 && annotation[0] == "true" {
return true
}
return false
}

func (f Flag) Definition() string {
var definition string
switch f.Mode() {
Expand All @@ -141,6 +149,14 @@ func (f Flag) Definition() string {
}
}

if f.Hidden {
definition += "&"
}

if f.Required() {
definition += "!"
}

if f.IsRepeatable() {
definition += "*"
}
Expand Down
2 changes: 2 additions & 0 deletions internal/shell/spec/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ type Command struct {
Aliases []string `yaml:"aliases,omitempty"`
Description string `yaml:"description,omitempty"`
Group string `yaml:"group,omitempty"`
Hidden bool `yaml:"hidden,omitempty"`
ExclusiveFlags [][]string `yaml:"exclusiveflags,omitempty"`
Flags map[string]string `yaml:"flags,omitempty"`
PersistentFlags map[string]string `yaml:"persistentflags,omitempty"`
Completion struct {
Expand Down
12 changes: 4 additions & 8 deletions internal/shell/spec/snippet.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,15 @@ func command(cmd *cobra.Command) Command {
Description: cmd.Short,
Aliases: cmd.Aliases,
Group: cmd.GroupID,
Hidden: cmd.Hidden,
Flags: make(map[string]string),
PersistentFlags: make(map[string]string),
Commands: make([]Command, 0),
}

cmd.LocalFlags().VisitAll(func(flag *pflag.Flag) {
if flag.Hidden {
return
}
// TODO mutually exclusive flags

cmd.LocalFlags().VisitAll(func(flag *pflag.Flag) {
if cmd.PersistentFlags().Lookup(flag.Name) != nil {
return
}
Expand All @@ -40,16 +39,13 @@ func command(cmd *cobra.Command) Command {
})

cmd.PersistentFlags().VisitAll(func(flag *pflag.Flag) {
if flag.Hidden {
return
}
f := pflagfork.Flag{Flag: flag}
c.PersistentFlags[f.Definition()] = f.Usage

})

for _, subcmd := range cmd.Commands() {
if !subcmd.Hidden {
if subcmd.Name() != "_carapace" {
c.Commands = append(c.Commands, command(subcmd))
}
}
Expand Down

0 comments on commit 1dce090

Please sign in to comment.