From a2c344de83ecc16be65f6b3ee5a6c08c16310824 Mon Sep 17 00:00:00 2001 From: Harvey Tindall Date: Sat, 24 Apr 2021 23:54:56 +0100 Subject: [PATCH] add shorthand flag names along with an ugly wrapper for the help message that merges the descriptions for the short & long versions. --- Makefile | 1 - go.mod | 1 + go.sum | 2 ++ help.go | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 11 +++++++ 5 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 help.go diff --git a/Makefile b/Makefile index d9ffd8c..2c33077 100644 --- a/Makefile +++ b/Makefile @@ -116,7 +116,6 @@ clean: -rm -r $(DATA) -rm -r build -rm mail/*.html - -rm embed.go -rm docs/docs.go docs/swagger.json docs/swagger.yaml go clean diff --git a/go.mod b/go.mod index 2839507..c30f1fa 100644 --- a/go.mod +++ b/go.mod @@ -33,6 +33,7 @@ require ( github.com/mailru/easyjson v0.7.7 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/smartystreets/goconvey v1.6.4 // indirect + github.com/spf13/pflag v1.0.5 // indirect github.com/swaggo/files v0.0.0-20190704085106-630677cd5c14 github.com/swaggo/gin-swagger v1.3.0 github.com/swaggo/swag v1.7.0 // indirect diff --git a/go.sum b/go.sum index 9134854..6a79883 100644 --- a/go.sum +++ b/go.sum @@ -197,6 +197,8 @@ github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykE github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s= github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.2.0 h1:Hbg2NidpLE8veEBkEZTL3CvlkUIVzuU9jDplZO54c48= github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= diff --git a/help.go b/help.go new file mode 100644 index 0000000..974d371 --- /dev/null +++ b/help.go @@ -0,0 +1,88 @@ +package main + +import ( + "bufio" + "bytes" + "flag" + "fmt" + "os" + "strings" +) + +/* Adds start/stop/systemd to help message, and +also gets rid of usage for shorthand flags, and merge them with the full-length one. +implementation is 🤢, will clean this up eventually. + -h SHORTHAND + -help + prints this message. +becomes: + -help, -h + prints this message. +*/ +func helpFunc() { + fmt.Fprint(os.Stderr, `Usage of jfa-go: + start + start jfa-go as a daemon and run in the background. + stop + stop a daemonized instance of jfa-go. + systemd + generate a systemd .service file. +`) + shortHands := []string{"-help", "-data", "-config", "-port"} + var b bytes.Buffer + // Write defaults into buffer then remove any shorthands + flag.CommandLine.SetOutput(&b) + flag.PrintDefaults() + flag.CommandLine.SetOutput(os.Stderr) + scanner := bufio.NewScanner(&b) + out := "" + line := scanner.Text() + eof := !scanner.Scan() + lastLine := false + for !eof || lastLine { + nextline := scanner.Text() + start := 0 + if len(nextline) != 0 { + for nextline[start] == ' ' && start < len(nextline) { + start++ + } + } + if strings.Contains(line, "SHORTHAND") || (len(nextline) != 0 && strings.Contains(nextline, "SHORTHAND") && nextline[start] != '-') { + line = nextline + if lastLine { + break + } + eof := !scanner.Scan() + if eof { + lastLine = true + } + continue + } + // if !strings.Contains(line, "SHORTHAND") && !(strings.Contains(nextline, "SHORTHAND") && !strings.Contains(nextline, "-")) { + match := false + for i, c := range line { + if c != '-' { + continue + } + for _, s := range shortHands { + if i+len(s) <= len(line) && line[i:i+len(s)] == s { + out += line[:i+len(s)] + ", " + s[:2] + line[i+len(s):] + "\n" + match = true + break + } + } + } + if !match { + out += line + "\n" + } + line = nextline + if lastLine { + break + } + eof := !scanner.Scan() + if eof { + lastLine = true + } + } + fmt.Fprint(os.Stderr, out) +} diff --git a/main.go b/main.go index 5faeeea..3ec8a01 100644 --- a/main.go +++ b/main.go @@ -169,15 +169,26 @@ func start(asDaemon, firstCall bool) { app.err = NewLogger(os.Stdout, "[ERROR] ", log.Ltime, color.FgRed) if firstCall { + flag.Usage = helpFunc + help := flag.Bool("help", false, "prints this message.") + flag.BoolVar(help, "h", false, "SHORTHAND") + DATA = flag.String("data", app.dataPath, "alternate path to data directory.") + flag.StringVar(DATA, "d", app.dataPath, "SHORTHAND") CONFIG = flag.String("config", app.configPath, "alternate path to config file.") + flag.StringVar(CONFIG, "c", app.configPath, "SHORTHAND") HOST = flag.String("host", "", "alternate address to host web ui on.") PORT = flag.Int("port", 0, "alternate port to host web ui on.") + flag.IntVar(PORT, "p", 0, "SHORTHAND") DEBUG = flag.Bool("debug", false, "Enables debug logging.") PPROF = flag.Bool("pprof", false, "Exposes pprof profiler on /debug/pprof.") SWAGGER = flag.Bool("swagger", false, "Enable swagger at /swagger/index.html") flag.Parse() + if *help { + flag.Usage() + os.Exit(0) + } if *SWAGGER { os.Setenv("SWAGGER", "1") }