flags : tabwriter output

This commit is contained in:
bth 2026-01-18 15:03:31 -08:00
commit c7c48eab43
2 changed files with 8 additions and 4 deletions

View file

@ -2,8 +2,10 @@ package flags
import ( import (
"fmt" "fmt"
"os"
"reflect" "reflect"
"strings" "strings"
"text/tabwriter"
) )
type Arg struct { type Arg struct {
@ -42,16 +44,19 @@ func Help(subcmd string, opts any) {
t := v.Type() t := v.Type()
fmt.Printf("um %s usage:\n", subcmd) fmt.Printf("um %s usage:\n", subcmd)
w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
for i := 0; i < v.NumField(); i++ { for i := 0; i < v.NumField(); i++ {
field := t.Field(i) field := t.Field(i)
value := v.Field(i) value := v.Field(i)
switch value.Interface().(type) { switch value.Interface().(type) {
case Arg: case Arg:
fmt.Printf("%-12v string\t: %v\n", "["+strings.ToLower(field.Name)+"]", value.FieldByName("Help")) fmt.Fprintf(w, "[%v]\tstring\t%v\n", strings.ToLower(field.Name), value.FieldByName("Help"))
case Flag[string]: case Flag[string]:
fmt.Printf("%-12v | %v string\t: %v\n", value.FieldByName("Long"), value.FieldByName("Short"), value.FieldByName("Help")) fmt.Fprintf(w, "%v | %v\tstring\t%v\n", value.FieldByName("Long"), value.FieldByName("Short"), value.FieldByName("Help"))
case Flag[bool]: case Flag[bool]:
fmt.Printf("%-12v | %v bool\t: %v\n", value.FieldByName("Long"), value.FieldByName("Short"), value.FieldByName("Help")) fmt.Fprintf(w, "%v | %v\tbool\t%v\n", value.FieldByName("Long"), value.FieldByName("Short"), value.FieldByName("Help"))
} }
} }
w.Flush()
} }

View file

@ -41,7 +41,6 @@ func ParseArgs(args []string) options {
} }
func Next(args []string) { func Next(args []string) {
fmt.Println("next")
opts := ParseArgs(args) opts := ParseArgs(args)
fmt.Println(opts) fmt.Println(opts)
} }