Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: update command line arguments (breaking change) #44

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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