mini

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2023 License: Apache-2.0 Imports: 7 Imported by: 0

README

mini

Parse (sort of) INI files like "flag" package in Go.

Example

import (
	"fmt"
	"log"
	"strings"

	"github.com/lucasepe/mini"
)

func ExampleNewConf() {
	data := `
width: 1600
subject: Computers,Education,Programming

[background]
art: shapes

[title]
text: Awesome Book!
color: #000000
font-size: 160
offset: 0.3
`
	conf := mini.NewConf()

	width := conf.Int("", "width", 1000)
	height := conf.Int("", "height", 1000)
	subject := conf.StringSlice("", "subject", []string{})
	watermark := conf.String("", "watermark", "")

	art := conf.Enum("background", "art", []string{
		"color-circles",
		"shapes",
		"circles-grid",
		"pixel-hole",
		"dots-wawe",
		"contour-line",
		"dot-line",
		"warp",
	})

	err := conf.Parse(strings.NewReader(data))
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(*width)
	fmt.Println(*height)
	fmt.Println(*subject)
	fmt.Println(*watermark)
	fmt.Println(*art)

	// Output:
	// 1600
	// 1000
	// [Computers,Education,Programming]
	//
	// shapes
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func RootSection

func RootSection() string

Types

type ConfSet

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

func NewConf

func NewConf() *ConfSet
Example
package main

import (
	"fmt"
	"log"
	"strings"

	"github.com/lucasepe/mini"
)

func main() {
	data := `
width: 1600
subject: Computers,Education,Programming

[background]
art: shapes

[title]
text: Awesome Book!
color: #000000
font-size: 160
offset: 0.3
`
	conf := mini.NewConf()

	width := conf.Int("", "width", 1000)
	height := conf.Int("", "height", 1000)
	subject := conf.StringSlice("", "subject", []string{})
	watermark := conf.String("", "watermark", "")

	art := conf.Enum("background", "art", []string{
		"color-circles",
		"shapes",
		"circles-grid",
		"pixel-hole",
		"dots-wawe",
		"contour-line",
		"dot-line",
		"warp",
	})

	err := conf.Parse(strings.NewReader(data))
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(*width)
	fmt.Println(*height)
	for _, x := range *subject {
		fmt.Println(x)
	}
	fmt.Println(*watermark)
	fmt.Println(*art)

}
Output:

1600
1000
Computers
Education
Programming

shapes

func (*ConfSet) Bool

func (c *ConfSet) Bool(sectionName, name string, value bool) *bool

func (*ConfSet) BoolVar

func (c *ConfSet) BoolVar(p *bool, sectionName, name string, value bool)

func (*ConfSet) Duration

func (c *ConfSet) Duration(sectionName, name string, value time.Duration) *time.Duration

func (*ConfSet) DurationVar

func (c *ConfSet) DurationVar(p *time.Duration, sectionName, name string, value time.Duration)

func (*ConfSet) Enum

func (c *ConfSet) Enum(sectionName, name string, value []string) *string

func (*ConfSet) EnumVar

func (c *ConfSet) EnumVar(p *string, sectionName, name string, value []string)

func (*ConfSet) Float64

func (c *ConfSet) Float64(sectionName, name string, value float64) *float64

func (*ConfSet) Float64Var

func (c *ConfSet) Float64Var(p *float64, sectionName, name string, value float64)

func (*ConfSet) Int

func (c *ConfSet) Int(sectionName, name string, value int) *int

func (*ConfSet) Int64

func (c *ConfSet) Int64(sectionName, name string, value int64) *int64

func (*ConfSet) Int64Var

func (c *ConfSet) Int64Var(p *int64, sectionName, name string, value int64)

func (*ConfSet) IntVar

func (c *ConfSet) IntVar(p *int, sectionName, name string, value int)

func (*ConfSet) Parse

func (c *ConfSet) Parse(in io.Reader) error

func (*ConfSet) String

func (c *ConfSet) String(sectionName, name string, value string) *string

func (*ConfSet) StringSlice

func (c *ConfSet) StringSlice(sectionName, name string, value []string) *[]string

func (*ConfSet) StringSliceVar

func (c *ConfSet) StringSliceVar(p *[]string, sectionName, name string, value []string)

func (*ConfSet) StringVar

func (c *ConfSet) StringVar(p *string, sectionName, name string, value string)

func (*ConfSet) Uint

func (c *ConfSet) Uint(sectionName, name string, value uint) *uint

func (*ConfSet) Uint64

func (c *ConfSet) Uint64(sectionName, name string, value uint64) *uint64

func (*ConfSet) Uint64Var

func (c *ConfSet) Uint64Var(p *uint64, sectionName, name string, value uint64)

func (*ConfSet) UintVar

func (c *ConfSet) UintVar(p *uint, sectionName, name string, value uint)

func (*ConfSet) Var

func (c *ConfSet) Var(val Value, sectionName, name string)

type Getter

type Getter interface {
	Value
	Get() any
}

Getter is an interface that allows the contents of a Value to be retrieved. It wraps the Value interface, rather than being part of it, because it appeared after Go 1 and its compatibility rules. All Value types provided by this package satisfy the Getter interface.

type Item

type Item struct {
	SectionName string
	Name        string
	Val         Value
}

type Section

type Section struct {
	Name string
	Vals map[string]*Item
}

type Value

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

Value is the interface to the dynamic value stored in a flag. (The default value is represented as a string.)

If a Value has an IsBoolFlag() bool method returning true, the command-line parser makes -name equivalent to -name=true rather than using the next command-line argument.

Jump to

Keyboard shortcuts

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