cmd/opt/option.go

36 lines
679 B
Go
Raw Normal View History

2023-11-12 17:19:34 -05:00
package opt
2023-11-11 19:28:58 -05:00
import "fmt"
type Option interface {
Name() string
ShortName() string
Description() string
Parse(raw string) error
2023-11-12 17:07:38 -05:00
RequiresVal() bool
2023-11-11 19:28:58 -05:00
}
func Names(o Option) string {
names := ""
if o.ShortName() != "" {
names += fmt.Sprintf("-%s, ", o.ShortName())
}
names += fmt.Sprintf("--%s", o.Name())
return names
}
func HelpLine(o Option, paddedWidth int) string {
argNames := ""
if o.ShortName() != "" {
argNames += fmt.Sprintf("-%s, ", o.ShortName())
}
argNames += fmt.Sprintf("--%s", o.Name())
spaceSize := paddedWidth - len(argNames)
if spaceSize > 0 {
argNames += fmt.Sprintf("%*s", spaceSize, "")
}
return argNames + " " + o.Description()
}