whiteflag

package module
v1.2.7 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2020 License: MIT Imports: 6 Imported by: 8

README

whiteflag

Tests GitHub tag (latest SemVer) GitHub go.mod Go version PkgGoDev Go Report Card

Whiteflag Gopher

A sane flag-package for gophers who just need some CLI flags in Golang projects, not command-structuring frameworks for space ships. If you waved a white flag on the usual whoppers, whiteflag is here to assist.

Features

  • simply provides FlagPresent() to check for specified flags, and GetBool|Int|String() to access their values (these functions can be utilized directly without further setup of each flag)
  • allows you to distinguish between absent and zero-valued flags
  • -h/--help prints basic generated Usage/Help text (see examples)
  • Default values for flags can be specified
  • Required flags can be achieved implicitly (see examples)

Examples

Please have a look at the comprehensive example source file.

Basic

The following snippet would print "gopher" when called with -p gopher.

package main

import wf "github.com/danielb42/whiteflag"

func main() {
    if wf.FlagPresent("p") {
        println(wf.GetString("p"))
    }
}
With long+required+default flags and nice 'Usage' output

The next snippet will print the sum of two integers given through -x and -y. For y we specify a default value. Let's also associate long flags to the short flags so we could equivalently run the snippet with --first and --second. Aliasing flags makes them known to the Usage/Help text generation.

package main

import wf "github.com/danielb42/whiteflag"

func main() {
    wf.Alias("x", "first",  "The first number.")
    wf.Alias("y", "second", "The second number.")
    wf.SetIntDefault("y", 42)

    // we don't do a FlagPresent() check on x und y before Get'ting them so
    // the program will exit if x is not specified, thus making x 'required'.
    // For a missing y flag, the default value of 42 would be used.

    x := wf.GetInt("x")
    y := wf.GetInt("y")
    sum := x + y
    println("sum of x and y:", sum)
}

For the snippet above the following Usage/Help text would be available through -h/--help:

Usage: ./example <flags>

Flags:
  -x  --first    The first number.
  -y  --second   The second number.

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Alias

func Alias(short, long, description string)

Alias associates one long flag to a short flag. Also, a description for that flag pair can be specified which will be included in --help/-h output. All aliases must be declared before any call to FlagPresent(), GetBool(), GetInt() or GetString().

func CheckBool

func CheckBool(flag string) bool

(DEPRECATED) Use FlagPresent().

func CheckInt

func CheckInt(flag string) bool

(DEPRECATED) Use FlagPresent().

func CheckString

func CheckString(flag string) bool

(DEPRECATED) Use FlagPresent().

func FlagPresent added in v1.2.0

func FlagPresent(flag string) bool

FlagPresent checks if flag was specified on the command line.

func GetBool

func GetBool(flag string) bool

GetBool checks if flag is present on the command line. It prints an error and exits the program if flag is not used in a boolean context (i.e. is followed by a value).

func GetInt

func GetInt(flag string) int

GetInt fetches the value of an int flag. It prints an error and exits the program if flag is missing or no int value is specified.

func GetString

func GetString(flag string) string

GetString fetches the value of a string flag. It prints an error and exits the program if flag is missing or no string value is specified.

func ParseCommandLine

func ParseCommandLine()

(DEPRECATED) This call can be removed.

func SetIntDefault added in v1.2.0

func SetIntDefault(flag string, value int)

SetIntDefault sets a default value for an int type flag. Defaults must be declared before any call to FlagPresent(), GetBool(), GetInt() or GetString().

func SetStringDefault added in v1.2.0

func SetStringDefault(flag, value string)

SetStringDefault sets a default value for a string type flag. Defaults must be declared before any call to FlagPresent(), GetBool(), GetInt() or GetString().

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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