kiper

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2022 License: MIT Imports: 8 Imported by: 4

README

Kiper

Although there are lots of solutions for handling command line flags or config file, sometimes we need to handle the command line flags and config file at the same time. And kiper is aiming at providing a convenience at this scenario.

Rewriting the command line flags and config file parser is reinventing the wheel, so the kiper is just a wrapper of kingpin + viper. I like the way viper handles the config files however personally I am a fan of kinping because it's fluent-style and it can validate the command line flags easily. This library is a combination of this two projects to handle the flags and config files at the same time.

Feature

  • Support various type of data like int, string, bool, struct, array(slice)
  • Merge flag and config file settings automatically.
  • Custom validations for both flag and config file settings.

install

$ go get github.com/leeif/kiper

Tags

kiper_config: an entry point of the kiper parser. kiper_config is a collection of kiper_values.

Field Description
name config name

kiper_value: the actual data filed you need to parse from the command line flags and config file.

Field Description
name value name
default default value
help help message
required set the flag to required, default value will be ignored due to the kingpin feature

KiperValue interface

You can implement the KiperValue interface int your own data structs to achieve the data validation.

type KiperValue interface {
	Set(string) error
	String() string
}
type Port struct {
	p string
}

func (port *Port) Set(p string) error {
	if p == "" {
		return errors.New("port can't be empty")
	}
	port.p = p
	return nil
}

func (port *Port) String() string {
	return port.p
}

type Config struct {
	Port *Port `kiper_value:"name:port;help:port of server;default:8080"`
}

Examples

basic_usage.go

kiper_value.go

Limitation

  • Config file settings have a higher priority when merge with flags
  • Only support int and string array(slice) type now
  • Not support sub command yet

Documentation

Index

Constants

View Source
const (
	DefaultConfigFile     = ".kiper"
	DefaultArrayDelimiter = `\s*,\s*`
)

DefaultConfigFile is default config file path

Variables

This section is empty.

Functions

This section is empty.

Types

type Kiper

type Kiper struct {
	Viper   *viper.Viper
	Kingpin *kingpin.Application
	// contains filtered or unexported fields
}

func NewKiper

func NewKiper(name, help string) *Kiper

func (*Kiper) Parse

func (k *Kiper) Parse(config interface{}, args []string) error

func (*Kiper) SetArrayDelimiter

func (k *Kiper) SetArrayDelimiter(delimiter string)

func (*Kiper) SetConfigFileFlag

func (k *Kiper) SetConfigFileFlag(flag string, description string, value string)

func (*Kiper) SetConfigFilePath added in v0.5.0

func (k *Kiper) SetConfigFilePath(path string)

func (*Kiper) SetSkipFlag added in v0.6.0

func (k *Kiper) SetSkipFlag(skip bool)

type KiperValue

type KiperValue interface {
	Set(string) error
	String() string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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