flags

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2026 License: BSD-3-Clause Imports: 4 Imported by: 0

Documentation

Overview

Package flags implements auxiliary command-line flag types for flags package.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrFlag = errors.New("invalid flag")

ErrFlag means the flag has invalid value.

Functions

func NewFlagError

func NewFlagError(f *flag.Flag, err error) error

NewFlagError creates a FlagError.

Types

type FlagError

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

FlagError is an error attached to the flag.

func (FlagError) Error

func (e FlagError) Error() string

Error implements [builtin.Error] interface.

func (FlagError) Is

func (e FlagError) Is(err error) bool

Is treats FlagError equivalent to ErrFlag.

func (FlagError) Unwrap

func (e FlagError) Unwrap() error

Unwrap returns the error that is associated with the flag.

type FlagSet

type FlagSet struct {
	*flag.FlagSet
}

FlagSet add flags validation to flag.FlagSet. It calls *.Validate() on every flag that implements Validator interface:

type Validator interface {
	Validate() error
}

which should return a shallow error, describing only what is wrong. The flags.FlagSet adds context such as flag name and value to the error description.

The FlagSet stops on the first invalid flag and returns FlagError.

EXAMPLE

	fs := flags.NewFlagSet("demo", flag.ExitOnError)
 // register flags
	if err := fs.ParseAndValidate(); err != nil {
		return err
	}
Example (NoValidation)
package main

import (
	"errors"
	"flag"
	"fmt"

	"github.com/skhal/lab/go/flags"
)

func main() {
	args := []string{
		"-n", "-10",
		"-m", "-20",
	}
	var (
		n int
		m int
	)
	defer func() {
		fmt.Printf("n: %d\n", n)
		fmt.Printf("m: %d\n", m)
	}()
	fs := flags.NewFlagSet("example", flag.ExitOnError)
	fs.IntVar(&n, "n", 1, "positive number n")
	fs.IntVar(&m, "m", 2, "positive number m")
	if err := fs.ParseAndValidate(args); err != nil {
		if !errors.Is(err, flags.ErrFlag) {
			fmt.Println(err)
		}
		return
	}
}
Output:
n: -10
m: -20
Example (Validate)
package main

import (
	"errors"
	"flag"
	"fmt"
	"strconv"

	"github.com/skhal/lab/go/flags"
)

func main() {
	args := []string{
		"-n", "-10",
		"-m", "20",
	}
	var (
		n = 1
		m = 2
	)
	defer func() {
		fmt.Printf("n: %d\n", n)
		fmt.Printf("m: %d\n", m)
	}()
	fs := flags.NewFlagSet("example", flag.ExitOnError)
	fs.Var(newTestPositiveInt(&n), "n", "positive number n")
	fs.Var(newTestPositiveInt(&m), "m", "positive number m")
	if err := fs.ParseAndValidate(args); err != nil {
		if !errors.Is(err, flags.ErrFlag) {
			fmt.Println(err)
		}
		return
	}
}

type testPositiveInt struct {
	n *int
}

func newTestPositiveInt(n *int) flag.Value {
	return &testPositiveInt{n}
}

func (f testPositiveInt) Set(s string) error {
	n, err := strconv.Atoi(s)
	if err != nil {
		return err
	}
	*f.n = n
	return nil
}

func (f testPositiveInt) String() string {
	if f.n == nil {
		return ""
	}
	return strconv.Itoa(*f.n)
}

func (f testPositiveInt) Validate() error {
	if *f.n <= 0 {
		return fmt.Errorf("non-positive integer")
	}
	return nil
}
Output:
n: -10
m: 20

func NewFlagSet

func NewFlagSet(name string, errorHandling flag.ErrorHandling) *FlagSet

NewFlagSet creates a FlagSet object that wraps flag.FlagSet.

func (*FlagSet) ParseAndValidate

func (fs *FlagSet) ParseAndValidate(args []string) error

ParseAndValidate runs flag.Parse, followed by [Validate]. It returns first non-nil error from either of these calls.

func (*FlagSet) Validate

func (fs *FlagSet) Validate() (err error)

Validate runs flag validator for any flag that implements Validator interface.

type StringList

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

StringList is a flag of comma-separated non-empty strings. It trims spaces and skips empty values. The flag wraps a slice of strings, where values are to be put.

The wrapped slice might be non-empty, which is to be used for default value. If the flag is present, StringList overwrites the default value.

Example
package main

import (
	"flag"
	"fmt"

	"github.com/skhal/lab/go/flags"
)

func main() {
	var tags []string
	fs := flag.NewFlagSet("demo", flag.ContinueOnError)
	fs.Var(flags.NewStringList(&tags), "tag", "comma separated tags")
	err := fs.Parse([]string{"-tag", "1", "-tag", "2,3", "-tag", ",,4"})
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(tags)
}
Output:
[1 2 3 4]

func NewStringList

func NewStringList(s *[]string) *StringList

NewStringList creates a StringList flag.

func (*StringList) Set

func (sl *StringList) Set(value string) error

Set implements flag.Value interface.

func (*StringList) String

func (sl *StringList) String() string

String implements flag.Value interface.

type Validator

type Validator interface {
	// Validate runs validation logic.
	Validate() error
}

Validator verifies that the flag has a valid value. It should return a shallow error, describing only the problem, e.g. "non-positive value".

Jump to

Keyboard shortcuts

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