Skip to content

Commit 631c722

Browse files
cli: update command line arguments (breaking change)
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
1 parent 7d146bd commit 631c722

File tree

8 files changed

+37
-7
lines changed

8 files changed

+37
-7
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Change Log
2+
3+
## x.y (unreleased)
4+
5+
### Breaking Changes
6+
7+
The `jsonpointer` commandline utility accepts either a file containing a JSON pointer expression (example: `jsonpointer -f ptr.json a.json b.json`) or
8+
the expression as a commandline argument (example: `jsonpointer -p "/a" a.json b.json`).

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
include AUTHORS
22
include LICENSE.txt
33
include README.md
4+
include CHANGELOG.md
45
include tests.py

bin/jsonpointer

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ ptr_group.add_argument('-f', '--pointer-file', type=argparse.FileType('r'),
2020
nargs='?',
2121
help='File containing a JSON pointer expression')
2222

23-
ptr_group.add_argument('POINTER', type=str, nargs='?',
23+
ptr_group.add_argument('-p', '--pointer', type=str, nargs='?',
2424
help='A JSON pointer expression')
2525

2626
parser.add_argument('FILE', type=argparse.FileType('r'), nargs='+',
@@ -39,8 +39,8 @@ def main():
3939

4040

4141
def parse_pointer(args):
42-
if args.POINTER:
43-
ptr = args.POINTER
42+
if args.pointer:
43+
ptr = args.pointer
4444
elif args.pointer_file:
4545
ptr = args.pointer_file.read().strip()
4646
else:

doc/commandline.rst

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,19 @@ that can be used to resolve a JSON pointers on JSON files.
66

77
The program has the following usage ::
88

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

1111
Resolve a JSON pointer on JSON files
1212

1313
positional arguments:
14-
POINTER File containing a JSON pointer expression
1514
FILE Files for which the pointer should be resolved
1615

1716
optional arguments:
1817
-h, --help show this help message and exit
18+
-f [POINTER_FILE], --pointer-file [POINTER_FILE]
19+
File containing a JSON pointer expression
20+
-p [POINTER], --pointer [POINTER]
21+
A JSON pointer expression
1922
--indent INDENT Indent output by n spaces
2023
-v, --version show program's version number and exit
2124

@@ -34,9 +37,9 @@ Example
3437
3538
# inspect JSON pointer
3639
$ cat ptr.json
37-
"/a"
40+
/a
3841
3942
# resolve JSON pointer
40-
$ jsonpointer ptr.json a.json b.json
43+
$ jsonpointer -f ptr.json a.json b.json
4144
[1, 2, 3]
4245
{"b": [1, 3, 4]}

test/a.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "a": [1, 2, 3] }

test/b.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "a": {"b": [1, 3, 4]}, "b": 1 }

test/ptr.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/a

tests.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import unittest
88
import sys
99
import copy
10+
import subprocess
1011
from jsonpointer import resolve_pointer, EndOfList, JsonPointerException, \
1112
JsonPointer, set_pointer
1213

@@ -298,6 +299,19 @@ def test_mock_dict_raises_key_error(self):
298299
self.assertRaises(JsonPointerException, resolve_pointer, doc, '/root/1/2/3/4')
299300

300301

302+
class CommandLineTests(unittest.TestCase):
303+
""" Tests the command line """
304+
305+
def test_file(self):
306+
output = subprocess.check_output(["jsonpointer", "-f", "ptr.json", "a.json", "b.json"], cwd="test")
307+
self.assertEqual(output, b'[1, 2, 3]\n{"b": [1, 3, 4]}\n')
308+
309+
310+
def test_pointerarg(self):
311+
output = subprocess.check_output(["jsonpointer", "-p", "/a", "a.json", "b.json"], cwd="test")
312+
self.assertEqual(output, b'[1, 2, 3]\n{"b": [1, 3, 4]}\n')
313+
314+
301315

302316
suite = unittest.TestSuite()
303317
suite.addTest(unittest.makeSuite(SpecificationTests))
@@ -306,6 +320,7 @@ def test_mock_dict_raises_key_error(self):
306320
suite.addTest(unittest.makeSuite(ToLastTests))
307321
suite.addTest(unittest.makeSuite(SetTests))
308322
suite.addTest(unittest.makeSuite(AltTypesTests))
323+
suite.addTest(unittest.makeSuite(CommandLineTests))
309324

310325
modules = ['jsonpointer']
311326

0 commit comments

Comments
 (0)