cli

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2023 License: MIT Imports: 8 Imported by: 2

README

CLI

This Go module provides a simple way to register program flags, options, and environment variables.

Documentation

Overview

Package cli provides a simple way to register program flags, options, and environment variables.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Create

func Create(
	name string,
	description string,
	usage string,
	version string,
	options []Opt,
	arguments []Arg,
)

Create a new CLI, parse environment and program flags all at once. This is equivalent to calling New(...).ReadEnv().Parse().

Types

type Arg

type Arg struct {
	// Name of the program argument.
	Name string
	// Desc description the argument.
	Desc string
	// Ptr is a pointer to the value to set. Supported types are string, int,
	// bool, and flag.Value.
	Ptr any
}

type CLI

type CLI struct {
	// contains filtered or unexported fields
}

func New

func New(
	name string,
	description string,
	usage string,
	version string,
	options []Opt,
	arguments []Arg,
) *CLI

New registers environment variables and program flags according to the options and handles creation of a usage func. Make sure to call cli.ReadEnv to read the environment variables and Parse to parse the program flags.

func (*CLI) Parse

func (cli *CLI) Parse() *CLI

Parse reads the program flags into the variables pointed to in the options.

func (*CLI) ReadEnv

func (cli *CLI) ReadEnv() *CLI

ReadEnv reads any non-empty environemnt variables into the values pointed to in the options.

type Opt

type Opt struct {
	// Name of the program flag. If empty, no program flag is registered.
	Name string
	// Desc description of option.
	Desc string
	// Env is an optional environment variable to register.
	Env string
	// Ptr is a pointer to the value to set. Supported types are string, int,
	// bool, and flag.Value. If the value being pointed to is non-zero it will be
	// used as the default value for the option.
	Ptr any
	// DocumentationOnly is set to document environment variables which are not
	// parsed through this package. It is permitted only on Opts where both Name
	// and Ptr fields are unspecified.
	DocumentationOnly bool
	// TypeName is an optional hint to the user what the type of the option is.
	// If unset the type name pointed-to by Ptr will be used.
	TypeName string
	// contains filtered or unexported fields
}

Opt is a program option.

Jump to

Keyboard shortcuts

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