all: do not print usage info for all the flags when incorrect command-line flag is passed

This should improve usability for VictoriaMetrics apps that have big number of command-line flags,
i.e. all the apps.
This commit is contained in:
Aliaksandr Valialkin 2020-12-03 21:40:30 +02:00
parent 8cf76d8747
commit bdac2171f1
10 changed files with 78 additions and 21 deletions

View file

@ -23,6 +23,7 @@ import (
"github.com/VictoriaMetrics/VictoriaMetrics/lib/buildinfo"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/cgroup"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/envflag"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/flagutil"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/httpserver"
graphiteserver "github.com/VictoriaMetrics/VictoriaMetrics/lib/ingestserver/graphite"
influxserver "github.com/VictoriaMetrics/VictoriaMetrics/lib/ingestserver/influx"
@ -275,8 +276,5 @@ vmagent collects metrics data via popular data ingestion protocols and routes it
See the docs at https://github.com/VictoriaMetrics/VictoriaMetrics/blob/master/app/vmagent/README.md .
`
f := flag.CommandLine.Output()
fmt.Fprintf(f, "%s\n", s)
flag.PrintDefaults()
flagutil.Usage(s)
}

View file

@ -219,8 +219,5 @@ vmalert processes alerts and recording rules.
See the docs at https://github.com/VictoriaMetrics/VictoriaMetrics/blob/master/app/vmalert/README.md .
`
f := flag.CommandLine.Output()
fmt.Fprintf(f, "%s\n", s)
flag.PrintDefaults()
flagutil.Usage(s)
}

View file

@ -2,7 +2,6 @@ package main
import (
"flag"
"fmt"
"net/http"
"net/http/httputil"
"net/url"
@ -12,6 +11,7 @@ import (
"github.com/VictoriaMetrics/VictoriaMetrics/lib/buildinfo"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/cgroup"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/envflag"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/flagutil"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/httpserver"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/procutil"
@ -100,8 +100,5 @@ vmauth authenticates and authorizes incoming requests and proxies them to Victor
See the docs at https://github.com/VictoriaMetrics/VictoriaMetrics/blob/master/app/vmauth/README.md .
`
f := flag.CommandLine.Output()
fmt.Fprintf(f, "%s\n", s)
flag.PrintDefaults()
flagutil.Usage(s)
}

View file

@ -103,10 +103,7 @@ or local filesystem. Backed up data can be restored with vmrestore.
See the docs at https://github.com/VictoriaMetrics/VictoriaMetrics/blob/master/app/vmbackup/README.md .
`
f := flag.CommandLine.Output()
fmt.Fprintf(f, "%s\n", s)
flag.PrintDefaults()
flagutil.Usage(s)
}
func newSrcFS() (*fslocal.FS, error) {

View file

@ -62,6 +62,7 @@ var (
func main() {
// Write flags and help message to stdout, since it is easier to grep or pipe.
flag.CommandLine.SetOutput(os.Stdout)
flag.Usage = usage
envflag.Parse()
buildinfo.Init()
logger.Init()
@ -257,3 +258,12 @@ var (
return float64(atomic.LoadUint64(&storage.TooLongLabelValues))
})
)
func usage() {
const s = `
vminsert accepts data via popular data ingestion protocols and routes it to vmstorage nodes configured via -storageNode.
See the docs at https://victoriametrics.github.io/Cluster-VictoriaMetrics.html .
`
flagutil.Usage(s)
}

View file

@ -62,10 +62,7 @@ vmrestore restores VictoriaMetrics data from backups made by vmbackup.
See the docs at https://github.com/VictoriaMetrics/VictoriaMetrics/blob/master/app/vmrestore/README.md .
`
f := flag.CommandLine.Output()
fmt.Fprintf(f, "%s\n", s)
flag.PrintDefaults()
flagutil.Usage(s)
}
func newDstFS() (*fslocal.FS, error) {

View file

@ -60,6 +60,7 @@ func getDefaultMaxConcurrentRequests() int {
func main() {
// Write flags and help message to stdout, since it is easier to grep or pipe.
flag.CommandLine.SetOutput(os.Stdout)
flag.Usage = usage
envflag.Parse()
buildinfo.Init()
logger.Init()
@ -547,3 +548,12 @@ var (
alertsRequests = metrics.NewCounter(`vm_http_requests_total{path="/select/{}/prometheus/api/v1/alerts"}`)
metadataRequests = metrics.NewCounter(`vm_http_requests_total{path="/select/{}/prometheus/api/v1/metadata"}`)
)
func usage() {
const s = `
vmselect processes incoming queries by fetching the requested data from vmstorage nodes configured via -storageNode.
See the docs at https://victoriametrics.github.io/Cluster-VictoriaMetrics.html .
`
flagutil.Usage(s)
}

View file

@ -45,6 +45,7 @@ var (
func main() {
// Write flags and help message to stdout, since it is easier to grep or pipe.
flag.CommandLine.SetOutput(os.Stdout)
flag.Usage = usage
envflag.Parse()
buildinfo.Init()
logger.Init()
@ -625,3 +626,12 @@ func jsonResponseError(w http.ResponseWriter, err error) {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, `{"status":"error","msg":%q}`, err)
}
func usage() {
const s = `
vmstorage stores time series data obtained from vminsert and returns the requested data to vmselect.
See the docs at https://victoriametrics.github.io/Cluster-VictoriaMetrics.html .
`
flagutil.Usage(s)
}

View file

@ -5,6 +5,8 @@
* FEATURE: optimize Consul service discovery speed when discovering big number of services. See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/574
* FEATURE: add `label_uppercase(q, label1, ... labelN)` and `label_lowercase(q, label1, ... labelN)` function to [MetricsQL](https://victoriametrics.github.io/MetricsQL.html)
for uppercasing and lowercasing values for the given labels. See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/936
* FEATURE: do not print usage info for all the command-line flags when incorrect command-line flag is passed. Previously it could be hard reading the error message
about incorrect command-line flag because of too big usage info for all the flags.
* BUGFIX: properly parse timestamps in OpenMetrics format - they are exposed as floating-point number in seconds instead of integer milliseconds
unlike in Prometheus exposition format. See [the docs](https://github.com/OpenObservability/OpenMetrics/blob/master/specification/OpenMetrics.md#timestamps).

39
lib/flagutil/usage.go Normal file
View file

@ -0,0 +1,39 @@
package flagutil
import (
"flag"
"fmt"
"os"
"strings"
)
// Usage prints s and optional description for all the flags if -h or -help flag is passed to the app.
func Usage(s string) {
f := flag.CommandLine.Output()
fmt.Fprintf(f, "%s\n", s)
if hasHelpFlag(os.Args[1:]) {
flag.PrintDefaults()
} else {
fmt.Fprintf(f, `Run "%s -help" in order to see the description for all the available flags`+"\n", os.Args[0])
}
}
func hasHelpFlag(args []string) bool {
for _, arg := range args {
if isHelpArg(arg) {
return true
}
}
return false
}
func isHelpArg(arg string) bool {
if !strings.HasPrefix(arg, "-") {
return false
}
arg = arg[1:]
if strings.HasPrefix(arg, "-") {
arg = arg[1:]
}
return arg == "h" || arg == "help"
}