Release v0.3.0
This commit is contained in:
commit
160b3321ef
16
command.go
16
command.go
|
@ -9,6 +9,7 @@ import (
|
||||||
|
|
||||||
type Command struct {
|
type Command struct {
|
||||||
Name string
|
Name string
|
||||||
|
version string
|
||||||
ShortDescription string
|
ShortDescription string
|
||||||
LongDescription string
|
LongDescription string
|
||||||
Aliases []string
|
Aliases []string
|
||||||
|
@ -20,8 +21,8 @@ type Command struct {
|
||||||
isRoot bool
|
isRoot bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRoot(name string, options ...Option) *Command {
|
func NewRoot(name string, version string, options ...Option) *Command {
|
||||||
cmd := &Command{Name: name, isRoot: true}
|
cmd := &Command{Name: name, version: version, isRoot: true}
|
||||||
cmd.ApplyOptions(options...)
|
cmd.ApplyOptions(options...)
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
@ -91,6 +92,12 @@ func (c *Command) Execute(args []string) {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
versionOpt, ok := opt.Globals().GetBool("version")
|
||||||
|
if ok && versionOpt.Value() {
|
||||||
|
c.ShowVersion()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
helpOpt, ok := opt.Globals().GetBool("help")
|
helpOpt, ok := opt.Globals().GetBool("help")
|
||||||
if ok && helpOpt.Value() {
|
if ok && helpOpt.Value() {
|
||||||
c.ShowHelp()
|
c.ShowHelp()
|
||||||
|
@ -104,3 +111,8 @@ func (c *Command) Execute(args []string) {
|
||||||
|
|
||||||
c.ShowHelp()
|
c.ShowHelp()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Command) ShowVersion() {
|
||||||
|
rootName := c.Root().Name
|
||||||
|
fmt.Printf("%s %s\n", rootName, c.version)
|
||||||
|
}
|
||||||
|
|
5
help.go
5
help.go
|
@ -2,6 +2,7 @@ package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"go.fifitido.net/cmd/opt"
|
"go.fifitido.net/cmd/opt"
|
||||||
)
|
)
|
||||||
|
@ -43,8 +44,10 @@ func (c *Command) ShowHelp() {
|
||||||
fmt.Println("Available subcommands:")
|
fmt.Println("Available subcommands:")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
paddedWidth := c.Subcommands.MaxWidth()
|
||||||
for _, s := range c.Subcommands {
|
for _, s := range c.Subcommands {
|
||||||
fmt.Println(" " + s.Name + " " + s.ShortDescription)
|
spaceSize := 2 + paddedWidth - len(s.Name)
|
||||||
|
fmt.Println(" " + s.Name + strings.Repeat(" ", spaceSize) + s.ShortDescription)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
2
set.go
2
set.go
|
@ -25,7 +25,7 @@ func (s Set) Get(name string) (*Command, bool) {
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Set) MaxNameWidth() int {
|
func (s Set) MaxWidth() int {
|
||||||
max := 0
|
max := 0
|
||||||
for _, f := range s {
|
for _, f := range s {
|
||||||
if w := len(f.Name); w > max {
|
if w := len(f.Name); w > max {
|
||||||
|
|
Loading…
Reference in New Issue