简体   繁体   English

Argparse可选的stdin读取和/或stdout输出

[英]Argparse optional stdin read and/or stdout out

A non-Python program will call a Python program with both input and output arguments. 非Python程序将同时使用输入和输出参数来调用Python程序。 Input may be a file reference or a string redirected to stdin in the non-Python program. 输入可以是文件引用,也可以是在非Python程序中重定向到stdin的字符串。 Output may be a file or stdout. 输出可能是文件或标准输出。

argparse.FileType seems ready to handle this as it already has the special - to direct to stdin/stdout. argparse.FileType似乎已准备好处理此问题,因为它已经具有特殊功能-可以直接指向stdin / stdout。 In fact, using - to direct to stdout works but the implementation/syntax for stdin is what I don't know. 实际上,使用-定向到stdout可以,但是我不知道stdin的实现/语法。

Examples calls in the non-Python code: 非Python代码中的示例调用:
python mycode.py - output.txt
python mycode.py - -

What does the non-Python code do after that? 之后,非Python代码会做什么? Print/stdout an input string? 打印/输出一个输入字符串?
What does the Python code do after that? 之后,Python代码做什么?

I will always need to distinguish where both args are going (ie input and output) so using default="-" nor default=sys.stdin in add_argument won't work because they require an absent argument. 我将始终需要区分两个arg的default=sys.stdin (即输入和输出),因此在add_argument使用default="-"default=sys.stdin add_argument都不起作用,因为它们需要一个不存在的参数。

Here's what I have so far: 这是我到目前为止的内容:

parser = argparse.ArgumentParser()

parser.add_argument('read_fref', type=argparse.FileType('r'))
parser.add_argument('write_fref', type=argparse.FileType('w'))
parser_ns = parser.parse_args()

with parser_ns.read_fref as f_r:
    read_f = json.load(f_r)    

output = {'k': 'v'}

with parser_ns.write_fref as f_w:
    json.dump(output, f_w)

I'm having trouble understanding what you are asking. 我无法理解您的要求。 I understand what Python and argparse are doing, but I don't quite understand what you are trying to do. 我了解Python和argparse在做什么,但是我不太了解您正在尝试做什么。

Your sample looks like it would run fine when called from a Linux shell. 从Linux shell调用时,您的示例看起来运行良好。 With the the - arguments, it should accept input from the keyboard, and display it on the screen. 使用-参数,它应该接受来自键盘的输入,并将其显示在屏幕上。 But those arguments are most often used with shell redirection controls >, <, | 但是这些参数最常与shell重定向控件>, <, | (details vary with shell, sh , bash , etc). (具体细节取决于shell, shbash等)。

But if you are using the shell to redirect stdin or stdout to/from files, you could just as well give those files as commandline arguments. 但是,如果您使用外壳将stdinstdout重定向到文件或从文件重定向,则可以将这些文件作为命令行参数来使用。

If you are bothered by required/default issue, consider making these arguments flagged (also called optionals ): 如果您被必需/默认问题所困扰,请考虑将这些参数标记为(也称为optionals ):

parser.add_argument('-r','--readfile', type=argparse.FileType('r'), default='-')
parser.add_argument('-w','--writefile', type=argparse.FileType('w'), default='-')

With this change, these calls are the same 进行此更改后,这些调用是相同的

python mycode.py -r - <test.json
python mycode.py <test.json
python mycode.py -r test.json

all writing to the screen (stdout). 全部写入屏幕(stdout)。 That could be redirected in similar ways. 可以通过类似的方式重定向。

To take typed input: 要输入输入内容:

python mycode.py
{...}
^D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM