zgo.at/zli Index | Examples | Files | Directories

package zli

import "zgo.at/zli"



Package Files

buildinfo_go118.go color.go csi.go flag.go term.go test.go usage.go zli.go


const (
    ColorOffsetFg = 16
    ColorOffsetBg = 40

Offsets where foreground and background colors are stored.

const (
    // UsageTrim removes leading and trailing whitespace and appends a newline.
    // This makes it easier to write usage strings without worrying too much
    // about leading/trailing whitespace, and with the trailing newline it's
    // easy to add a blank line between the usage and any error message
    // (fmt.Println if you wnat a blank line, fmt.Print if you don't).
    UsageTrim = 1

    // UsageHeaders formats headers in the form of:
    //   Header:
    // A header must be at the start of the line, preceded by a blank line, and
    // end with a double colon (:).
    UsageHeaders = 2

    // UsageFlags formats flags in the form of:
    //   -f
    //   -flag
    //   -flag=foo
    //   -flag=[foo]
    UsageFlags = 4

    // UsageProgram replaces "%(prog)" with filepath.Base(os.Args[0]).
    UsageProgram = 8

Formatting flags for Usage.


var (
    // AllowUnknown indicates that unknown flags are not an error; unknown flags
    // are added to the Args list.
    // This is useful if you have subcommands with different flags, for example:
    //     f := zli.NewFlags(os.Args)
    //     globalFlag := f.String(..)
    //     f.Parse(zli.AllowUnknown())
    //     switch cmd := f.ShiftCommand(..) {
    //     case "serve":
    //         serveFlag := f.String(..)
    //         f.Parse()   // *Will* error out on unknown flags.
    //     }
    AllowUnknown = func() parseOpt { return func(o *parseOpts) { o.allowUnknown = true } }

    // Positional sets the lower and upper bounds for the number of positional
    // arguments.
    //   Positional(0, 0)     No limit and accept everything (the default).
    //   Positional(1, 0)     Must have at least one positional argument.
    //   Positional(1, 1)     Must have exactly one positional argument.
    //   Positional(0, 3)     May optionally have up to three positional arguments.
    //   Positional(-1, 0)    Don't accept any conditionals (the max is ignored).only the min is
    Positional = func(min, max int) parseOpt { return func(o *parseOpts) { o.pos = [2]int{min, max} } }

    // NoPositional is a shortcut for Positional(-1, 0)
    NoPositional = func() parseOpt { return func(o *parseOpts) { o.pos = [2]int{-1, -1} } }
var (
    // FormatHeader is the formatting to apply for a header.
    FormatHeader = Bold

    // FormatFlag is the formatting to apply for a flag.
    FormatFlag = Underline
var (
    Exit   func(int) = os.Exit
    Stdin  io.Reader = os.Stdin
    Stdout io.Writer = os.Stdout
    Stderr io.Writer = os.Stderr
var ExitCode = 1

ExitCode is the exit code to use for Fatalf() and F()

var IsTerminal = func(fd uintptr) bool { return term.IsTerminal(int(fd)) }

IsTerminal reports if this file descriptor is an interactive terminal.

TODO: this is a bit tricky now, as we can replace zli.Stdout with something else; checking os.Stdout may not be correct in those cases.

var TerminalSize = func(fd uintptr) (width, height int, err error) { return term.GetSize(int(fd)) }

TerminalSize gets the dimensions of the given terminal.

var WantColor = func() bool {
    _, ok := os.LookupEnv("NO_COLOR")
    return os.Getenv("TERM") != "dumb" && term.IsTerminal(int(os.Stdout.Fd())) && !ok

WantColor indicates if the program should output any colors. This is automatically set from from the output terminal and NO_COLOR environment variable.

You can override this if the user sets "--color=force" or the like.

TODO: maybe expand this a bit with WantMonochrome or some such, so you can still output bold/underline/reverse text for people who don't want colors.

func AskPassword

func AskPassword(minlen int) (string, error)

AskPassword interactively asks the user for a password and confirmation.

Just a convenient wrapper for term.ReadPassword() to call it how you want to use it much of the time to ask for a new password.

func Colorf

func Colorf(format string, c Color, a ...interface{})

Colorf prints colorized output if WantColor is true.

The text will end with the reset code. Note that this is always added at the end, after any newlines in the string.

func Colorize

func Colorize(text string, c Color) string

Colorize the text with a color if WantColor is true.

The text will end with the reset code.

func Colorln

func Colorln(text string, c Color)

Colorln prints colorized output if WantColor is true.

The text will end with the reset code.

func DeColor

func DeColor(text string) string

DeColor removes ANSI color escape sequences from a string.

func EraseLine

func EraseLine()

ErasesLine erases the entire line and puts the cursor at the start of the line.

func Errorf

func Errorf(s interface{}, args ...interface{})

Error prints an error message to stderr prepended with the program name and with a newline appended.

func F

func F(err error)

F prints the err.Error() to stderr with Errorf() and exits, but it won't do anything if the error is nil.

func Fatalf

func Fatalf(s interface{}, args ...interface{})

Fatalf is like Errorf(), but will exit with a code of 1.

func InputOrArgs

func InputOrArgs(args []string, sep string, quiet bool) ([]string, error)

InputOrArgs reads arguments separated by sep from stdin if args is empty, or returns args unmodified if it's not.

The argument are split on newline; the following are all identical:

prog foo bar
printf "foo\nbar\n" | prog

prog 'foo bar' 'x y'
printf "foo bar\nx y\n" | prog

It will print a message to stderr notifying the user it's reading from stdin if the terminal is interactive and quiet is false. See: https://www.arp242.net/read-stdin.html

func InputOrFile

func InputOrFile(path string, quiet bool) (io.ReadCloser, error)

InputOrFile will return a reader connected to stdin if path is "" or "-", or open a path for any other value.

It will print a message to stderr notifying the user it's reading from stdin if the terminal is interactive and quiet is false. See: https://www.arp242.net/read-stdin.html

func Pager

func Pager(text io.Reader)

Pager pipes the content of text to $PAGER, or prints it to stdout of this fails.

func PagerStdout

func PagerStdout() func()

PagerStdout replaces Stdout with a buffer and pipes the content of it to $PAGER.

The typical way to use this is at the start of a function like so:

defer zli.PageStdout()()

You need to be a bit careful when calling Exit() explicitly, since that will exit immediately without running any defered functions. You have to either use a wrapper or call the returned function explicitly.

func PrintVersion

func PrintVersion(verbose bool)

PrintVersion prints this program's version.

If verbose is true it also prints detailed build information. This only works for Go 1.18 or newer.

This assumes that zgo.at/zli.version and zgo.at/zli.progname were set at build time:

go build -ldflags '-X "zgo.at/zli.version=VERSION" -X "zgo.at/zli.progname=PROG"'

func Program

func Program() string

Program gets the program name from argv.

func ReplaceLine

func ReplaceLine(a ...interface{})

ReplaceLine replaces the current line.

func ReplaceLinef

func ReplaceLinef(s string, a ...interface{})

ReplaceLinef replaces the current line.

func Usage

func Usage(opts int, text string) string

Usage applies some formatting to a usage message. See the Usage* constants.

type Color

type Color uint64

Color is a set of attributes to apply; the attributes are stored as follows:

                                     fg true, 256, 16 color mode ─┬──┐
                                  bg true, 256, 16 color mode ─┬─┐│  │
                                                               │ ││  │┌── error parsing hex color
   ┌───── bg color ────────────┐ ┌───── fg color ────────────┐ │ ││  ││┌─ term attr
   v                           v v                           v v vv  vvv         v
0b 0000_0000 0000_0000 0000_0000 0000_0000 0000_0000 0000_0000 0000_0000 0000_0000

The terminal attributes are bold, underline, etc. are stored as flags. The error flag signals there was an error parsing a hex color with ColorHex().

The colors are stored for the background and foreground and are applied depending on the values of the color mode bitmasks.

The biggest advantage of storing it like this is that we can can use a single variable/function parameter to represent all terminal attributes, which IMHO gives a rather nicer API than using a slice or composing the colors with functions or some such.

const (
    Reset Color = 0
    Bold  Color = 1 << (iota - 1)

Basic terminal attributes.

const (
    ColorMode16Fg Color = ColorError << (iota + 1)


Color modes.

const (
    Black Color = (iota << ColorOffsetFg) | ColorMode16Fg

First 8 colors; use Brighten(1) to get the bright variants.

const ColorError Color = CrossedOut << 1

ColorError signals there was an error in parsing a color hex attribute.



zli.Stdout = os.Stdout
zli.Colorln("You're looking rather red", zli.Red) // Apply a color.
zli.Colorln("A bold move", zli.Bold)              // Or an attribute.

zli.Colorln("Tomato", zli.Red.Bg()) // Transform to background color.

zli.Colorln("Wow, such beautiful text", // Can be combined.

zli.Colorln("Contrast ratios is for suckers (and web devs)", // 256 color

zli.Colorln("REAL men use TRUE color!", // True color

zli.Colorf("Hello, %s!\n", zli.Red, "Mars") // Like fmt.Printf

smurf := zli.Colorize("Smurfs!", zli.Blue) // Colorize a string (don't print)

// .String() method outputs escape sequence
fmt.Printf("%sc%so%sl%so%sr%s\n", zli.Red, zli.Magenta, zli.Cyan, zli.Blue, zli.Yellow, zli.Reset)


You're looking rather red
A bold move
Wow, such beautiful text
Contrast ratios is for suckers (and web devs)
REAL men use TRUE color!
Hello, Mars!

func Color256

func Color256(n uint8) Color

Color256 creates a new 256-mode color.

The first 16 (starting at 0) are the same as the color names (Black, Red, etc.) 16 to 231 are various colors. 232 to 255 are greyscale tones.

The 16-231 colors should always be identical on every display (unlike the basic colors, whose exact color codes are undefined and differ per implementation).

See ./cmd/colortest for a little CLI to display the colors.

func ColorHex

func ColorHex(h string) Color

ColorHex gets a 24-bit "true color" from a hex string such as "#f44" or "#ff4444". The leading "#" is optional.

Parsing errors are signaled with by setting the ColorError flag, which String() shows as "(zli.Color ERROR invalid hex color)".

func (Color) Bg

func (c Color) Bg() Color

Bg returns the background variant of this color. If doesn't do anything if this is already a background color.

func (Color) Brighten

func (c Color) Brighten(n int) Color

Brighten or darken (for negative values) a color.

Operations will never go out-of-bounds; brighting the brightest variant will do nothing and will simply return the same color.

For 16 colors it will convert a normal color to a "bright" variant, or vice versa.

For 256 colors it will shift to the same column position in the next "square"; see the chart printed by ./cmd/colortest. The scale of n is 6.

For true colors it will brighten the color; the scale of n is 255.

func (Color) String

func (c Color) String() string

String gets the escape sequence for this color code.

This will always return an empty string if WantColor is false or if the error flag is set.

You can use this to set colors directly with fmt.Print:

fmt.Println(zli.Red|zli.Bold, "red!") // Set colors "directly"; Println() will call String()
fmt.Println("and bold!", zli.Reset)   // Don't forget to reset it!

fmt.Printf("%sc%so%sl%so%sr%s\n", zli.Red, zli.Magenta, zli.Cyan, zli.Blue, zli.Yellow, zli.Reset)

type ErrCommandAmbiguous

type ErrCommandAmbiguous struct {
    Cmd  string
    Opts []string

Sentinel return values for ShiftCommand()

func (ErrCommandAmbiguous) Error

func (e ErrCommandAmbiguous) Error() string

type ErrCommandNoneGiven

type ErrCommandNoneGiven struct{}

Sentinel return values for ShiftCommand()

func (ErrCommandNoneGiven) Error

func (e ErrCommandNoneGiven) Error() string

type ErrCommandUnknown

type ErrCommandUnknown string

Sentinel return values for ShiftCommand()

func (ErrCommandUnknown) Error

func (e ErrCommandUnknown) Error() string

type ErrFlagDouble

type ErrFlagDouble struct {
    // contains filtered or unexported fields

ErrFlagDouble is used when a flag is given more than once.

func (ErrFlagDouble) Error

func (e ErrFlagDouble) Error() string

type ErrFlagInvalid

type ErrFlagInvalid struct {
    // contains filtered or unexported fields

ErrFlagInvalid is used when a flag has an invalid syntax (e.g. "no" for an int flag).

func (ErrFlagInvalid) Error

func (e ErrFlagInvalid) Error() string

func (ErrFlagInvalid) Unwrap

func (e ErrFlagInvalid) Unwrap() error

type ErrFlagUnknown

type ErrFlagUnknown struct {
    // contains filtered or unexported fields

ErrFlagUnknown is used when the flag parsing encounters unknown flags.

func (ErrFlagUnknown) Error

func (e ErrFlagUnknown) Error() string

type ErrPositional

type ErrPositional struct {
    // contains filtered or unexported fields

ErrPositional is used when there are too few or too many positional arguments.

func (ErrPositional) Error

func (e ErrPositional) Error() string

type Flags

type Flags struct {
    Program string   // Program name.
    Args    []string // List of arguments, after parsing this will be reduces to non-flags.
    // contains filtered or unexported fields

Flags are a set of parsed flags.

The rules for parsing are as follows:



// Create new flags from os.Args.
f := zli.NewFlags([]string{"example", "-vv", "-f=csv", "-a", "xx", "yy"})

// Add a string, bool, and "counter" flag.
var (
    verbose = f.IntCounter(0, "v", "verbose")
    all     = f.Bool(false, "a", "all")
    format  = f.String("", "f", "format")

// Shift the first argument (i.e. os.Args[1], if any, empty string if there
// isn't). Useful to get the "subcommand" name. This works before and after
// Parse().
switch f.Shift() {
case "help":
    // Run help
case "install":
    // Run install
case "":
    // Error: need a command (or just print the usage)
    // Error: Unknown command

// Parse the shebang!
err := f.Parse()
if err != nil {
    // Print error, usage.

// You can check if the flag was present on the CLI with Set(). This way you
// can distinguish between "was an empty value passed" // (-format '') and
// "this flag wasn't on the CLI".
if format.Set() {
    fmt.Println("Format was set to", format.String())

// The IntCounter adds 1 for every time the -v flag is on the CLI.
if verbose.Int() > 1 {
    // ...Print very verbose info.
} else if verbose.Int() > 0 {
    // ...Print less verbose info.

// Just a bool!
fmt.Println("All:", all.Bool())

// f.Args is set to everything that's not a flag or argument.
fmt.Println("Remaining:", f.Args)


Format was set to csv
All: true
Remaining: [xx yy]

func NewFlags

func NewFlags(args []string) Flags

NewFlags creates a new Flags from os.Args.

func (*Flags) Bool

func (f *Flags) Bool(def bool, name string, aliases ...string) flagBool

func (*Flags) Float64

func (f *Flags) Float64(def float64, name string, aliases ...string) flagFloat64

func (*Flags) Int

func (f *Flags) Int(def int, name string, aliases ...string) flagInt

func (*Flags) Int64

func (f *Flags) Int64(def int64, name string, aliases ...string) flagInt64

func (*Flags) IntCounter

func (f *Flags) IntCounter(def int, name string, aliases ...string) flagIntCounter

func (*Flags) IntList

func (f *Flags) IntList(def []int, name string, aliases ...string) flagIntList

func (*Flags) Optional

func (f *Flags) Optional() *Flags

Optional indicates the next flag may optionally have value.

By default String(), Int(), etc. require a value, but with Optional() set both "-str" and "-str foo" will work. The default value will be used if "-str" was used.

func (*Flags) Parse

func (f *Flags) Parse(opts ...parseOpt) error

Parse the set of flags in f.Args.

func (*Flags) Shift

func (f *Flags) Shift() string

Shift a value from the argument list.

func (*Flags) ShiftCommand

func (f *Flags) ShiftCommand(cmds ...string) (string, error)

ShiftCommand shifts the first non-flag value from the argument list.

This can work both before or after f.Parse(); this is useful if you want to have different flags for different arguments, and both of these will work:

$ prog -flag cmd
$ prog cmd -flag

If cmds is given then it matches commands with this list; commands can be matched as an abbreviation as long as it's unambiguous; if you have "search" and "identify" then "i", "id", etc. will all return "identify". If you have the commands "search" and "see", then "s" or "se" are ambiguous, and it will return an ErrCommandAmbiguous error.

Commands can also contain aliases as "alias=cmd"; for example "ci=commit".

It will return ErrCommandNoneGiven if there is no command, and ErrCommandUnknown if the command is not found.



f := zli.NewFlags(append([]string{"prog", "i"}))

// Known commands.
commands := []string{"help", "version", "verbose", "install"}

switch cmd, err := f.ShiftCommand(commands...); cmd {
// On error the return value is "" and err is set to something useful; for
// example:
//    % prog
//    prog: no command given
//    % prog hello
//    prog: unknown command: "hello"
//    % prog v
//    prog: ambigious command: "v"; matches: "verbose", "version"
case "":

// The full command is returned, e.g. "prog h" will return "help".
case "help":
    fmt.Println("cmd: help")
case "version":
    fmt.Println("cmd: version")
case "install":
    fmt.Println("cmd: install")


cmd: install

func (*Flags) String

func (f *Flags) String(def, name string, aliases ...string) flagString

func (*Flags) StringList

func (f *Flags) StringList(def []string, name string, aliases ...string) flagStringList

type TestExit

type TestExit int

TestExit records the exit code and aborts the normal program execution. It's intended to test exit codes in a program.

The Exit() method call is a replacement for zli.Exit:

exit := TestExit(-1)
Exit = exit.Exit
defer func() { Exit = os.Exit }()

This can be recovered like so:

func() {
    defer exit.Recover()
    Fatal("oh noes!")
fmt.Println("Exit", exit)

The function wrapper is needed so that the test function itself doesn't get aborted.

TODO: this isn't thread-safe in cases where os.Exit() gets called from a goroutine in the running program.

type TextExit struct {
  mu    *sync.Mutex
  exits []int

func Test

func Test(t *testing.T) (exit *TestExit, in, out *bytes.Buffer)

Test replaces Stdin, Stdout, Stderr, and Exit for testing.

The state will be reset when the test finishes.

The code points to the latest zli.Exit() return code.

func (*TestExit) Exit

func (t *TestExit) Exit(c int)

Exit sets TestExit to the given status code and panics with itself.

func (*TestExit) Recover

func (t *TestExit) Recover()

Recover any panics where the argument is this TestExit instance. it will re-panic on any other errors (including other TestExit instances).

func (*TestExit) Want

func (t *TestExit) Want(tt *testing.T, c int)

Want checks that the recorded exit code matches the given code and issues a t.Error() if it doesn't.


cmd/colortestCommand colortest prints an overview of colors for testing.
cmd/grepCommand grep is a simple grep implementation for demo purposes.
v0.0.0-20220707072716-d3aefb87935e (latest)
Jul 7, 2022
17 packages (graph)
Last checked
1 month ago

Tools for package owners.