diff --git a/Lib/json/tool.py b/Lib/json/tool.py --- a/Lib/json/tool.py +++ b/Lib/json/tool.py @@ -12,16 +12,25 @@ Usage:: """ import sys import json def main(): if len(sys.argv) == 1: infile = sys.stdin outfile = sys.stdout + elif len(sys.argv) == 2 and sys.argv[1] in {'-h', '--help'}: + usage = """usage: %s -m json.tool [infile [outfile]] + +optional arguments: + + -h, --help show this help message and exit + """ + print(usage % sys.executable) + sys.exit(0) elif len(sys.argv) == 2: infile = open(sys.argv[1], 'r') outfile = sys.stdout elif len(sys.argv) == 3: infile = open(sys.argv[1], 'r') outfile = open(sys.argv[2], 'w') else: raise SystemExit(sys.argv[0] + " [infile [outfile]]") diff --git a/Lib/test/test_json/test_tool.py b/Lib/test/test_json/test_tool.py --- a/Lib/test/test_json/test_tool.py +++ b/Lib/test/test_json/test_tool.py @@ -62,8 +62,14 @@ class TestTool(unittest.TestCase): infile = self._create_infile() outfile = support.TESTFN + '.out' rc, out, err = assert_python_ok('-m', 'json.tool', infile, outfile) self.addCleanup(os.remove, outfile) with open(outfile, "r") as fp: self.assertEqual(fp.read(), self.expect) self.assertEqual(out, b'') self.assertEqual(err, b'') + + def test_help_flag(self): + rc, out, err = assert_python_ok('-m', 'json.tool', '-h') + self.assertEqual(rc, 0) + self.assertTrue(out.startswith(b'usage: ')) + self.assertEqual(err, b'')