Categorygithub.com/coder/serpent
modulepackage
0.8.0
Repository: https://github.com/coder/serpent.git
Documentation: pkg.go.dev

# README

serpent

Go Reference

serpent is a Go CLI configuration framework based on cobra and used by coder/coder. It's designed for large-scale CLIs with dozens of commands and hundreds of options. If you're building a small, self-contained tool, go with cobra.

help example

When compared to cobra, serpent strives for:

  • Better default help output inspired by the Go toolchain
  • Greater flexibility in accepting options that span across multiple sources
  • Composition via middleware
  • Testability (e.g. OS Stdout and Stderr is only available to commands explicitly)

Basic Usage

See example/echo:

package main

import (
	"os"
	"strings"

	"github.com/coder/serpent"
)

func main() {
	var upper bool
	cmd := serpent.Command{
		Use:   "echo <text>",
		Short: "Prints the given text to the console.",
		Options: serpent.OptionSet{
			{
				Name:        "upper",
				Value:       serpent.BoolOf(&upper),
				Flag:        "upper",
				Description: "Prints the text in upper case.",
			},
		},
		Handler: func(inv *serpent.Invocation) error {
			if len(inv.Args) == 0 {
				inv.Stderr.Write([]byte("error: missing text\n"))
				os.Exit(1)
			}

			text := inv.Args[0]
			if upper {
				text = strings.ToUpper(text)
			}

			inv.Stdout.Write([]byte(text))
			return nil
		},
	}

	err := cmd.Invoke().WithOS().Run()
	if err != nil {
		panic(err)
	}
}

Design

This Design section assumes you have a good understanding of how cobra works.

Options

Serpent is designed for high-configurability. To us, that means providing many ways to configure the same value (env, YAML, flags, etc.) and keeping the code clean and testable as you scale the number of options.

Serpent's Option type looks like:

type Option struct {
	Name string
	Flag string
	Env string
	Default string
	Value pflag.Value
	// ...
}

And is used by each Command when passed as an array to the Options field.

More coming...

This README is a stub for now. We'll better explain the design and usage of serpent in the future.

# Packages

No description provided by the author
No description provided by the author

# Functions

No description provided by the author
Chain returns a Handler that first calls middleware in order.
DefaultCompletionHandler is a handler that prints all the subcommands, or all the options that haven't been exhaustively set, if the current word starts with a dash.
DefaultHelpFn returns a function that generates usage (help) output for a given command.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
ParseEnviron returns all environment variables starting with prefix without said prefix.
No description provided by the author
RequireRangeArgs returns a Middleware that requires the number of arguments to be between start and end (inclusive).
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Constants

CompletionModeEnv is a special environment variable that is set when the command is being run in completion mode.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Variables

DiscardValue does nothing but implements the pflag.Value interface.

# Structs

Command describes an executable command.
No description provided by the author
No description provided by the author
Var represents a single environment variable of form NAME=VALUE.
Group describes a hierarchy of groups that an option or command belongs to.
HostPort is a host:port pair.
Invocation represents an instance of a command being executed.
Option is a configuration option for a CLI application.
No description provided by the author
Struct is a special value type that encodes an arbitrary struct.
No description provided by the author
Validator is a wrapper around a pflag.Value that allows for validation of the value after or before it has been set.

# Interfaces

Net abstracts CLI commands interacting with the operating system networking.
NoOptDefValuer describes behavior when no option is passed into the flag.

# Type aliases

Annotations is an arbitrary key-mapping used to extend the Option and Command types.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
HandlerFunc handles an Invocation of a command.
No description provided by the author
MiddlewareFunc returns the next handler in the chain, or nil if there are no more.
OptionSet is a group of options that can be applied to a command.
No description provided by the author
No description provided by the author
StringArray is a slice of strings that implements pflag.Value and pflag.SliceValue.
No description provided by the author
No description provided by the author
YAMLConfigPath is a special value type that encodes a path to a YAML configuration file where options are read from.