ffhelp

package
v4.0.0-alpha.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 20, 2023 License: Apache-2.0 Imports: 11 Imported by: 38

Documentation

Overview

Package ffhelp provides tools to produce help text for flags and commands.

Index

Constants

This section is empty.

Variables

View Source
var DefaultColumns = 120

DefaultColumns is the default value returned by Columns.

View Source
var DefaultLinePrefix = "  "

DefaultLinePrefix is used by Section constructors in this package.

Functions

func Columns

func Columns() int

Columns in the current TTY, or DefaultColumns if the TTY can't be determined.

func FormatFlag

func FormatFlag(f ff.Flag, format string) string

FormatFlag is a simple helper to format a flag in a single motion.

func Rewrap

func Rewrap(s string) string

Rewrap calls RewrapAt with a max width of Columns. If columns is less than 40, the max width will be set to 40. If columns is greater than 180, the max width will be scaled down proportional to its value.

func RewrapAt

func RewrapAt(s string, max int) string

RewrapAt rewraps s at max columns. Each line in s is trimmed of any leading tabs. Single newlines are treated as spaces. Two or more newlines are treated as paragraph breaks.

Types

type Flag

type Flag struct{ ff.Flag }

Flag wraps ff.Flag to implement fmt.Formatter. It's relatively low-level. Most consumers are probably better served by higher-level helpers.

func WrapFlag

func WrapFlag(f ff.Flag) Flag

WrapFlag lets you write ffhelp.WrapFlag(f) instead of ffhelp.Flag{Flag: f}.

func (Flag) Format

func (f Flag) Format(s fmt.State, verb rune)

Format implements fmt.Formatter with support for the following verbs.

VERB   DESCRIPTION                              EXAMPLE
%s     short and long name, comma delimited     "f, foo"
%+s    like %s with hyphen prefixes             "-f, --foo"
%#+s   like %+s with empty short names padded   "    --foo"
%v     like %s with placeholder suffix          "f, foo STR"
%+v    like %+s with placeholder suffix         "-f, --foo STR"
%#+v   like %#+s with placeholder suffix        "    --foo STR"
%n     short name                               "f"
%+n    short name with one-hyphen prefix        "-f"
%l     long name                                "foo"
%+l    long name with two-hyphen prefix         "--foo"
%u     usage text                               "foo parameter"
%k     placeholder                              "STR"
%d     default value                            "bar"

See the tests for more complete examples.

type FlagSpec

type FlagSpec struct {
	Flag  ff.Flag
	Spec  string // "-f, --foo STRING"
	Usage string // "value of foo parameter (default: bar)"
}

FlagSpec represents a single-line help text for an ff.Flag. That line consists of two parts: the spec, which is a fixed-width formatted description of the flag names and placeholder; and the usage, which is a combination of the usage string and the default value (if non-empty).

func MakeFlagSpec

func MakeFlagSpec(f ff.Flag) FlagSpec

MakeFlagSpec produces a FlagSpec from an ff.Flag.

func (FlagSpec) String

func (fs FlagSpec) String() string

String returns a tab-delimited and newline-terminated string containing the spec and the usage. It's intended to be written to a tabwriter.Writer.

type Help

type Help []Section

Help represents help output for a flag set, command, etc.

func Command

func Command(cmd *ff.Command) Help

Command returns a standard Help for the given command.

This function is meant as reasonable default for most users, and as an example. Callers who want different help output should implement their own Help value constructors like this one.

func Flags

func Flags(fs ff.Flags, usage ...string) Help

Flags returns a default Help value representing fs. If usage strings are provided, they're used as lines in a USAGE section, included before any FLAGS sections.

This function is meant as reasonable default for most users, and as an example. Callers who want different help output should implement their own Help value constructors like this one.

func (Help) String

func (h Help) String() string

String implements fmt.Stringer.

func (Help) WriteTo

func (h Help) WriteTo(w io.Writer) (n int64, _ error)

WriteTo implements io.WriterTo.

type Section

type Section struct {
	// Title of the section, typically UPPERCASE.
	Title string

	// Lines in the section. Lines should be formatted for the user device, i.e.
	// long lines should be split into multiple lines. Each line is prefixed
	// with LinePrefix before being rendered.
	Lines []string

	// LinePrefix is prefixed to each line when the section is rendered.
	LinePrefix string

	// LineColumns indicates that each line is a tab-delimited set of fields,
	// and therefore will be rendered in a columnar format via text/tabwriter.
	LineColumns bool
}

Section describes a single block of help text. A section usually begins with an uppercase TITLE, and contains one or more lines of content, which are typically indented by LinePrefix.

func NewFlagsSection

func NewFlagsSection(fs ff.Flags) Section

NewFlagsSection returns a single FLAGS section representing every flag available to fs. Each flag is rendered via FlagSpec.

func NewFlagsSections

func NewFlagsSections(fs ff.Flags) []Section

NewFlagsSections returns FLAGS section(s) representing every flag available to fs. Flags are grouped into sections according to their parent flag set. Each flag is rendered via FlagSpec.

func NewSection

func NewSection(title string, lines ...string) Section

NewSection returns a section with the given title and lines, using DefaultLinePrefix.

func NewSubcommandsSection

func NewSubcommandsSection(subcommands []*ff.Command) Section

NewSubcommandsSection returns a SUBCOMMANDS section containing one line for every subcommand in the slice. Lines consist of the subcommand name and the ShortHelp for that subcommand, in a columnar format.

func NewUntitledSection

func NewUntitledSection(lines ...string) Section

NewUntitledSection returns a section with no title and the given lines, with no line prefix.

func (Section) String

func (s Section) String() string

String returns a multi-line string representing the section.

func (Section) WriteTo

func (s Section) WriteTo(w io.Writer) (n int64, _ error)

WriteTo implements io.WriterTo, always ending with a newline.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL