博客
关于我
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 ansi nulls_SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON 什么意思
查看>>
multi swiper bug solution
查看>>
MySQL Binlog 日志监听与 Spring 集成实战
查看>>
MySQL binlog三种模式
查看>>
multi-angle cosine and sines
查看>>
Mysql Can't connect to MySQL server
查看>>
mysql case when 乱码_Mysql CASE WHEN 用法
查看>>
Multicast1
查看>>
mysql client library_MySQL数据库之zabbix3.x安装出现“configure: error: Not found mysqlclient library”的解决办法...
查看>>
MySQL Cluster 7.0.36 发布
查看>>
Multimodal Unsupervised Image-to-Image Translation多通道无监督图像翻译
查看>>
MySQL Cluster与MGR集群实战
查看>>
multipart/form-data与application/octet-stream的区别、application/x-www-form-urlencoded
查看>>
mysql cmake 报错,MySQL云服务器应用及cmake报错解决办法
查看>>
Multiple websites on single instance of IIS
查看>>
mysql CONCAT()函数拼接有NULL
查看>>
multiprocessing.Manager 嵌套共享对象不适用于队列
查看>>
multiprocessing.pool.map 和带有两个参数的函数
查看>>
MYSQL CONCAT函数
查看>>
multiprocessing.Pool:map_async 和 imap 有什么区别?
查看>>