diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..3c34674 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,8 @@ +# Change Log + +## x.y (unreleased) + +### Breaking Changes + +The `jsonpointer` commandline utility accepts either a file containing a JSON pointer expression (example: `jsonpointer -f ptr.json a.json b.json`) or +the expression as a commandline argument (example: `jsonpointer -p "/a" a.json b.json`). diff --git a/MANIFEST.in b/MANIFEST.in index 0f9b9f6..5ff096b 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,4 +1,5 @@ include AUTHORS include LICENSE.txt include README.md +include CHANGELOG.md include tests.py diff --git a/bin/jsonpointer b/bin/jsonpointer index d577d01..bc5c506 100755 --- a/bin/jsonpointer +++ b/bin/jsonpointer @@ -20,7 +20,7 @@ ptr_group.add_argument('-f', '--pointer-file', type=argparse.FileType('r'), nargs='?', help='File containing a JSON pointer expression') -ptr_group.add_argument('POINTER', type=str, nargs='?', +ptr_group.add_argument('-p', '--pointer', type=str, nargs='?', help='A JSON pointer expression') parser.add_argument('FILE', type=argparse.FileType('r'), nargs='+', @@ -39,8 +39,8 @@ def main(): def parse_pointer(args): - if args.POINTER: - ptr = args.POINTER + if args.pointer: + ptr = args.pointer elif args.pointer_file: ptr = args.pointer_file.read().strip() else: diff --git a/doc/commandline.rst b/doc/commandline.rst index b4a01de..66f6cf8 100644 --- a/doc/commandline.rst +++ b/doc/commandline.rst @@ -6,16 +6,19 @@ that can be used to resolve a JSON pointers on JSON files. The program has the following usage :: - usage: jsonpointer [-h] [--indent INDENT] [-v] POINTER FILE [FILE ...] + usage: jsonpointer [-h] (-f [POINTER_FILE] | -p [POINTER]) [--indent INDENT] [-v] FILE [FILE ...] Resolve a JSON pointer on JSON files positional arguments: - POINTER File containing a JSON pointer expression FILE Files for which the pointer should be resolved optional arguments: -h, --help show this help message and exit + -f [POINTER_FILE], --pointer-file [POINTER_FILE] + File containing a JSON pointer expression + -p [POINTER], --pointer [POINTER] + A JSON pointer expression --indent INDENT Indent output by n spaces -v, --version show program's version number and exit @@ -34,9 +37,9 @@ Example # inspect JSON pointer $ cat ptr.json - "/a" + /a # resolve JSON pointer - $ jsonpointer ptr.json a.json b.json + $ jsonpointer -f ptr.json a.json b.json [1, 2, 3] {"b": [1, 3, 4]} diff --git a/test/a.json b/test/a.json new file mode 100644 index 0000000..a52df9c --- /dev/null +++ b/test/a.json @@ -0,0 +1 @@ +{ "a": [1, 2, 3] } diff --git a/test/b.json b/test/b.json new file mode 100644 index 0000000..286ca6b --- /dev/null +++ b/test/b.json @@ -0,0 +1 @@ +{ "a": {"b": [1, 3, 4]}, "b": 1 } diff --git a/test/ptr.json b/test/ptr.json new file mode 100644 index 0000000..5ff0a6e --- /dev/null +++ b/test/ptr.json @@ -0,0 +1 @@ +/a diff --git a/tests.py b/tests.py index 54ca436..80b4ac0 100755 --- a/tests.py +++ b/tests.py @@ -7,6 +7,7 @@ import unittest import sys import copy +import subprocess from jsonpointer import resolve_pointer, EndOfList, JsonPointerException, \ JsonPointer, set_pointer @@ -298,6 +299,19 @@ def test_mock_dict_raises_key_error(self): self.assertRaises(JsonPointerException, resolve_pointer, doc, '/root/1/2/3/4') +class CommandLineTests(unittest.TestCase): + """ Tests the command line """ + + def test_file(self): + output = subprocess.check_output(["bin/jsonpointer", "-f", "test/ptr.json", "test/a.json", "test/b.json"]) + self.assertEqual(output, b'[1, 2, 3]\n{"b": [1, 3, 4]}\n') + + + def test_pointerarg(self): + output = subprocess.check_output(["bin/jsonpointer", "-p", "/a", "test/a.json", "test/b.json"]) + self.assertEqual(output, b'[1, 2, 3]\n{"b": [1, 3, 4]}\n') + + suite = unittest.TestSuite() suite.addTest(unittest.makeSuite(SpecificationTests)) @@ -306,6 +320,7 @@ def test_mock_dict_raises_key_error(self): suite.addTest(unittest.makeSuite(ToLastTests)) suite.addTest(unittest.makeSuite(SetTests)) suite.addTest(unittest.makeSuite(AltTypesTests)) +suite.addTest(unittest.makeSuite(CommandLineTests)) modules = ['jsonpointer']