feat(bonfire): global flags, Artisan list and exit codes
- 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
This commit is contained in:
@@ -101,10 +101,12 @@ func (s *site) checkCommands(docs []parsedDoc) ([]Problem, error) {
|
||||
// command. tool is true for summer and `go run ./cmd/summer`, false for
|
||||
// ./bin/{app} and bin/{app}. ok is false when the line is not one of
|
||||
// those programs or when flags leave no command word. Flag handling
|
||||
// matches cobra's stripFlags for a root whose only flag is the bool
|
||||
// --help / -h: "--" ends the search, --help and -h take no value, any
|
||||
// other --name or two-character -x consumes the next token, and a token
|
||||
// that already contains "=" is skipped alone.
|
||||
// matches cobra's stripFlags for the bonfire root and its global options:
|
||||
// "--" ends the search; --help, -h, -v, -vv, -vvv, --verbose, -q, --quiet,
|
||||
// -n, --no-interaction, --ansi and --no-ansi take no value; --env takes
|
||||
// the next token (or --env=X); any other --name or two-character -x
|
||||
// consumes the next token, and a token that already contains "=" is
|
||||
// skipped alone.
|
||||
func commandWord(cmd string) (name string, tool bool, ok bool) {
|
||||
fields := strings.Fields(strings.TrimSpace(cmd))
|
||||
if len(fields) > 0 && fields[0] == "$" {
|
||||
@@ -134,7 +136,7 @@ func commandWord(cmd string) (name string, tool bool, ok bool) {
|
||||
return "", tool, false
|
||||
case strings.HasPrefix(s, "-") && strings.Contains(s, "="):
|
||||
fields = fields[1:]
|
||||
case s == "--help" || s == "-h":
|
||||
case valuelessGlobal(s):
|
||||
fields = fields[1:]
|
||||
case strings.HasPrefix(s, "--") || (strings.HasPrefix(s, "-") && len(s) == 2):
|
||||
if len(fields) < 3 {
|
||||
@@ -150,6 +152,18 @@ func commandWord(cmd string) (name string, tool bool, ok bool) {
|
||||
return "", tool, false
|
||||
}
|
||||
|
||||
// valuelessGlobal reports the root flags that take no value: --help and -h
|
||||
// and bonfire's bool global options. "--env" takes a value and is handled
|
||||
// with the other value flags.
|
||||
func valuelessGlobal(s string) bool {
|
||||
switch s {
|
||||
case "--help", "-h", "-v", "-vv", "-vvv", "--verbose", "-q", "--quiet",
|
||||
"-n", "--no-interaction", "--ansi", "--no-ansi":
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// isAssignment reports a leading VAR=value token: a name of letters,
|
||||
// digits and underscores that does not start with a digit.
|
||||
func isAssignment(tok string) bool {
|
||||
|
||||
@@ -593,6 +593,11 @@ func TestCommandWord(t *testing.T) {
|
||||
{"summer", "", tool, false},
|
||||
{"FOO=1", "", false, false},
|
||||
{"summer --version", "", tool, false},
|
||||
{"bin/acme -v serve", "serve", app, true},
|
||||
{"bin/acme -vvv --env=dev migrate", "migrate", app, true},
|
||||
{"bin/acme --env staging migrate", "migrate", app, true},
|
||||
{"summer -q --no-ansi -n list", "list", tool, true},
|
||||
{"bin/acme --verbose --quiet --no-interaction --ansi help migrate", "help", app, true},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
name, isTool, ok := commandWord(tc.in)
|
||||
|
||||
Reference in New Issue
Block a user