[lang-ref] ( example_switch_input_stdin_or_file ) ( python )
def test_example_switch_input_stdin_or_file(monkeypatch):
#
# Note: argparse.FileType("r") is pending deprecation,
# so we open files after parsing.
import argparse
import sys, io
text = 'A1,B1,C1\nA2,B2,C2\nA3,B3,C3\n'
monkeypatch.setattr(sys, 'stdin', io.StringIO(text))
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("input_file", nargs="?", default="-", help='Use "-" to read from stdin.')
args = parser.parse_args([])
if args.input_file == '-':
r = sys.stdin.read()
else:
# omitted details:
# - check file existence
# - with open(args.input_file, encoding="utf-8") as f: ...
raise AssertionError("not covered in this test")
assert r == text