Improve flag handling

1. Don't define the prefork flag
2. Don't expose the prefork-child flag in the flag usage message
pull/542/head
doingodswork 2020-07-04 16:17:27 +02:00
parent c39a12c769
commit a092d0d40a
2 changed files with 79 additions and 10 deletions

4
app.go
View File

@ -256,10 +256,6 @@ func New(settings ...*Settings) *App {
if app.Settings.ErrorHandler == nil {
app.Settings.ErrorHandler = defaultErrorHandler
}
if !app.Settings.Prefork { // Default to -prefork flag if false
app.Settings.Prefork = utils.GetArgument(flagPrefork)
}
// Replace unsafe conversion functions
if app.Settings.Immutable {
getBytes, getString = getBytesImmutable, getStringImmutable

View File

@ -7,6 +7,7 @@ import (
"net"
"os"
"os/exec"
"reflect"
"runtime"
"strconv"
"strings"
@ -16,16 +17,16 @@ import (
)
var (
flagPrefork = "-prefork"
flagChild = "-prefork-child"
isPrefork bool
isChild bool
flagChild = "-prefork-child"
isChild bool
)
func init() { //nolint:gochecknoinits
// Avoid panic when the user adds their own flags and runs `flag.Parse()`
flag.BoolVar(&isPrefork, flagPrefork[1:], false, "Prefork")
// Prevent users from defining the same flag on their own.
flag.BoolVar(&isChild, flagChild[1:], false, "Child Process")
// Change the default usage message so the child flag isn't exposed to users of the app
// when for example running `app -help`.
flag.Usage = usage
}
// prefork manages child processes to make use of the OS REUSEPORT or REUSEADDR feature
@ -98,3 +99,75 @@ func (app *App) prefork(addr string, tlsconfig ...*tls.Config) (err error) {
return
}
// -- string Value
// This code is copied from the stdlib.
type stringValue string
// This code is copied from the stdlib.
func (s *stringValue) Set(val string) error {
*s = stringValue(val)
return nil
}
// This code is copied from the stdlib.
func (s *stringValue) Get() interface{} { return string(*s) }
// This code is copied from the stdlib.
func (s *stringValue) String() string { return string(*s) }
// usage prints a usage message documenting all defined command-line flags,
// but skips printing our `-prefork-child` flag as it shouldn't be exposed.
// This code is based on the stdlib with the only change to skip that flag.
func usage() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args[0])
flag.CommandLine.VisitAll(func(f *flag.Flag) {
// Skip printing usage info for our `-prefork-child` flag
if f.Name == flagChild[1:] {
return
}
s := fmt.Sprintf(" -%s", f.Name) // Two spaces before -; see next two comments.
name, usage := flag.UnquoteUsage(f)
if len(name) > 0 {
s += " " + name
}
// Boolean flags of one ASCII letter are so common we
// treat them specially, putting their usage on the same line.
if len(s) <= 4 { // space, space, '-', 'x'.
s += "\t"
} else {
// Four spaces before the tab triggers good alignment
// for both 4- and 8-space tab stops.
s += "\n \t"
}
s += strings.ReplaceAll(usage, "\n", "\n \t")
if !isZeroValue(f, f.DefValue) {
if _, ok := f.Value.(*stringValue); ok {
// put quotes on the value
s += fmt.Sprintf(" (default %q)", f.DefValue)
} else {
s += fmt.Sprintf(" (default %v)", f.DefValue)
}
}
fmt.Fprint(flag.CommandLine.Output(), s, "\n")
})
}
// isZeroValue determines whether the string represents the zero
// value for a flag.
// This code is copied from the stdlib.
func isZeroValue(f *flag.Flag, value string) bool {
// Build a zero value of the flag's Value type, and see if the
// result of calling its String method equals the value passed in.
// This works unless the Value type is itself an interface type.
typ := reflect.TypeOf(f.Value)
var z reflect.Value
if typ.Kind() == reflect.Ptr {
z = reflect.New(typ.Elem())
} else {
z = reflect.Zero(typ)
}
return value == z.Interface().(flag.Value).String()
}