Skip to content

Commit

Permalink
Merge pull request #170 from rsteube/support-hidden
Browse files Browse the repository at this point in the history
support hidden flags and commands
  • Loading branch information
rsteube authored Jul 10, 2023
2 parents ef1afbe + 29c9b7d commit cbe595a
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 4 deletions.
2 changes: 2 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type Command struct {
Aliases []string `json:"aliases,omitempty" jsonschema_description:"Aliases of the command"`
Description string `json:"description,omitempty" jsonschema_description:"Description of the command"`
Group string `json:"group,omitempty" jsonschema_description:"Group of the command"`
Hidden bool `json:"hidden,omitempty" jsonschema_description:"Hidden state of the command"`
Flags map[string]string `json:"flags,omitempty" jsonschema_description:"Flags of the command with their description"`
PersistentFlags map[string]string `json:"persistentflags,omitempty" jsonschema_description:"Persistent flags of the command with their description"`
ExclusiveFlags [][]string `json:"exclusiveflags,omitempty" jsonschema_description:"Flags that are mutually exclusive"`
Expand Down Expand Up @@ -47,6 +48,7 @@ func (c Command) ToCobraE() (*cobra.Command, error) {
Short: c.Description,
GroupID: c.Group,
Args: cobra.ArbitraryArgs,
Hidden: c.Hidden,
Run: func(cmd *cobra.Command, args []string) {},
}
carapace.Gen(cmd).Standalone()
Expand Down
1 change: 1 addition & 0 deletions docs/src/carapace-spec/command.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type Command struct {
Aliases []string
Description string
Group string
Hidden bool
Flags map[string]string
PersistentFlags map[string]string
Completion struct {
Expand Down
1 change: 1 addition & 0 deletions docs/src/carapace-spec/command/flags.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ flags:
- `=` flag takes an argument
- `*` flag is repeatable
- `?` flag takes an optional argument
- `!` flag is hidden

## Non-posix

Expand Down
10 changes: 10 additions & 0 deletions example/example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,13 @@ commands:
- name: sub2
group: group2
description: subcommand2
- name: hidden
description: hidden command
hidden: true
flags:
--hidden!=: hidden flag
completion:
flag:
hidden: ["first", "second", "third"]
positional:
- ["hPos1", "hPos2"]
8 changes: 7 additions & 1 deletion flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ type flag struct {
optarg bool
value bool
nonposix bool
hidden bool
}

func parseFlag(s, usage string) (*flag, error) {
r := regexp.MustCompile(`^(?P<shorthand>-[^-][^ =*?]*)?(, )?(?P<longhand>-[-]?[^ =*?]*)?(?P<modifier>[=*?]*)$`)
r := regexp.MustCompile(`^(?P<shorthand>-[^-][^ =*?!]*)?(, )?(?P<longhand>-[-]?[^ =*?!]*)?(?P<modifier>[=*?!]*)$`)
if !r.MatchString(s) {
return nil, fmt.Errorf("flag syntax invalid: %v", s)
}
Expand All @@ -34,6 +35,7 @@ func parseFlag(s, usage string) (*flag, error) {
f.slice = strings.Contains(matches["modifier"], "*")
f.optarg = strings.Contains(matches["modifier"], "?")
f.value = f.optarg || strings.Contains(matches["modifier"], "=")
f.hidden = strings.Contains(matches["modifier"], "!")

if f.longhand == "" && f.shorthand == "" {
return nil, fmt.Errorf("malformed flag: '%v'", s)
Expand Down Expand Up @@ -134,5 +136,9 @@ func (f flag) addTo(fset *pflag.FlagSet) error {
}
}

if f.hidden {
fs.Lookup(f.longhand).Hidden = f.hidden
}

return nil
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.18

require (
github.com/invopop/jsonschema v0.7.0
github.com/rsteube/carapace v0.37.3
github.com/rsteube/carapace v0.38.1
github.com/spf13/cobra v1.7.0
github.com/spf13/pflag v1.0.5
gopkg.in/yaml.v3 v3.0.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ github.com/invopop/jsonschema v0.7.0 h1:2vgQcBz1n256N+FpX3Jq7Y17AjYt46Ig3zIWyy77
github.com/invopop/jsonschema v0.7.0/go.mod h1:O9uiLokuu0+MGFlyiaqtWxwqJm41/+8Nj0lD7A36YH0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rsteube/carapace v0.37.3 h1:Us0AnzZ0JiWVHmETSX8PBgp2UJCf/O7KhgSKSpokkII=
github.com/rsteube/carapace v0.37.3/go.mod h1:jkLt41Ne2TD2xPuMdX/2O05Smhy8vMgG7O2TYvC0yOc=
github.com/rsteube/carapace v0.38.1 h1:Og5zIVSTfa7LT+z3DUGtWXMWpYyMEIGbL2nCb6UoBvA=
github.com/rsteube/carapace v0.38.1/go.mod h1:jkLt41Ne2TD2xPuMdX/2O05Smhy8vMgG7O2TYvC0yOc=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
Expand Down
14 changes: 14 additions & 0 deletions spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,20 @@ func TestSpecNonposix(t *testing.T) {
}
}

func TestHidden(t *testing.T) {
if out := execute(t, example, "example", ""); strings.Contains(out, "hidden") {
t.Error(out)
}

if out := execute(t, example, "example", "hidden", ""); !strings.Contains(out, "hPos1") {
t.Error(out)
}

if out := execute(t, example, "example", "hidden", "--hidden", ""); !strings.Contains(out, "first") {
t.Error(out)
}
}

func execute(t *testing.T, spec string, args ...string) string {
var stdout, stderr bytes.Buffer
var c Command
Expand Down

0 comments on commit cbe595a

Please sign in to comment.