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 9, 2021
1 parent 7d146bd commit e0c81d7
Show file tree
Hide file tree
Showing 8 changed files with 43 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
21 changes: 21 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import doctest
import unittest
import sys
import os
import copy
import subprocess
from jsonpointer import resolve_pointer, EndOfList, JsonPointerException, \
JsonPointer, set_pointer

Expand Down Expand Up @@ -298,6 +300,24 @@ 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"],
env={"PYTHONPATH": os.getcwd()},
)
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"],
env={"PYTHONPATH": os.getcwd()},
)
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 +326,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 e0c81d7

Please sign in to comment.