Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support -? as an alias for --help #882

Merged
merged 1 commit into from
Jun 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@

flVersion := pflag.Bool("version", false, "print the version and exit")
flHelp := pflag.BoolP("help", "h", false, "print help text and exit")
pflag.BoolVarP(flHelp, "__?", "?", false, "print help text and exit") // support -? as an alias to -h
pflag.CommandLine.MarkHidden("__?")

Check failure on line 145 in main.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `pflag.CommandLine.MarkHidden` is not checked (errcheck)
flManual := pflag.Bool("man", false, "print the full manual and exit")

flVerbose := pflag.IntP("verbose", "v",
Expand Down Expand Up @@ -306,6 +308,23 @@
"DEPRECATED: use --period instead")
mustMarkDeprecated("wait", "use --period instead")

// For whatever reason pflag hardcodes stderr for the "usage" line when
// using the default FlagSet. We tweak the output a bit anyway.
usage := func(out io.Writer, msg string) {
// When pflag parsing hits an error, it prints a message before and
// after the usage, which makes for nice reading.
if msg != "" {
fmt.Fprintln(out, msg)
}
fmt.Fprintln(out, "Usage:")
pflag.CommandLine.SetOutput(out)
pflag.PrintDefaults()
if msg != "" {
fmt.Fprintln(out, msg)
}
}
pflag.Usage = func() { usage(os.Stderr, "") }

//
// Parse and verify flags. Errors here are fatal.
//
Expand All @@ -318,8 +337,7 @@
os.Exit(0)
}
if *flHelp {
pflag.CommandLine.SetOutput(os.Stdout)
pflag.PrintDefaults()
usage(os.Stdout, "")
os.Exit(0)
}
if *flManual {
Expand All @@ -329,7 +347,7 @@

// Make sure we have a root dir in which to work.
if *flRoot == "" {
fmt.Fprintf(os.Stderr, "ERROR: --root must be specified\n")
usage(os.Stderr, "required flag: --root must be specified")
os.Exit(1)
}
var absRoot absPath
Expand Down Expand Up @@ -1029,6 +1047,8 @@
fmt.Fprintln(os.Stderr, s)
if printUsage {
pflag.Usage()
// pflag prints flag errors both before and after usage
fmt.Fprintln(os.Stderr, s)
}
log.ExportError(s)
os.Exit(1)
Expand Down
Loading