JSON配置

发布于 2025-02-05 19:16:04 字数 1148 浏览 3 评论 0原文

我正在尝试将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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

·深蓝 2025-02-12 19:16:04

几年前,我为这个存储库做出了贡献,但最近没有使用过它或JavaScript。

在python argparse中,我认为这是您要做的:

In [27]: conf = {'path': '../data/testInput.txt',
    ...: 'json': '../build/testSet.json',
    ...: 'name': 'foo.txt',
    ...: 'output': 'output.txt'}
In [28]: parser = argparse.ArgumentParser()
In [29]: for k in conf.keys():
    ...:     print(k,conf[k])
    ...:     parser.set_defaults(**{k:conf[k]})
    ...:     
path ../data/testInput.txt
json ../build/testSet.json
name foo.txt
output output.txt

In [30]: parser.parse_args([])
Out[30]: Namespace(path='../data/testInput.txt', json='../build/testSet.json', name='foo.txt', output='output.txt')

或仅立即设置所有密钥:

In [31]: parser = argparse.ArgumentParser()
In [32]: parser.set_defaults(**conf)    
In [33]: parser.parse_args([])
Out[33]: Namespace(path='../data/testInput.txt', json='../build/testSet.json', name='foo.txt', output='output.txt')

在Python 中(** conf)等同于(路径='.. data ...',json ='../build'等)

set_defaults的操作是添加到_defaults属性:

In [34]: parser._defaults
Out[34]: 
{'path': '../data/testInput.txt',
 'json': '../build/testSet.json',
 'name': 'foo.txt',
 'output': 'output.txt'}

Python代码是:

def set_defaults(self, **kwargs):
    self._defaults.update(kwargs)
    # and set defaults of actions if any

相应的JavaScript代码是:

set_defaults(kwargs) {
    Object.assign(this._defaults, kwargs)
    # and set action.default if any

我不记得足够不是。

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:

In [27]: conf = {'path': '../data/testInput.txt',
    ...: 'json': '../build/testSet.json',
    ...: 'name': 'foo.txt',
    ...: 'output': 'output.txt'}
In [28]: parser = argparse.ArgumentParser()
In [29]: for k in conf.keys():
    ...:     print(k,conf[k])
    ...:     parser.set_defaults(**{k:conf[k]})
    ...:     
path ../data/testInput.txt
json ../build/testSet.json
name foo.txt
output output.txt

In [30]: parser.parse_args([])
Out[30]: Namespace(path='../data/testInput.txt', json='../build/testSet.json', name='foo.txt', output='output.txt')

or simply set all keys at once:

In [31]: parser = argparse.ArgumentParser()
In [32]: parser.set_defaults(**conf)    
In [33]: parser.parse_args([])
Out[33]: Namespace(path='../data/testInput.txt', json='../build/testSet.json', name='foo.txt', output='output.txt')

In Python (**conf) is equivalent to (path='..data...', json='../build', etc).

The action of set_defaults is to add to the _defaults attribute:

In [34]: parser._defaults
Out[34]: 
{'path': '../data/testInput.txt',
 'json': '../build/testSet.json',
 'name': 'foo.txt',
 'output': 'output.txt'}

The python code is:

def set_defaults(self, **kwargs):
    self._defaults.update(kwargs)
    # and set defaults of actions if any

The corresponding javascript code is:

set_defaults(kwargs) {
    Object.assign(this._defaults, kwargs)
    # and set action.default if any

I don't remember enough javascript to say whether that's correct or not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文