export command fields

This commit is contained in:
gutmet 2020-05-29 10:10:30 +02:00
parent 1a369915ff
commit 29b75b0bb1

View File

@ -217,10 +217,10 @@ type CommandFlagsInit func(s *flag.FlagSet)
type CommandInitExecFunc func() (CommandFlagsInit, CommandFunc) type CommandInitExecFunc func() (CommandFlagsInit, CommandFunc)
type Command struct { type Command struct {
cmd string Cmd string
exec CommandFunc Exec CommandFunc
desc string Desc string
flags *flag.FlagSet Flags *flag.FlagSet
} }
func NewCommandWithFlags(cmd string, f CommandInitExecFunc, desc string) Command { func NewCommandWithFlags(cmd string, f CommandInitExecFunc, desc string) Command {
@ -246,8 +246,8 @@ func usageAndExit(cmds []Command) {
fmt.Println() fmt.Println()
fmt.Println("Possible commands:") fmt.Println("Possible commands:")
for _, cmd := range cmds { for _, cmd := range cmds {
fmt.Println(" " + cmd.cmd) fmt.Println(" " + cmd.Cmd)
fmt.Println(" \t" + cmd.desc) fmt.Println(" \t" + cmd.Desc)
} }
fmt.Println() fmt.Println()
fmt.Println("for detailed information type '" + app + " help COMMAND'") fmt.Println("for detailed information type '" + app + " help COMMAND'")
@ -257,7 +257,7 @@ func usageAndExit(cmds []Command) {
func Execute(possibleCommands []Command) error { func Execute(possibleCommands []Command) error {
commands := commandCollection{} commands := commandCollection{}
for _, c := range possibleCommands { for _, c := range possibleCommands {
commands[c.cmd] = c commands[c.Cmd] = c
} }
arglen := len(os.Args) arglen := len(os.Args)
if arglen <= 1 { if arglen <= 1 {
@ -272,11 +272,11 @@ func Execute(possibleCommands []Command) error {
if !ok { if !ok {
usageAndExit(possibleCommands) usageAndExit(possibleCommands)
} else { } else {
fmt.Println("Description of " + cmd.cmd + ":") fmt.Println("Description of " + cmd.Cmd + ":")
fmt.Println(" " + cmd.desc) fmt.Println(" " + cmd.Desc)
if cmd.flags != nil { if cmd.Flags != nil {
fmt.Println("Possible flags:") fmt.Println("Possible flags:")
cmd.flags.PrintDefaults() cmd.Flags.PrintDefaults()
} else { } else {
fmt.Println("No flags") fmt.Println("No flags")
} }
@ -288,11 +288,11 @@ func Execute(possibleCommands []Command) error {
if !ok { if !ok {
usageAndExit(possibleCommands) usageAndExit(possibleCommands)
} }
if cmd.flags != nil { if cmd.Flags != nil {
cmd.flags.Parse(os.Args[2:]) cmd.Flags.Parse(os.Args[2:])
return cmd.exec(cmd.flags.Args()) return cmd.Exec(cmd.Flags.Args())
} else { } else {
return cmd.exec(os.Args[2:]) return cmd.Exec(os.Args[2:])
} }
} }