- Global options -v/-vv/-vvv, -q, --ansi/--no-ansi, -n and --env on every command; PeekGlobals reads them before config loads, OnGlobals hooks re-apply them after parsing (diary.ApplyGlobals in the generated main) - Bare binary, --help and list print the Artisan list with the version header from BuildVersion and namespace blocks - Execute runs the root through fang (no version, man page or completions) and returns 0, 1 or 2; Fail renders errors before the root exists - NewRoot rejects duplicate command names (ErrDuplicateCommand) and flags that reuse a global option (ErrReservedFlag) - Generated main and summer use PeekGlobals, compass.Open with --env, diary.FromGlobals and bonfire.Execute; docs checker knows the globals
53 lines
1.2 KiB
Go
53 lines
1.2 KiB
Go
package bonfire
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"strings"
|
|
)
|
|
|
|
// UsageError marks a mistake in how a command was called: an unknown
|
|
// command or flag, or a wrong number of arguments. Execute returns ExitUsage
|
|
// for it.
|
|
type UsageError struct {
|
|
Err error
|
|
}
|
|
|
|
func (e *UsageError) Error() string { return e.Err.Error() }
|
|
|
|
func (e *UsageError) Unwrap() error { return e.Err }
|
|
|
|
// Usage wraps err as a UsageError; nil stays nil.
|
|
func Usage(err error) error {
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
var ue *UsageError
|
|
if errors.As(err, &ue) {
|
|
return err
|
|
}
|
|
return &UsageError{Err: err}
|
|
}
|
|
|
|
// isUsage reports a UsageError in the chain or cobra's unknown command error.
|
|
func isUsage(err error) bool {
|
|
var ue *UsageError
|
|
if errors.As(err, &ue) {
|
|
return true
|
|
}
|
|
return strings.HasPrefix(err.Error(), "unknown command")
|
|
}
|
|
|
|
// RenderError writes err to w as the console error block: a blank line,
|
|
// " ERROR <message>" and a blank line. name is the root name of the binary
|
|
// (summer, or the application binary's name).
|
|
func RenderError(w io.Writer, name string, err error, g Globals) {
|
|
if err == nil || w == nil {
|
|
return
|
|
}
|
|
_ = name
|
|
_ = g
|
|
fmt.Fprintf(w, "\n ERROR %s\n\n", err.Error())
|
|
}
|