feat(diary): boot --help invocations with the console at warn (quick-261028-rqc)
- Options.Args holds the command-line arguments; FromGlobals fills it from os.Args[1:], so the generated main needs no change - Bootstrap and Setup install the setup with Quiet when Args only ask for help (-h, --help) and no -v is given; files keep their levels - TestBootQuietSmoke runs the generated main's sequence with a plugin that logs from Register and Boot
This commit is contained in:
55
modules/diary/helponly.go
Normal file
55
modules/diary/helponly.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package diary
|
||||
|
||||
// helpOnly reports whether args, a binary's command line without the
|
||||
// program name, only ask for help: before "--" they hold --help or a
|
||||
// single-dash cluster that reaches h. It is a tolerant pre-scan of the raw
|
||||
// arguments, made before plugins boot, and reads them the way
|
||||
// bonfire.PeekGlobals does: it stops at "--", skips the value of --env and
|
||||
// reads a single-dash cluster letter by letter, stopping at the first
|
||||
// letter that is not a global shorthand (v, q, n, h). It never fails and
|
||||
// cannot tell a passthrough command (whose --help reaches its Run) from
|
||||
// another, so "<passthrough> --help" counts; the root's OnGlobals hook
|
||||
// (ApplyGlobals) restores the console levels before that command runs.
|
||||
func helpOnly(args []string) bool {
|
||||
for i := 0; i < len(args); i++ {
|
||||
a := args[i]
|
||||
switch {
|
||||
case a == "--":
|
||||
return false
|
||||
case a == "--env":
|
||||
i++
|
||||
case a == "--help":
|
||||
return true
|
||||
case len(a) >= 2 && a[0] == '-' && a[1] != '-':
|
||||
if clusterHasHelp(a[1:]) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// clusterHasHelp reports whether a shorthand cluster (the letters after the
|
||||
// dash) reaches h before a letter that is not a global shorthand.
|
||||
func clusterHasHelp(cluster string) bool {
|
||||
for i := 0; i < len(cluster); i++ {
|
||||
switch cluster[i] {
|
||||
case 'h':
|
||||
return true
|
||||
case 'v', 'q', 'n':
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// bootQuiet applies the help-only rule of Bootstrap and Setup: with Args
|
||||
// set, no -v and arguments that only ask for help, the setup is installed
|
||||
// with Quiet, which raises only the console channels to warn.
|
||||
func bootQuiet(opts Options) Options {
|
||||
if opts.Args != nil && opts.Verbosity == 0 && helpOnly(opts.Args) {
|
||||
opts.Quiet = true
|
||||
}
|
||||
return opts
|
||||
}
|
||||
Reference in New Issue
Block a user