cli

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2024 License: ISC Imports: 15 Imported by: 0

Documentation

Overview

Package cli provides utilities for building command-line applications.

Index

Constants

This section is empty.

Variables

View Source
var ErrExitVersion = &unprintableError{errors.New("version flag exit")}

ErrExitVersion is an error indicating the application should exit after showing version.

View Source
var ErrInvalidArgs = errors.New("invalid arguments")

This error should be wrapped with fmt.Errorf to provide a specific, user-friendly message explaining the nature of the invalid arguments.

For example:

return fmt.Errorf("%w: missing required argument 'filename'", cli.ErrInvalidArgs)

Functions

func Main added in v0.6.0

func Main(app App)

Main is a helper function that handles common startup tasks for command-line applications. It sets up signal handling for interrupts, runs the application, and prints errors to stderr.

func Run added in v0.3.0

func Run(ctx context.Context, app App, env *Env) error

Run handles the command-line application startup.

func SetDocComment added in v0.6.0

func SetDocComment(src []byte)

SetDocComment stores the provided byte slice as the source for the application's documentation comment.

The parsing process assumes that the documentation comment is enclosed within a single /* ... */ block and extracts the content line by line. Any other multi-line comments within the embedded file will be ignored.

The parsed documentation will be included in the help message.

Example usage

In application's doc.go:

/*
Amazinator does amazing things...

# Usage

	$ amazinator [flags...]

Amazinator amazes amazinations by amazing your amazinators.
*/
package main

import (
	_ "embed"

	"go.astrophena.name/tools/internal/cli"
)

//go:embed doc.go
var doc []byte

func init() { cli.SetDocComment(doc) }

Types

type App added in v0.3.0

type App interface {
	// Run runs the application.
	Run(context.Context, *Env) error
}

App represents a command-line application.

type AppFunc added in v0.6.0

type AppFunc func(context.Context, *Env) error

AppFunc is a function type that implements the App interface. It has no defined flags.

func (AppFunc) Run added in v0.6.0

func (f AppFunc) Run(ctx context.Context, env *Env) error

Run calls f(ctx, env).

type Env added in v0.6.0

type Env struct {
	Args   []string
	Getenv func(string) string
	Stdin  io.Reader
	Stdout io.Writer
	Stderr io.Writer
	// contains filtered or unexported fields
}

Env represents the application environment.

func OSEnv added in v0.6.0

func OSEnv() *Env

OSEnv returns the current operating system environment.

func (*Env) Logf added in v0.6.0

func (e *Env) Logf(format string, args ...any)

Logf writes the formatted message to standard error of this environment.

type HasFlags added in v0.6.0

type HasFlags interface {
	App

	// Flags adds flags to the flag set.
	Flags(*flag.FlagSet)
}

HasFlags represents a command-line application that has flags.

Directories

Path Synopsis
Package clitest provides utilities for testing command-line applications.
Package clitest provides utilities for testing command-line applications.

Jump to

Keyboard shortcuts

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