Skip to content

Commit

Permalink
help formatted
Browse files Browse the repository at this point in the history
  • Loading branch information
nikandfor committed Jul 6, 2024
1 parent c354aca commit fe162c1
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 30 deletions.
18 changes: 13 additions & 5 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,13 @@ func (c *Command) MainName() string {

func (c *Command) Command(n string) *Command {
for _, sub := range c.Commands {
if match(sub.Name, n) {
sub.Parent = c

return sub
if sub == nil || !match(sub.Name, n) {
continue
}

sub.Parent = c

return sub
}

return nil
Expand All @@ -140,7 +142,7 @@ func (c *Command) Command(n string) *Command {
func (c *Command) Flag(n string) *Flag {
for q := c; q != nil; q = q.Parent {
for _, f := range q.Flags {
if !match(f.Name, n) {
if f == nil || !match(f.Name, n) {
continue
}

Expand Down Expand Up @@ -361,6 +363,10 @@ func (c *Command) check() (err error) {
}

for _, f := range c.Flags {
if f == nil {
continue
}

if err = flag.CheckFlag(f); err != nil {
return errors.WrapNoCaller(err, f.MainName())
}
Expand All @@ -371,11 +377,13 @@ func (c *Command) check() (err error) {

func match(name, sub string) bool {
ns := strings.Split(name, ",")

for _, sn := range ns {
if sn == sub {
return true
}
}

return false
}

Expand Down
115 changes: 90 additions & 25 deletions help.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cli
import (
"bytes"
"fmt"
"strings"

"nikand.dev/go/cli/flag"
"tlog.app/go/errors"
Expand All @@ -16,6 +17,8 @@ var HelpFlag = &Flag{
}

func defaultHelp(f *Flag, arg string, args []string) (rest []string, err error) {
const minNameW, maxNameW = 20, 40

c := f.CurrentCommand.(*Command)

_, v, rest, err := flag.ParseArg(arg, args, false, true)
Expand All @@ -27,6 +30,38 @@ func defaultHelp(f *Flag, arg string, args []string) (rest []string, err error)

b := new(bytes.Buffer)

pline := func(name, usage, desc string, w int, val interface{}) {
name += usage

fmt.Fprintf(b, " %-*s", w, name)

if len(name) > w {
fmt.Fprintf(b, "\n %-*s", w, "")
}

lines := strings.Split(desc, "\n")

for i, l := range lines {
if i == 0 {
fmt.Fprintf(b, " - ")
} else {
fmt.Fprintf(b, "\n %-*s ", w, "")
}

fmt.Fprintf(b, "%s", l)
}

if val != nil && val != "" {
if len(lines) != 0 && lines[len(lines)-1] == "" {
fmt.Fprintf(b, "default %v", val)
} else {
fmt.Fprintf(b, " (default %v)", val)
}
}

fmt.Fprintf(b, "\n")
}

// full := FullName(c.Parent)
// if full != nil {
// fmt.Fprintf(b, "%s ", strings.Join(full, " "))
Expand Down Expand Up @@ -54,9 +89,15 @@ func defaultHelp(f *Flag, arg string, args []string) (rest []string, err error)

if len(c.Commands) != 0 {
cnt := 0
namew := minNameW

for _, sub := range c.Commands {
if sub.Hidden && !hidden {
if sub == nil {
// spacing
} else if sub.Hidden && !hidden {
continue
} else if w := len(sub.Name) + len(sub.Usage); w > namew {
namew = w
}

cnt++
Expand All @@ -66,26 +107,46 @@ func defaultHelp(f *Flag, arg string, args []string) (rest []string, err error)
fmt.Fprintf(b, "\nSubcommands\n")
}

headernl := false

if namew > maxNameW {
namew = maxNameW
}

for _, sub := range c.Commands {
if sub.Hidden && !hidden {
switch {
case sub == nil:
fmt.Fprintf(b, "\n")
continue
}

fmt.Fprintf(b, " %-20s", sub.Name)
case sub.Name == "":
if headernl {
fmt.Fprintf(b, "\n")
headernl = false
}

if sub.Description != "" {
fmt.Fprintf(b, " - %s", sub.Description)
fmt.Fprintf(b, " %*s # %s\n", namew, "", sub.Description)
continue
case sub.Hidden && !hidden:
continue
}

fmt.Fprintf(b, "\n")
headernl = true

pline(sub.Name, "", sub.Description, namew, nil)
}
}

for cc := c; cc != nil; cc = cc.Parent {
cnt := 0
namew := minNameW

for _, f := range cc.Flags {
if f.Hidden && !hidden || cc != c && f.Local {
if f == nil {
// spacing
} else if f.Hidden && !hidden || cc != c && f.Local {
continue
} else if w := len(f.Name) + len(f.Usage); w > namew {
namew = w
}

cnt++
Expand All @@ -101,28 +162,32 @@ func defaultHelp(f *Flag, arg string, args []string) (rest []string, err error)
fmt.Fprintf(b, "\nFlags of parent command %v\n", cc.MainName())
}

for _, f := range cc.Flags {
if f.Hidden && !hidden || cc != c && f.Local {
continue
}

name := f.Name
headernl := false

if f.Usage != "" {
name += f.Usage
}
if namew > maxNameW {
namew = maxNameW
}

fmt.Fprintf(b, " %-20s", name)
for _, f := range cc.Flags {
switch {
case f == nil:
fmt.Fprintf(b, "\n")
continue
case f.Name == "":
if headernl {
fmt.Fprintf(b, "\n")
headernl = false
}

if f.Description != "" {
fmt.Fprintf(b, " - %s", f.Description)
fmt.Fprintf(b, " %*s # %s\n", namew, "", f.Description)
continue
case f.Hidden && !hidden || cc != c && f.Local:
continue
}

if f.Value != nil && f.Value != "" {
fmt.Fprintf(b, " (default %v)", f.Value)
}
headernl = true

fmt.Fprintf(b, "\n")
pline(f.Name, f.Usage, f.Description, namew, f.Value)
}
}

Expand Down

0 comments on commit fe162c1

Please sign in to comment.