[英]Python argparse mutually exclusive with stdin being one of the options
I would like my script to receive these mutually exclusive input options: 我希望我的脚本接收以下互斥的输入选项:
script.py -i input.json
); 包含JSON的输入文件( script.py -i input.json
); script.py '{"a":1}'
); 包含JSON的字符串( script.py '{"a":1}'
); echo '{"a":1}' | script.py
or cat input.json | script.py
). 来自stdin的JSON( echo '{"a":1}' | script.py
或cat input.json | script.py
)。 and these mutually exclusive output options : 这些互斥的输出选项 :
So I tried with this code 所以我尝试了这段代码
import json,sys,argparse
parser = argparse.ArgumentParser(description='Template for python script managing JSON as input/output format')
group = parser.add_mutually_exclusive_group()
group.add_argument('--input-file', '-i', type=str, help='Input file name containing a valid JSON.', default=sys.stdin)
group.add_argument('json', nargs='?', type=str, help='Input string containing a valid JSON.' , default=sys.stdin)
parser.add_argument('--output-file', '-o',type=str, help='Output file name.')
args = parser.parse_args()
if not sys.stdin.isatty():
data = sys.stdin.read()
else:
# args = parser.parse_args()
if args.input_file :
data=open(args.input_file).read()
elif args.json :
data=args.json
datain=json.loads(data)
dataout=json.dumps(datain, indent=2)
if args.output_file :
output_file=open(args.output_file, 'w')
output_file.write(dataout+'\n')
output_file.close()
else:
print (dataout)
But it does not work with stdin as it requires at least one of the two group
options. 但是它不适用于stdin,因为它至少需要两个group
选项之一。
How can I add stdin in the list of input options? 如何在输入选项列表中添加标准输入?
Adding the default=sys.stdin
argument works if I call it like that 如果我这样命名,则添加default=sys.stdin
参数是default=sys.stdin
echo '{}' | ./script.py -
but not like that: 但不是这样的:
echo '{}' | ./script.py
I would take advantage of argparse.FileType
with a default value of sys.stdin
. 我会利用argparse.FileType
的默认值sys.stdin
。
import json,sys,argparse
parser = argparse.ArgumentParser(description='Template for python script managing JSON as input/output format')
group = parser.add_mutually_exclusive_group()
group.add_argument(
'--input-file', '-i',
type=argparse.FileType('r'),
default=sys.stdin,
help='Input file name containing a valid JSON.')
group.add_argument(
'json',
nargs='?',
type=str,
help='Input string containing a valid JSON.')
parser.add_argument(
'--output-file', '-o',
type=argparse.FileType('w'),
help='Output file name.',
default=sys.stdout)
args = parser.parse_args()
data = args.json or args.input_file.read()
datain=json.loads(data)
dataout=json.dumps(datain, indent=2)
args.output_file.write(dataout)
With: 附:
group.add_argument('--input-file', '-i')
You could test 你可以测试
if args.input_file is None:
<-i wasn't supplied>
else:
if args.input_file == '-':
f = sys.stdin
else:
f = open(args.input_file)
data = f.read() # etc
Or may be better: 或者可能更好:
if args.input_file == '-':
data = sys.stdin.read()
else
with open(args.input_file) as f:
f.read()
A tricky thing with stdin
is that you don't want to close it after use like you would with a regular file name. 使用stdin
一个棘手的事情是,您不想像使用常规文件名一样在使用后关闭它。 And you can't use it in a with
. 并且您不能在with
使用它。
Similarly with stdout
. 与stdout
类似。
Some code sets a flag when it opens a file, as opposed to receiving an already open one, so it can remember to close the file at the end. 某些代码在打开文件时会设置一个标志,而不是接收一个已经打开的文件,因此它可以记住在最后关闭文件。
group.add_argument('--input-file','-i',nargs='?', default=None, const=sys.stdin)
would set arg.input_file
to stdin
when given -i
without an argument. 给定-i
不带参数时,会将arg.input_file
设置为stdin
。 But I think looking for a plain -
string is a better idea. 但是我觉得找一个普通的-
字符串是一个更好的主意。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.