博客
关于我
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保姆级教程(SQL语法基础篇)从小白到高手的进阶指南,收藏这一篇就够了
查看>>
MySQL修改密码报错ERROR 1396 (HY000): Operation ALTER USER failed for ‘root‘@‘localhost‘
查看>>
Mysql全局优化参数
查看>>
MySQL全文索引实现简单版搜索引擎
查看>>
MySQL全面瓦解:安装部署与准备
查看>>
MySQL内存表使用技巧
查看>>
MySQL再叙(体系结构、存储引擎、索引、SQL执行过程)
查看>>
MySQL函数
查看>>
mysql函数汇总之数学函数
查看>>
mysql函数汇总之日期和时间函数
查看>>
mysql函数汇总之条件判断函数
查看>>
mysql函数汇总之系统信息函数
查看>>
MySQL函数简介
查看>>
mysql函数遍历json数组
查看>>
MySQL函数(转发)
查看>>
mysql分区表
查看>>
MySQL分层架构与运行机制详解
查看>>
mysql分库分表中间件简书_MySQL分库分表
查看>>
MySQL分库分表会带来哪些问题?分库分表问题
查看>>
MySQL分组函数
查看>>