diff --git a/pkg/command/command.go b/pkg/command/command.go index 8c54bf0..d42cf0d 100644 --- a/pkg/command/command.go +++ b/pkg/command/command.go @@ -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 +} diff --git a/pkg/command/flag.go b/pkg/command/flag.go new file mode 100644 index 0000000..982fbf3 --- /dev/null +++ b/pkg/command/flag.go @@ -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 +}