pflags

package
v1.1.59 Latest Latest
Warning

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

Go to latest
Published: May 26, 2025 License: MIT Imports: 17 Imported by: 6

README

pflags

pflags 是一个强大的配置管理工具,支持从多种源加载配置参数,包括命令行参数、环境变量、配置文件等。

功能特性

  • 支持多种数据类型:string、int、bool、float64、duration、slice等
  • 支持结构体配置映射
  • 支持配置文件监听和热更新
  • 支持环境变量绑定
  • 支持命令行参数解析
  • 支持配置验证

基本使用

package main

import (
    "fmt"
    "github.com/go-puzzles/puzzles/pflags"
)

var (
    port = pflags.Int("port", 8080, "服务端口")
    debug = pflags.Bool("debug", false, "是否开启调试模式")
    name = pflags.String("name", "myapp", "应用名称")
)

func main() {
    pflags.Parse()
    
    fmt.Printf("Port: %d\n", port())
    fmt.Printf("Debug: %t\n", debug())
    fmt.Printf("Name: %s\n", name())
}

结构体配置

type Config struct {
    Port    int    `flag:"port" default:"8080" usage:"服务端口"`
    Debug   bool   `flag:"debug" default:"false" usage:"调试模式"`
    Name    string `flag:"name" default:"myapp" usage:"应用名称"`
}

var config = pflags.Struct("config", &Config{}, "struct config")

func main() {
    pflags.Parse()
    
    cfg := &Config{} 
    err := config(cfg)
    fmt.Printf("Config: %+v\n", cfg)
}

更多示例

详细使用示例请参考 example 目录。

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotStruct = errors.New("not struct")
)

Functions

func BindPFlag

func BindPFlag(key string, flag *pflag.Flag)

func Duration

func Duration(key string, defaultValue time.Duration, usage string) func() time.Duration

func DurationRequired

func DurationRequired(key, usage string) func() time.Duration

func GetServiceName

func GetServiceName() string

func OverrideDefaultConfigFile

func OverrideDefaultConfigFile(configFile string)

func Parse

func Parse(opts ...OptionFunc)

func SetDefaultConfigReader added in v1.1.7

func SetDefaultConfigReader(reader reader.ConfigReader)

func SetDefaultConfigWatcher added in v1.1.7

func SetDefaultConfigWatcher(watchers watcher.ConfigWatcher)

func SetStructParseTagName added in v1.0.1

func SetStructParseTagName(t string)

func Struct

func Struct(key string, defaultVal any, usage string) func(out any) error

func Viper

func Viper() *viper.Viper

Types

type BoolGetter

type BoolGetter func() bool

func Bool

func Bool(key string, defaultVal bool, usage string) BoolGetter

func BoolRequired

func BoolRequired(key, usage string) BoolGetter

func (BoolGetter) Value

func (bg BoolGetter) Value() bool

type Float64Getter

type Float64Getter func() float64

func Float64

func Float64(key string, defaultVal float64, usage string) Float64Getter

func Float64Required

func Float64Required(key, usage string) Float64Getter

func (Float64Getter) Value

func (fg Float64Getter) Value() float64

type HasDefault

type HasDefault interface {
	SetDefault()
}

type HasReloader

type HasReloader interface {
	Reload()
}

type HasValidator

type HasValidator interface {
	Validate() error
}

type IntGetter

type IntGetter func() int

func Int

func Int(key string, defaultVal int, usage string) IntGetter

func IntRequired

func IntRequired(key, usage string) IntGetter

func (IntGetter) Value

func (ig IntGetter) Value() int

type Option

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

type OptionFunc

type OptionFunc func(*Option)

func WithConfigFileName added in v1.1.24

func WithConfigFileName(n string) OptionFunc

func WithConfigFileType added in v1.1.24

func WithConfigFileType(t string) OptionFunc

func WithConfigReader

func WithConfigReader(reader reader.ConfigReader) OptionFunc

func WithConfigWatcher

func WithConfigWatcher(watchers ...watcher.ConfigWatcher) OptionFunc

type StringGetter

type StringGetter func() string

func String

func String(key, defaultVal, usage string) StringGetter

func StringP

func StringP(key, shorthand, defaultVal, usage string) StringGetter

func StringRequired

func StringRequired(key, usage string) StringGetter

func (StringGetter) Value

func (sg StringGetter) Value() string

type StringSliceGetter

type StringSliceGetter func() []string

func StringSlice

func StringSlice(key string, defaultVal []string, usage string) StringSliceGetter

func (StringSliceGetter) Value

func (sg StringSliceGetter) Value() []string

Directories

Path Synopsis
example
pflags command

Jump to

Keyboard shortcuts

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