consul/command/flags/usage.go

118 lines
2.5 KiB
Go
Raw Permalink Normal View History

// Copyright (c) HashiCorp, Inc.
[COMPLIANCE] License changes (#18443) * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at <Blog URL>, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * add missing license headers * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
2023-08-11 13:12:13 +00:00
// SPDX-License-Identifier: BUSL-1.1
package flags
import (
"bytes"
"flag"
"fmt"
"io"
"strings"
text "github.com/kr/text"
)
func Usage(txt string, flags *flag.FlagSet) string {
u := &Usager{
Usage: txt,
Flags: flags,
}
return u.String()
}
type Usager struct {
Usage string
Flags *flag.FlagSet
}
func (u *Usager) String() string {
out := new(bytes.Buffer)
out.WriteString(strings.TrimSpace(u.Usage))
out.WriteString("\n")
out.WriteString("\n")
if u.Flags != nil {
f := &HTTPFlags{}
clientFlags := f.ClientFlags()
serverFlags := f.ServerFlags()
var httpFlags, cmdFlags *flag.FlagSet
u.Flags.VisitAll(func(f *flag.Flag) {
if contains(clientFlags, f) || contains(serverFlags, f) {
if httpFlags == nil {
httpFlags = flag.NewFlagSet("", flag.ContinueOnError)
}
httpFlags.Var(f.Value, f.Name, f.Usage)
} else {
if cmdFlags == nil {
cmdFlags = flag.NewFlagSet("", flag.ContinueOnError)
}
cmdFlags.Var(f.Value, f.Name, f.Usage)
}
})
if httpFlags != nil {
printTitle(out, "HTTP API Options")
httpFlags.VisitAll(func(f *flag.Flag) {
printFlag(out, f)
})
}
if cmdFlags != nil {
printTitle(out, "Command Options")
cmdFlags.VisitAll(func(f *flag.Flag) {
printFlag(out, f)
})
}
}
return strings.TrimRight(out.String(), "\n")
}
// printTitle prints a consistently-formatted title to the given writer.
func printTitle(w io.Writer, s string) {
fmt.Fprintf(w, "%s\n\n", s)
}
// printFlag prints a single flag to the given writer.
func printFlag(w io.Writer, f *flag.Flag) {
example, _ := flag.UnquoteUsage(f)
if example != "" {
fmt.Fprintf(w, " -%s=<%s>\n", f.Name, example)
} else {
fmt.Fprintf(w, " -%s\n", f.Name)
}
indented := wrapAtLength(f.Usage, 5)
fmt.Fprintf(w, "%s\n\n", indented)
}
// contains returns true if the given flag is contained in the given flag
// set or false otherwise.
func contains(fs *flag.FlagSet, f *flag.Flag) bool {
if fs == nil {
return false
}
var in bool
fs.VisitAll(func(hf *flag.Flag) {
in = in || f.Name == hf.Name
})
return in
}
// maxLineLength is the maximum width of any line.
const maxLineLength int = 72
// wrapAtLength wraps the given text at the maxLineLength, taking into account
// any provided left padding.
func wrapAtLength(s string, pad int) string {
wrapped := text.Wrap(s, maxLineLength-pad)
lines := strings.Split(wrapped, "\n")
for i, line := range lines {
lines[i] = strings.Repeat(" ", pad) + line
}
return strings.Join(lines, "\n")
}