Skip to content

Commit

Permalink
command: add flag convenience function
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Aug 31, 2023
1 parent fe1a796 commit 07c7a21
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pkg/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,10 @@ type Command struct {
} `yaml:"completion,omitempty" json:"completion,omitempty" jsonschema_description:"Completion definition"`
Commands []Command `yaml:"commands,omitempty" json:"commands,omitempty" jsonschema_description:"Subcommands of the command"`
}

func (c *Command) AddFlag(f Flag) {
if c.Flags == nil {
c.Flags = make(map[string]string)
}
c.Flags[f.format()] = f.Usage
}
48 changes: 48 additions & 0 deletions pkg/command/flag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package command

type Flag struct {
Longhand string
Shorthand string
Usage string
Repeatable bool
Optarg bool
Value bool
Hidden bool
Required bool
}

func (f Flag) format() string {
var s string

if f.Shorthand != "" {
s += f.Shorthand
if f.Longhand != "" {
s += ", "
}
}

if f.Longhand != "" {
s += f.Longhand
}

switch {
case f.Optarg:
s += "?"
case f.Value:
s += "="
}

if f.Repeatable {
s += "*"
}

if f.Required {
s += "!"
}

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

return s
}

0 comments on commit 07c7a21

Please sign in to comment.