opt

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2024 License: MIT Imports: 6 Imported by: 0

README

Simple, Opinionated, Options

API Documentation can be found at https://pkg.go.dev/github.com/a-jentlemane/opt?tab=doc

Go Reference

Documentation

Overview

Package opt is a simple, opinionated, options wrapper for pflag & cobra.

It aims to be a simpler alternative to viper.

By default, all options can be specified as:

  1. Default values (lowest precedent)
  2. Environment variables
  3. Command-line flags (highest precedent)

All commands have a name, which is used to set default values.

The default environment variable name is the option's name, but in upper case (see strings.ToUpper). To disallow an option from being specified as an environment variable, override it environment variable name to be "" using EnvName.

The default command-line flag name is the option's name. By default, flags are added to the cobra.Command's normal cobra.FlagSet. Use FlagIsPersistent to add it to the persistent cobra.FlagSet instead. To disallow an option from being specified as a command-line flag, override its flag name to be "" using FlagName.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bool added in v0.0.3

func Bool(cmd *cobra.Command, v *bool, name string, optFuncs ...OptFunc[bool])

Bool sets up a boolean option with the specified name

func String added in v0.0.3

func String(cmd *cobra.Command, v *string, name string, optFuncs ...OptFunc[string])

String sets up a string option with the specified name

Types

type OptFunc added in v0.0.3

type OptFunc[T OptType] func(*opt[T])

func Default added in v0.0.3

func Default[T OptType](v T) OptFunc[T]

Default sets the default value for this option.

func EnvName added in v0.0.3

func EnvName[T OptType](envName string) OptFunc[T]

EnvName overrides the default environment variable name.

As stated in the package documentation, if the environment name is not overriden, it defaults to an upper-cased version of the option's name.

To disable environment lookup functionlity entirely for this option, override the environment name to be "".

func FlagIsPersistent added in v0.0.3

func FlagIsPersistent[T OptType]() OptFunc[T]

FlagIsPersistent specifies that the flag should be added to the cobra.Command's persistent pflag.FlagSet.

func FlagName added in v0.0.3

func FlagName[T OptType](flagName string) OptFunc[T]

FlagName overrides the default flag name. As stated in the package documentation, if the flag name is not overriden, it defaults to the option's name.

To disable flag functionlity entirely for this option, override the flag name to be "".

func FlagShorthand added in v0.0.3

func FlagShorthand[T OptType](flagShorthand string) OptFunc[T]

FlagShorthand specifies a shorthand that can be used for the flag.

Setting it to "" does nothing.

The shorthand is only applicable when the flag is not disabled. See FlagName's documentation for how to disable flags.

func IsDirname added in v0.0.3

func IsDirname() OptFunc[string]

IsDirname specifies that the option's value should be a directory name. This is only used with shell auto-completion; the caller must validate the actual value before use.

func IsFilename added in v0.0.3

func IsFilename() OptFunc[string]

IsFilename specifies that the option's value should be a file name. This is only used with shell auto-completion; the caller must validate the actual value before use.

type OptType

type OptType interface {
	string | bool
}

Jump to

Keyboard shortcuts

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