我正在研究一种接受两个参数的方法,使用argparse,如果给定一个标志,它将切换dest变量将从stdin获得它的值。
用例提供了我正在编写的cli工具,并提供了两种提供数据的方法。
例如,在调用函数处理最终namespace之前,这4个命令应该是等效的。
cat data.json | template-engine --template template.htm --json-stdin
template-engine --template template.htm --json-data data.json
template-engine --template <(cat template.htm) --json-data <(cat data.json)
cat template.htm | template-engine --json-data data.json这个问题并不涉及太多的代码,因为根据我在文档中所读到的,这看起来很困难,但我认为使用action类可以这样做,但我不完全确定是如何做到的。
有什么想法吗?
为了明确起见,我想使用argparse (而不是任何其他命令行接口库)来完成这个任务。
我尝试使用一个操作,该操作将检查变量值的来源,但如果为其中一个参数分配默认值,则会引发问题。
我想提出的一些问题是:
argparse工具来完成这个任务,或者应该实现逻辑后解析。发布于 2019-06-10 20:56:40
像大多数Unix工具一样,使用连字符-来表示stdin要简单得多。例如:
template-engine --template template.htm --json data.json
cat data.json | template-engine --template template.htm --json -
cat template.htm | template-engine --template - --json data.json要处理连字符,可以使用fileinput.input(files=[filename])而不是手动执行。
https://stackoverflow.com/questions/56532806
复制相似问题