博客
关于我
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定时器Events
查看>>
mysql实战01|基础架构:一条SQL查询语句是如何执行的?
查看>>
Mysql实战之数据备份
查看>>
MySQL实战教程:从小白到大神的进阶之路!
查看>>
mysql实现成绩排名
查看>>
Mysql客户端中文乱码问题解决
查看>>
mysql客户端工具使用
查看>>
MySQL密码忘记,怎么办?
查看>>
mysql对同一张表进行查询和赋值更新
查看>>
mysql导入数据库出现:Incorrect string value: '\xE7\x82\xB9\xE9\x92\x9F' for column 'chinese' at row 1...
查看>>
mysql导入(ibd文件)
查看>>
Mysql工作笔记006---Mysql服务器磁盘爆满了_java.sql.SQLException: Error writing file ‘tmp/MYfXO41p‘
查看>>
MySQL工具1:mysqladmin
查看>>
mysql常用命令
查看>>
MySQL常用命令
查看>>
mysql常用命令
查看>>
MySQL常用指令集
查看>>
mysql常用操作
查看>>
MySQL常用日期格式转换函数、字符串函数、聚合函数详
查看>>
MySQL常见架构的应用
查看>>