Skip to content

Commit

Permalink
cli: update command line arguments (breaking change)
Browse files Browse the repository at this point in the history
This commit updates the CLI arguments to accept either a file containing
a json pointer expression, or an expression as a commandline argument:

    usage: jsonpointer [-h] (-f [POINTER_FILE] | -p [POINTER]) [--indent INDENT] [-v] FILE [FILE ...]

Without this commit

    jsonpointer "/a" a.json b.json

works fine, however using a file doesn't work:

    $ jsonpointer -f ptr.json a.json b.json
    usage: jsonpointer [-h] [-f [POINTER_FILE]] [--indent INDENT] [-v] [POINTER] FILE [FILE ...]
    jsonpointer: error: argument POINTER: not allowed with argument -f/--pointer-file

This commit also improves the usage message by explaining the mutually exclusive arguments,
adds tests for the changes, updates the documentation and adds a new CHANGELOG.md file.

Resolves stefankoegl#43
  • Loading branch information
andreasgerstmayr committed Aug 5, 2021
1 parent 7d146bd commit 631c722
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 7 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`).
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
include AUTHORS
include LICENSE.txt
include README.md
include CHANGELOG.md
include tests.py
6 changes: 3 additions & 3 deletions bin/jsonpointer
Original file line number Diff line number Diff line change
Expand Up @@ -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='+',
Expand All @@ -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:
Expand Down
11 changes: 7 additions & 4 deletions doc/commandline.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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]}
1 change: 1 addition & 0 deletions test/a.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "a": [1, 2, 3] }
1 change: 1 addition & 0 deletions test/b.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "a": {"b": [1, 3, 4]}, "b": 1 }
1 change: 1 addition & 0 deletions test/ptr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/a
15 changes: 15 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import unittest
import sys
import copy
import subprocess
from jsonpointer import resolve_pointer, EndOfList, JsonPointerException, \
JsonPointer, set_pointer

Expand Down Expand Up @@ -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(["jsonpointer", "-f", "ptr.json", "a.json", "b.json"], cwd="test")
self.assertEqual(output, b'[1, 2, 3]\n{"b": [1, 3, 4]}\n')


def test_pointerarg(self):
output = subprocess.check_output(["jsonpointer", "-p", "/a", "a.json", "b.json"], cwd="test")
self.assertEqual(output, b'[1, 2, 3]\n{"b": [1, 3, 4]}\n')



suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(SpecificationTests))
Expand All @@ -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']

Expand Down

0 comments on commit 631c722

Please sign in to comment.