xmain

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2026 License: ISC Imports: 19 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

flag_helpers.go are private functions from pflag/flag.go

Package xmain provides a standard stub for the main of a command handling logging, flags, signals and shutdown.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Main

func Main(run RunFunc)

Types

type ExitError

type ExitError struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
}

func ExitErrorf

func ExitErrorf(code int, msg string, v ...interface{}) ExitError

func (ExitError) Error

func (ee ExitError) Error() string

type Opts

type Opts struct {
	Args  []string
	Flags *pflag.FlagSet
	// contains filtered or unexported fields
}

func NewOpts

func NewOpts(env *xos.Env, args []string) *Opts

func (*Opts) Bool

func (o *Opts) Bool(envKey, flag, shortFlag string, defaultVal bool, usage string) (*bool, error)

func (*Opts) Defaults

func (o *Opts) Defaults() string

Mostly copy pasted pasted from pflag.FlagUsagesWrapped with modifications for env var

func (*Opts) Float64

func (o *Opts) Float64(envKey, flag, shortFlag string, defaultVal float64, usage string) (*float64, error)

func (*Opts) Int64

func (o *Opts) Int64(envKey, flag, shortFlag string, defaultVal int64, usage string) (*int64, error)

func (*Opts) Int64Slice

func (o *Opts) Int64Slice(envKey, flag, shortFlag string, defaultVal []int64, usage string) (*[]int64, error)

func (*Opts) String

func (o *Opts) String(envKey, flag, shortFlag string, defaultVal, usage string) *string

type RunFunc

type RunFunc func(context.Context, *State) error

type State

type State struct {
	Name string

	Stdin  io.Reader
	Stdout io.WriteCloser
	Stderr io.WriteCloser

	Log  *cmdlog.Logger
	Env  *xos.Env
	Opts *Opts

	PWD string
}

func (*State) AbsPath

func (ms *State) AbsPath(fp string) string

AbsPath joins the PWD with fp to give the absolute path to fp.

func (*State) AtomicWritePath

func (ms *State) AtomicWritePath(fp string, p []byte) error

func (*State) HumanPath

func (ms *State) HumanPath(fp string) string

HumanPath makes absolute path fp more suitable for human consumption by replacing $HOME in fp with ~ and making it relative to the current PWD.

func (*State) Main

func (ms *State) Main(ctx context.Context, sigs <-chan os.Signal, run func(context.Context, *State) error) error

func (*State) ReadPath

func (ms *State) ReadPath(fp string) ([]byte, error)

func (*State) WritePath

func (ms *State) WritePath(fp string, p []byte) error

type TestState

type TestState struct {
	Run  func(context.Context, *State) error
	Env  *xos.Env
	Args []string
	PWD  string

	Stdin  io.Reader
	Stdout io.Writer
	Stderr io.Writer
	// contains filtered or unexported fields
}

func (*TestState) Cleanup

func (ts *TestState) Cleanup(tb testing.TB)

func (*TestState) Signal

func (ts *TestState) Signal(ctx context.Context, sig os.Signal) (err error)

func (*TestState) Start

func (ts *TestState) Start(tb testing.TB, ctx context.Context)

func (*TestState) StderrPipe

func (ts *TestState) StderrPipe() (pr io.Reader)

func (*TestState) StdinPipe

func (ts *TestState) StdinPipe() (pw io.WriteCloser)

func (*TestState) StdoutPipe

func (ts *TestState) StdoutPipe() (pr io.Reader)

func (*TestState) Wait

func (ts *TestState) Wait(ctx context.Context) (err error)

type UsageError

type UsageError struct {
	Message string `json:"message"`
}

func UsageErrorf

func UsageErrorf(msg string, v ...interface{}) UsageError

func (UsageError) Error

func (ue UsageError) Error() string

Jump to

Keyboard shortcuts

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