move usage to own file

This commit is contained in:
Dan Anglin 2024-05-23 17:39:19 +01:00
parent e7f857cf4f
commit 8a82486005
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
2 changed files with 47 additions and 41 deletions

View file

@ -4,8 +4,6 @@ import (
"flag"
"fmt"
"os"
"slices"
"strings"
"codeflow.dananglin.me.uk/apollo/enbas/internal/executor"
)
@ -46,7 +44,7 @@ func run() error {
commandLogin: "login to an account on GoToSocial",
commandVersion: "print the application's version and build information",
commandShow: "print details about a specified resource",
commandSwitch: "switch to a different account",
commandSwitch: "perform a switch operation (e.g. switch logged in accounts)",
commandCreate: "create a specific resource",
commandDelete: "delete a specific resource",
commandEdit: "edit a specific resource",
@ -178,41 +176,3 @@ func run() error {
return nil
}
func usageFunc(summaries map[string]string) func() {
cmds := make([]string, len(summaries))
ind := 0
for k := range summaries {
cmds[ind] = k
ind++
}
slices.Sort(cmds)
return func() {
var builder strings.Builder
builder.WriteString("SUMMARY:\n enbas - A GoToSocial client for the terminal.\n\n")
if binaryVersion != "" {
builder.WriteString("VERSION:\n " + binaryVersion + "\n\n")
}
builder.WriteString("USAGE:\n enbas [flags]\n enbas [command]\n\nCOMMANDS:")
for _, cmd := range cmds {
fmt.Fprintf(&builder, "\n %s\t%s", cmd, summaries[cmd])
}
builder.WriteString("\n\nFLAGS:\n --help\n print the help message\n")
flag.VisitAll(func(f *flag.Flag) {
fmt.Fprintf(&builder, "\n --%s\n %s\n", f.Name, f.Usage)
})
builder.WriteString("\nUse \"enbas [command] --help\" for more information about a command.\n")
w := flag.CommandLine.Output()
fmt.Fprint(w, builder.String())
}
}

46
cmd/enbas/usage.go Normal file
View file

@ -0,0 +1,46 @@
package main
import (
"flag"
"fmt"
"slices"
"strings"
)
func usageFunc(summaries map[string]string) func() {
cmds := make([]string, len(summaries))
ind := 0
for k := range summaries {
cmds[ind] = k
ind++
}
slices.Sort(cmds)
return func() {
var builder strings.Builder
builder.WriteString("SUMMARY:\n enbas - A GoToSocial client for the terminal.\n\n")
if binaryVersion != "" {
builder.WriteString("VERSION:\n " + binaryVersion + "\n\n")
}
builder.WriteString("USAGE:\n enbas [flags]\n enbas [command]\n\nCOMMANDS:")
for _, cmd := range cmds {
fmt.Fprintf(&builder, "\n %s\t%s", cmd, summaries[cmd])
}
builder.WriteString("\n\nFLAGS:\n --help\n print the help message\n")
flag.VisitAll(func(f *flag.Flag) {
fmt.Fprintf(&builder, "\n --%s\n %s\n", f.Name, f.Usage)
})
builder.WriteString("\nUse \"enbas [command] --help\" for more information about a command.\n")
w := flag.CommandLine.Output()
fmt.Fprint(w, builder.String())
}
}