博客
关于我
go-options-一个命令行标识、配置文件值解析库
阅读量:581 次
发布时间:2019-03-11

本文共 1555 字,大约阅读时间需要 5 分钟。

1、go-options 简介

一个用于解析命令行标志、配置文件和默认结构体值去设置配置值的类库。

项目地址:github.com/mreiferson/go-options

主要目的:将命令行flags(FlagSet)或者一个外部解析的配置文件(可以是一个map)设置的 配置值 解析设置到一个结构体struts的成员 上

options支持的结构体的成员tag:"flag","cfg"和“deprecated”

值按以下优先级解析(从高到低):

1. Command line flag

2. Deprecated command line flag

3. Config file value

4. Get() value (if Getter)

5. Options struct default value

2、实例

 

package mainimport (   "flag"   "fmt"   "github.com/mreiferson/go-options"   "time")type Options struct {   MaxSize int64 `flag:"max-size" cfg:"max_size"`   Timeout time.Duration `flag:"timeout" cfg:"timeout"`   Description string `flag:"description" cfg:"description"`}func main() {   flagSet := flag.NewFlagSet("example", flag.ExitOnError)   flagSet.Int64("max-size",1024768,"maximum size")   flagSet.Duration("timeout",1*time.Hour,"timeout setting")   flagSet.String("description","","description info")   // parse command line arguments here   //flagSet.Parse(os.Args[1:])   flagSet.Parse([]string {"-timeout=5s"})   //创建Options   opts := &Options{      MaxSize: 1,      Timeout: time.Second,   }   // config map   cfg := map[string]interface{}{      "max-size":888,      "timeout":"2h",      "description":"description-info",      "description1":"has noting",   }   fmt.Printf("%#v",opts)//&main.Options{MaxSize:1, Timeout:1000000000, Description:""}   //解析命令行FlagSet和cfg   //优先级 commandline FlagSet > cfg> struct values   options.Resolve(opts,flagSet,cfg)   fmt.Println()   fmt.Printf("%#v",opts)   //&main.Options{MaxSize:1024768, Timeout:5000000000, Description:"description-info"}}

 

转载地址:http://jwmvz.baihongyu.com/

你可能感兴趣的文章
Mysql8.0以上重置初始密码的方法
查看>>
mysql8.0新特性-自增变量的持久化
查看>>
Mysql8.0注意url变更写法
查看>>
Mysql8.0的特性
查看>>
MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
查看>>
MySQL8修改密码的方法
查看>>
Mysql8在Centos上安装后忘记root密码如何重新设置
查看>>
Mysql8在Windows上离线安装时忘记root密码
查看>>
MySQL8找不到my.ini配置文件以及报sql_mode=only_full_group_by解决方案
查看>>
mysql8的安装与卸载
查看>>
MySQL8,体验不一样的安装方式!
查看>>
MySQL: Host '127.0.0.1' is not allowed to connect to this MySQL server
查看>>
Mysql: 对换(替换)两条记录的同一个字段值
查看>>
mysql:Can‘t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock‘解决方法
查看>>
MYSQL:基础——3N范式的表结构设计
查看>>
MYSQL:基础——触发器
查看>>
Mysql:连接报错“closing inbound before receiving peer‘s close_notify”
查看>>
mysqlbinlog报错unknown variable ‘default-character-set=utf8mb4‘
查看>>
mysqldump 参数--lock-tables浅析
查看>>
mysqldump 导出中文乱码
查看>>