diff options
author | Hervé Beraud <herveberaud.pro@gmail.com> | 2019-05-14 16:52:42 (GMT) |
---|---|---|
committer | Victor Stinner <vstinner@redhat.com> | 2019-05-14 16:52:42 (GMT) |
commit | 4d45a3b1107977baba9dce868e80d1d95bce4085 (patch) | |
tree | 98b198113f2b2fb0c91b85dcc939ab2e9a20a2aa | |
parent | f12ba7cd0a7370631214ac0b337ab5455ce717b2 (diff) | |
download | cpython-4d45a3b1107977baba9dce868e80d1d95bce4085.zip cpython-4d45a3b1107977baba9dce868e80d1d95bce4085.tar.gz cpython-4d45a3b1107977baba9dce868e80d1d95bce4085.tar.bz2 |
json.tool: use stdin and stdout in default cmdlne arguments (GH-11992)
Argparse can handle default value as stdin and stdout for parameters
as file type (infile, outfile).
-rw-r--r-- | Lib/json/tool.py | 10 |
1 files changed, 6 insertions, 4 deletions
diff --git a/Lib/json/tool.py b/Lib/json/tool.py index 1d82bc8..b3ef992 100644 --- a/Lib/json/tool.py +++ b/Lib/json/tool.py @@ -21,17 +21,19 @@ def main(): 'to validate and pretty-print JSON objects.') parser = argparse.ArgumentParser(prog=prog, description=description) parser.add_argument('infile', nargs='?', type=argparse.FileType(), - help='a JSON file to be validated or pretty-printed') + help='a JSON file to be validated or pretty-printed', + default=sys.stdin) parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'), - help='write the output of infile to outfile') + help='write the output of infile to outfile', + default=sys.stdout) parser.add_argument('--sort-keys', action='store_true', default=False, help='sort the output of dictionaries alphabetically by key') parser.add_argument('--json-lines', action='store_true', default=False, help='parse input using the jsonlines format') options = parser.parse_args() - infile = options.infile or sys.stdin - outfile = options.outfile or sys.stdout + infile = options.infile + outfile = options.outfile sort_keys = options.sort_keys json_lines = options.json_lines with infile, outfile: |