社区首页 >问答首页 >戈朗:如何用蛇而不是眼镜蛇来支持cli params,env和config文件?

戈朗:如何用蛇而不是眼镜蛇来支持cli params,env和config文件?
EN

Stack Overflow用户
提问于 2022-01-11 12:13:24
回答 2查看 1.2K关注 0票数 1

我正试图编写一个只有一个命令的应用程序,因此,我想我可以跳过眼镜蛇。

应用程序应该能够支持所有类型的配置:

  • 命令行params
  • env vars
  • 配置文件

我用的是毒蛇,但我无法用它来读我的cli params。

代码语言:javascript
代码运行次数:0
复制
  v := viper.New()
  viper.SetConfigName("config") 
  viper.AddConfigPath(".")      
  v.SetDefault(pathKey, defaultPath)

  fs := pflag.NewFlagSet("app", pflag.ExitOnError)
  fs.String(pathKey, defaultPath, "Default path")
  fs.StringSlice(wordsKey, []string{""}, "Words")
  fs.String(loglevelKey, "info", "Log level")

  if err := v.BindPFlags(fs); err != nil {
    fmt.Println(err)
    os.Exit(1)
  }
  if err := fs.Parse(os.Args[1:]); err != nil {
    fmt.Println(err)
    os.Exit(1)
  }

  v.AutomaticEnv()

  if err := v.ReadInConfig(); err != nil {
    fmt.Println("no conf file") //ignore, it can be either cli params, or conf file
  }

  var c conf
  if err := v.Unmarshal(&c); err != nil {
    fmt.Println(err)
    os.Exit(1)
  }

但我从来没有让那些人进入建筑。在v之前打印Unmarshal没有显示我提供的任何cli参数。

我遗漏了什么?我需要用眼镜蛇做这个吗?还是必须手动将每个标记(例如fs.String(pathKey, defaultPath, "Default path") )分配给配置结构?

EN

回答 2

Stack Overflow用户

发布于 2022-01-11 12:50:54

为了子孙后代,我想我发现了一个问题:

我的conf结构没有像标志那样有对应的键名。例如,当字段名为json:"logLevel"时,仅设置DisplayLogLevel是不够的,它必须是:

代码语言:javascript
代码运行次数:0
复制
const (
  pathKey = "path"
  wordsKey = "words"
  logLevelKey = "logLevel"
)

type conf struct {
   Path string `json:"path"`
   Words []string `json:"words"`
   LogLevel string `json:"logLevel"`
}
票数 1
EN

Stack Overflow用户

发布于 2022-01-11 13:57:58

也许您必须设置配置类型。来自https://github.com/spf13/viper

viper.SetConfigType("yaml") // REQUIRED if the config file does not have the extension in the name

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70672896

复制
相关文章
python3 TypeError: a
运行telnetlib的时候报错:TypeError: a bytes-like object is required, not ‘str’,原因是因为python2和python3的版本的差异。 在python2中可正常运行,而python3最重要的新特性也是对文本和二进制数据做了更清晰的区分。文本用unicode编码,为str类型,二进制数据则为bytes类型。
py3study
2020/01/03
1.1K0
Mac 安装Python3
Python 2.7.10 (default, Jul 15 2017, 17:16:57) 
星宇大前端
2019/01/15
2K0
Pygraphviz
代码越来越复杂,为了理清架构,需要搞清楚现有代码业务逻辑,没有很顺手的工具,简单写写学习一下。
vanguard
2020/12/30
5750
python3 调用heapq库 时遭遇 "TypeError: unorderable types"
同样的代码在LeetCode上提交,在 python3解释器 下报错,换成 python2解释器 下却好好的:
JNingWei
2018/09/27
7550
python3 调用heapq库 时遭遇 "TypeError: unorderable types"
mac python3 安装parami
$ git clone https://github.com/paramiko/paramiko.git
py3study
2020/01/03
4740
mac> python3 安装Djang
说明:mac系统自带了python2.x (在/System/Library/Frameworks/Python.Frameworks/Version/2.x/...)
py3study
2020/01/03
4990
安装python3(mac、使用brew
其中在最后一行/usr/local/Cellar/python/3.6.5这个便是使用brew安装python3的目录
py3study
2020/01/13
3.1K0
mac系统安装pycharm_mac下载python3
pycharm 是一款针对python开发的优秀的IDE, 以下是针对其在mac上的开发配置使用
全栈程序员站长
2022/09/28
5200
PyGraphviz 安装使用
PyGraphviz 对于图、点和边的设置,如颜色、样式、形状等属性,分别提供有 graph_attr、node_attr 和 edge_attr 属性设置函数。
zhipingChen
2018/09/13
2.2K0
PyGraphviz 安装使用
基础服务系列-Mac 安装Python3
前言 本例采用brew安装。未安装brew,参考Mac 安装Brew。 安装步骤 search brew search python3 ==> Formulae boost-python3 python3 ✔ python@3 ✔ python@3.8 If you meant "python3" specifically: It was migra
用户2146693
2021/12/28
3200
基础服务系列-Mac 安装Python3
mac安装虚拟环境python3
Virtaulenvwrapper是virtualenv的扩展包,可以更方便地新增,删除,复制,切换虚拟环境。
新人小试
2020/03/31
5980
python3搞系统里头之Mac篇
mac系统自带python2环境, 但现在一般使用的都是python3, 而且python2在2020.1.1以后不会再进行维护了, 所以安装个python3还是很有必要的
不止于python
2022/05/31
3140
python3搞系统里头之Mac篇
Mac下Python3安装及基于Idea开发
本篇文章带大家基于Mac OS操作系统,下载、安装Python环境,并基于Idea编写第一个Demo。
程序新视界
2023/04/09
1.1K0
Mac下Python3安装及基于Idea开发
MAC os :LightGBM Installation Error in MacOS with Anaconda Python3
macos 报错 OSError: dlopen(/Users/yanerrol/opt/anaconda3/envs/py366/lib/python3.6/site-packages/lightgbm/lib_lightgbm.so, 6): Library not loaded: /usr/local/opt/libomp/lib/libomp.dylib Referenced from: /Users/yanerrol/opt/anaconda3/envs/py366/lib/python3.6
AI拉呱
2021/03/04
1.5K0
linux下运行python3出现TypeError: a bytes-like object is required, not 'str'
 以上代码用python3运行后,出现TypeError: a bytes-like object is required, not 'str'
逆向小白
2018/09/12
13.8K0
linux下运行python3出现TypeError: a bytes-like object is required, not 'str'
mac os+selenium2+chrome驱动+python3
mac os 10.11.5 mac自带python2.7,自己下载了python3.5,pip list查看系统中的安装包,本人电脑中已经安装了pip和setuptools,若未安装,请先使用
全栈程序员站长
2022/09/07
4710
Mac上最简单配置python3开发环
网上有使用pyenv方式安装python3以实现与系统的python版本共存而不冲突,个人觉得其实没有必要,我们其实可以单独运行python3。 首先我们还是需要先安装python3,这里使用homebrew安装,方便快捷好管理,棒棒哒!
py3study
2020/01/05
1.3K0
Mac下安装Python3、pip、ipython的方法
1 安装Xcode 1.1 App Store 搜索Xcode 并安装 1.2 安装 Xcode command line tool 1.2.1 打开命令行terminal工具 control + space 输入terminal 回车 1.2.2 安装Xcode command line tool xcode-select --install 2 安装套件管理工具 Homebrew 2.1 安装 Homebrew /usr/bin/ruby -e "$(curl -fsSL https://raw
Python疯子
2018/09/06
1.7K0
python3在pycharm中为什么导入random模块不能用? TypeError: ‘module’ object is not callable…
新手学python求大神指导,也用sys导入了random.py的路径,仍然不行。
全栈程序员站长
2022/07/07
1.5K0
Django Models UML
Generate a class diagram based on GraphViz(pygraphviz) and django django-extensions
vanguard
2020/12/09
8260

相似问题

Python3,可除以3,typeError

12

Python3: TypeError:“TypeError”对象不可订阅

11

安装PyGraphviz时出错(Mac、Anaconda)

40

PySide & Python3: TypeError on QAction

10

Python3 Runserver TypeError {newbie}

15
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档