hlctl/cmdlets/cmdlet.go

48 lines
922 B
Go
Raw Normal View History

2023-01-07 16:56:15 +00:00
package cmdlets
import (
"fmt"
"strings"
)
type Usage struct {
Name string
Short string
Long []string
Flags map[string]string
Examples []string
}
func (u Usage) String() string {
str := strings.Builder{}
str.WriteString(
fmt.Sprintf("%s SUBCOMMAND\n\n", strings.ToUpper(u.Name)),
)
str.WriteString(fmt.Sprintf("\t%s\t%s\n\n", u.Name, u.Short))
for _, line := range u.Long {
str.WriteString(fmt.Sprintf("\t%s\n", line))
}
str.WriteRune('\n')
if len(u.Flags) != 0 {
for flag, desc := range u.Flags {
str.WriteString(fmt.Sprintf("\t%s\n\t\t%s\n", flag, desc))
}
str.WriteRune('\n')
}
if len(u.Examples) != 0 {
str.WriteString("EXAMPLES\n\n")
for _, example := range u.Examples {
str.WriteString(fmt.Sprintf("\t%s\n", example))
}
str.WriteRune('\n')
}
return str.String()
}
type Commandlet interface {
Name() string
Usage() Usage
Invoke(args ...string) error
}