Better flags help lines
This commit is contained in:
parent
8f98961c46
commit
46a6f8d48e
|
@ -5,8 +5,14 @@ import (
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"go.fifitido.net/cmd"
|
"go.fifitido.net/cmd"
|
||||||
|
"go.fifitido.net/cmd/flags"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var stringFlag = flags.String("string", "s", "", "example string flag")
|
||||||
|
var intFlag = flags.Int("int", "i", 0, "example int flag")
|
||||||
|
var boolFlag = flags.Bool("bool", "b", false, "example bool flag")
|
||||||
|
var floatFlag = flags.Float("float", "f", 0, "example float flag")
|
||||||
|
|
||||||
var root = cmd.NewRoot(
|
var root = cmd.NewRoot(
|
||||||
cmd.WithShortDescription("Example command"),
|
cmd.WithShortDescription("Example command"),
|
||||||
cmd.WithLongDescription(`An example command to show how to use go.fifitido.net/cmd
|
cmd.WithLongDescription(`An example command to show how to use go.fifitido.net/cmd
|
||||||
|
@ -15,6 +21,7 @@ this example is just a simple hello world program to show
|
||||||
the basics of the library.`),
|
the basics of the library.`),
|
||||||
cmd.WithSubcommand(subcmd),
|
cmd.WithSubcommand(subcmd),
|
||||||
cmd.WithArgument("name", false),
|
cmd.WithArgument("name", false),
|
||||||
|
cmd.WithFlags(stringFlag, intFlag, boolFlag, floatFlag),
|
||||||
cmd.WithRunFunc(func(args []string) {
|
cmd.WithRunFunc(func(args []string) {
|
||||||
if len(args) == 0 {
|
if len(args) == 0 {
|
||||||
fmt.Println("Hello World!")
|
fmt.Println("Hello World!")
|
||||||
|
|
|
@ -11,7 +11,7 @@ type BoolFlag struct {
|
||||||
|
|
||||||
var _ Flag = (*StringFlag)(nil)
|
var _ Flag = (*StringFlag)(nil)
|
||||||
|
|
||||||
func NewBoolFlag(name, shortName string, defaultValue bool, description string) *BoolFlag {
|
func Bool(name, shortName string, defaultValue bool, description string) *BoolFlag {
|
||||||
return &BoolFlag{
|
return &BoolFlag{
|
||||||
name: name,
|
name: name,
|
||||||
shortName: shortName,
|
shortName: shortName,
|
||||||
|
|
|
@ -1,8 +1,34 @@
|
||||||
package flags
|
package flags
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
type Flag interface {
|
type Flag interface {
|
||||||
Name() string
|
Name() string
|
||||||
ShortName() string
|
ShortName() string
|
||||||
Description() string
|
Description() string
|
||||||
Parse(raw string) error
|
Parse(raw string) error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Names(f Flag) string {
|
||||||
|
names := ""
|
||||||
|
if f.ShortName() != "" {
|
||||||
|
names += fmt.Sprintf("-%s, ", f.ShortName())
|
||||||
|
}
|
||||||
|
names += fmt.Sprintf("--%s", f.Name())
|
||||||
|
return names
|
||||||
|
}
|
||||||
|
|
||||||
|
func HelpLine(f Flag, paddedWidth int) string {
|
||||||
|
argNames := ""
|
||||||
|
if f.ShortName() != "" {
|
||||||
|
argNames += fmt.Sprintf("-%s, ", f.ShortName())
|
||||||
|
}
|
||||||
|
argNames += fmt.Sprintf("--%s", f.Name())
|
||||||
|
|
||||||
|
spaceSize := paddedWidth - len(argNames)
|
||||||
|
if spaceSize > 0 {
|
||||||
|
argNames += fmt.Sprintf("%*s", spaceSize, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
return argNames + " " + f.Description()
|
||||||
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@ type FloatFlag struct {
|
||||||
|
|
||||||
var _ Flag = (*FloatFlag)(nil)
|
var _ Flag = (*FloatFlag)(nil)
|
||||||
|
|
||||||
func NewFloatFlag(name, shortName string, defaultValue float64, description string) *FloatFlag {
|
func Float(name, shortName string, defaultValue float64, description string) *FloatFlag {
|
||||||
return &FloatFlag{
|
return &FloatFlag{
|
||||||
name: name,
|
name: name,
|
||||||
shortName: shortName,
|
shortName: shortName,
|
||||||
|
|
|
@ -11,7 +11,7 @@ type IntFlag struct {
|
||||||
|
|
||||||
var _ Flag = (*StringFlag)(nil)
|
var _ Flag = (*StringFlag)(nil)
|
||||||
|
|
||||||
func NewIntFlag(name, shortName string, defaultValue int, description string) *IntFlag {
|
func Int(name, shortName string, defaultValue int, description string) *IntFlag {
|
||||||
return &IntFlag{
|
return &IntFlag{
|
||||||
name: name,
|
name: name,
|
||||||
shortName: shortName,
|
shortName: shortName,
|
||||||
|
|
10
flags/set.go
10
flags/set.go
|
@ -23,6 +23,16 @@ func (s Set) Get(name string) (Flag, bool) {
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s Set) MaxWidth() int {
|
||||||
|
max := 0
|
||||||
|
for _, f := range s {
|
||||||
|
if w := len(Names(f)); w > max {
|
||||||
|
max = w
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return max
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Implement
|
// TODO: Implement
|
||||||
func (s Set) Parse(args []string) error {
|
func (s Set) Parse(args []string) error {
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -9,7 +9,7 @@ type StringFlag struct {
|
||||||
|
|
||||||
var _ Flag = (*StringFlag)(nil)
|
var _ Flag = (*StringFlag)(nil)
|
||||||
|
|
||||||
func NewStringFlag(name, shortName, defaultValue, description string) *StringFlag {
|
func String(name, shortName, defaultValue, description string) *StringFlag {
|
||||||
return &StringFlag{
|
return &StringFlag{
|
||||||
name: name,
|
name: name,
|
||||||
shortName: shortName,
|
shortName: shortName,
|
||||||
|
@ -31,7 +31,6 @@ func (f *StringFlag) Name() string {
|
||||||
// ShortName implements Flag.
|
// ShortName implements Flag.
|
||||||
func (f *StringFlag) ShortName() string {
|
func (f *StringFlag) ShortName() string {
|
||||||
return f.shortName
|
return f.shortName
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Value implements Flag.
|
// Value implements Flag.
|
||||||
|
|
7
help.go
7
help.go
|
@ -2,6 +2,8 @@ package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"go.fifitido.net/cmd/flags"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (c *Command) ShowHelp() {
|
func (c *Command) ShowHelp() {
|
||||||
|
@ -39,8 +41,9 @@ func (c *Command) ShowHelp() {
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
fmt.Println("Available flags:")
|
fmt.Println("Available flags:")
|
||||||
|
|
||||||
for _, s := range c.flags {
|
paddedWidth := c.flags.MaxWidth()
|
||||||
fmt.Println(" " + s.Name() + " " + s.Description())
|
for _, f := range c.flags {
|
||||||
|
fmt.Println(" " + flags.HelpLine(f, paddedWidth))
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(" -h, --help Show the help menu")
|
fmt.Println(" -h, --help Show the help menu")
|
||||||
|
|
|
@ -22,7 +22,7 @@ func WithArgument(name string, required bool) Option {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func WithArguments(args []*Argument) Option {
|
func WithArguments(args ...*Argument) Option {
|
||||||
return func(c *Command) {
|
return func(c *Command) {
|
||||||
c.arguments = append(c.arguments, args...)
|
c.arguments = append(c.arguments, args...)
|
||||||
}
|
}
|
||||||
|
@ -34,7 +34,7 @@ func WithFlag(f flags.Flag) Option {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func WithFlags(fs []flags.Flag) Option {
|
func WithFlags(fs ...flags.Flag) Option {
|
||||||
return func(c *Command) {
|
return func(c *Command) {
|
||||||
c.flags = append(c.flags, fs...)
|
c.flags = append(c.flags, fs...)
|
||||||
}
|
}
|
||||||
|
@ -47,7 +47,7 @@ func WithSubcommand(s *Command) Option {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func WithSubcommands(ss []*Command) Option {
|
func WithSubcommands(ss ...*Command) Option {
|
||||||
return func(c *Command) {
|
return func(c *Command) {
|
||||||
c.subcommands = append(c.subcommands, ss...)
|
c.subcommands = append(c.subcommands, ss...)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue