JSON配置
我正在尝试将ArgParse集成到我的JavaScript程序中,以便从外部配置文件中加载所有参数 ,然后可以用提供的CLI参数将这些值覆盖。为此,我决定阅读一个配置文件&将其解析到JSON对象中,然后通过键迭代并在解析器中设置默认值,最后解析剩余的CLI参数以覆盖默认值。不幸的是,我得到了一些奇怪的行为,并且由于它是Python存储库的一个端口,因此语法上没有太多的文档( https://www.npmjs.com/package/argparse/argparse )
我有以下逻辑:
//readFile helper method not shown
const configArgs = JSON.parse(readFile('config.json'))
for (let key in configArgs) {
let value = configArgs[key]
//used for debugging
console.log(`${key}: ${value}`)
parser.set_defaults({key: value})
}
var args = parser.parse_args()
//used for debugging
console.log(args)
但是,它看起来像parser.set_defaults()正常工作:
path: ../data/testInput.txt
json: ../build/testSet.json
name: foo.txt
output: output.txt
Namespace(path=undefined, json=undefined, name=undefined, output=undefined, key='output.txt')
即使将键
传递到set_defaults()
是一个变量&尽管键
传递到键
,为什么要创建一个新的配置选项“键”。每次将其登录到控制台时都有新价值吗?
I'm trying to integrate argparse into my Javascript program, such that all arguments can be loaded from an external config file, and then those values can be overridden with provided CLI arguments afterwards. To do this, I decided to read a config file & parse it into a JSON object, then iterate through the keys and set the default values in the parser, and finally parse any remaining CLI arguments to override the defaults. Unfortunately, I'm getting some strange behavior back, and since it's a port of the Python repository there's not much syntactically-relevant documentation (https://www.npmjs.com/package/argparse)
I have the following logic:
//readFile helper method not shown
const configArgs = JSON.parse(readFile('config.json'))
for (let key in configArgs) {
let value = configArgs[key]
//used for debugging
console.log(`${key}: ${value}`)
parser.set_defaults({key: value})
}
var args = parser.parse_args()
//used for debugging
console.log(args)
However, it looks like the parser.set_defaults()
line isn't working correctly:
path: ../data/testInput.txt
json: ../build/testSet.json
name: foo.txt
output: output.txt
Namespace(path=undefined, json=undefined, name=undefined, output=undefined, key='output.txt')
Why is it trying to create a new config option "key", even though the key
passed into set_defaults()
is a variable & has a new value every time it's logged to the console?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
几年前,我为这个存储库做出了贡献,但最近没有使用过它或JavaScript。
在python
argparse
中,我认为这是您要做的:或仅立即设置所有密钥:
在Python
中(** conf)等同于
(路径='.. data ...',json ='../build'等)
。set_defaults
的操作是添加到_defaults
属性:Python代码是:
相应的JavaScript代码是:
我不记得足够不是。
I contributed to this repository years ago, but haven't worked with it or javascript recently.
In python
argparse
, I think this is what you want to do:or simply set all keys at once:
In Python
(**conf)
is equivalent to(path='..data...', json='../build', etc)
.The action of
set_defaults
is to add to the_defaults
attribute:The python code is:
The corresponding javascript code is:
I don't remember enough javascript to say whether that's correct or not.