博客
关于我
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/

你可能感兴趣的文章
MySQL 索引失效的 15 种场景!
查看>>
MySQL 索引深入解析及优化策略
查看>>
MySQL 索引的面试题总结
查看>>
mysql 索引类型以及创建
查看>>
MySQL 索引连环问题,你能答对几个?
查看>>
Mysql 索引问题集锦
查看>>
Mysql 纵表转换为横表
查看>>
mysql 编译安装 window篇
查看>>
mysql 网络目录_联机目录数据库
查看>>
MySQL 聚簇索引&&二级索引&&辅助索引
查看>>
Mysql 脏页 脏读 脏数据
查看>>
mysql 自增id和UUID做主键性能分析,及最优方案
查看>>
Mysql 自定义函数
查看>>
mysql 行转列 列转行
查看>>
Mysql 表分区
查看>>
mysql 表的操作
查看>>
mysql 视图,视图更新删除
查看>>
MySQL 触发器
查看>>
mysql 让所有IP访问数据库
查看>>
mysql 记录的增删改查
查看>>