Skip to content

Commit

Permalink
Merge pull request #174 from rsteube/support-required
Browse files Browse the repository at this point in the history
support required flags
  • Loading branch information
rsteube authored Jul 10, 2023
2 parents b698872 + 971b87c commit e4847e9
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 4 deletions.
6 changes: 6 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ func (c Command) addPersistentFlags(cmd *cobra.Command) error {
return err
}
flag.addTo(cmd.PersistentFlags())
if flag.required {
cmd.MarkFlagRequired(flag.longhand)
}
}
return nil
}
Expand All @@ -99,6 +102,9 @@ func (c Command) addFlags(cmd *cobra.Command) error {
return err
}
flag.addTo(cmd.Flags())
if flag.required {
cmd.MarkFlagRequired(flag.longhand)
}
}
return nil
}
Expand Down
5 changes: 4 additions & 1 deletion docs/src/carapace-spec/command/flags.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ flags:
-v=: shorthand with value
--repeatable*: longhand repeatable
-o, --optarg?: shorthand and longhand with optional argument
--hidden&: longhand hidden
--required!: longhand required
```
## Modifiers:
- `=` flag takes an argument
- `*` flag is repeatable
- `?` flag takes an optional argument
- `!` flag is hidden
- `&` flag is hidden
- `!` flag is required

## Non-posix

Expand Down
3 changes: 2 additions & 1 deletion example/example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ commands:
description: hidden command
hidden: true
flags:
--hidden!=: hidden flag
--hidden&=: hidden flag
--required!: required flag
completion:
flag:
hidden: ["first", "second", "third"]
Expand Down
6 changes: 4 additions & 2 deletions flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ type flag struct {
value bool
nonposix bool
hidden bool
required 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 @@ -35,7 +36,8 @@ 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"], "!")
f.hidden = strings.Contains(matches["modifier"], "&")
f.required = strings.Contains(matches["modifier"], "!")

if f.longhand == "" && f.shorthand == "" {
return nil, fmt.Errorf("malformed flag: '%v'", s)
Expand Down
4 changes: 4 additions & 0 deletions scrape.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ func scrape(cmd *cobra.Command, tmpDir string) {
if f.Hidden {
fmt.Fprintf(out, ` %vCmd.Flag("%v").Hidden = true`+"\n", cmdVarName(cmd), f.Name)
}

if annotation := f.Annotations[cobra.BashCompOneRequiredFlag]; len(annotation) == 1 && annotation[0] == "true" {
fmt.Fprintf(out, ` %vCmd.MarkFlagRequired("%v")`+"\n", cmdVarName(cmd), f.Name)
}
})

if cmd.HasParent() {
Expand Down

0 comments on commit e4847e9

Please sign in to comment.