spruce/internal/cmd/cmd.go
Dan Anglin 90638f5569
fix: customised usage messages
Add functionality to display the default help message when running
spruce without any arguments or when the help flag is used.

Customise the help message for the subcommands.

Additional changes:

- Refactor: move the Runner interface to the internal cmd package
- Fix: Add a summary for each of the subcommands.
- Refactor: Use string builder to replace string literals.
- Perf: Use a switch statement to only create the subcommand that the
  user calls.
2023-08-13 17:45:33 +01:00

51 lines
927 B
Go

package cmd
import (
"flag"
"fmt"
"strings"
)
type Runner interface {
Parse([]string) error
Name() string
Run() error
}
func SpruceUsage() {
usage := `A tool for building CVs
Usage:
spruce [flags]
spruce [command]
Available Commands:
create create a new CV JSON file
generate generate a PDF file from an existing CV JSON file
version print the application's build information
Flags:
-h, --help
print the help message for spruce
Use "spruce [command] --help" for more information about a command.
`
fmt.Print(usage)
}
func usageFunc(name, summary string, flagset *flag.FlagSet) func() {
return func() {
var b strings.Builder
w := flag.CommandLine.Output()
fmt.Fprintf(&b, "%s\n\nUsage:\n spruce %s [flags]\n\nFlags:", summary, name)
flagset.VisitAll(func(f *flag.Flag) {
fmt.Fprintf(&b, "\n --%s\n %s\n", f.Name, f.Usage)
})
fmt.Fprint(w, b.String())
}
}