2023-03-28 19:12:30 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 19:12:30 +00:00
|
|
|
|
2017-10-11 12:51:19 +00:00
|
|
|
package flags
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
|
2018-08-14 19:23:18 +00:00
|
|
|
text "github.com/kr/text"
|
2017-10-11 12:51:19 +00:00
|
|
|
)
|
|
|
|
|
2017-10-17 22:00:01 +00:00
|
|
|
func Usage(txt string, flags *flag.FlagSet) string {
|
2017-10-11 12:51:19 +00:00
|
|
|
u := &Usager{
|
2017-10-17 22:00:01 +00:00
|
|
|
Usage: txt,
|
|
|
|
Flags: flags,
|
2017-10-11 12:51:19 +00:00
|
|
|
}
|
|
|
|
return u.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
type Usager struct {
|
2017-10-17 22:00:01 +00:00
|
|
|
Usage string
|
|
|
|
Flags *flag.FlagSet
|
2017-10-11 12:51:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *Usager) String() string {
|
|
|
|
out := new(bytes.Buffer)
|
|
|
|
out.WriteString(strings.TrimSpace(u.Usage))
|
|
|
|
out.WriteString("\n")
|
|
|
|
out.WriteString("\n")
|
|
|
|
|
2017-10-17 22:00:01 +00:00
|
|
|
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)
|
2017-10-11 12:51:19 +00:00
|
|
|
}
|
|
|
|
})
|
2017-10-17 22:00:01 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
2017-10-11 12:51:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2017-10-17 22:00:01 +00:00
|
|
|
// contains returns true if the given flag is contained in the given flag
|
2017-10-11 12:51:19 +00:00
|
|
|
// set or false otherwise.
|
2017-10-17 22:00:01 +00:00
|
|
|
func contains(fs *flag.FlagSet, f *flag.Flag) bool {
|
2017-10-11 12:51:19 +00:00
|
|
|
if fs == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-10-17 22:00:01 +00:00
|
|
|
var in bool
|
2017-10-11 12:51:19 +00:00
|
|
|
fs.VisitAll(func(hf *flag.Flag) {
|
2017-10-17 22:00:01 +00:00
|
|
|
in = in || f.Name == hf.Name
|
2017-10-11 12:51:19 +00:00
|
|
|
})
|
2017-10-17 22:00:01 +00:00
|
|
|
return in
|
2017-10-11 12:51:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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")
|
|
|
|
}
|