Skip to content

Commit cb9fd8e

Browse files
committed
cli: reject invalid pointers with a usage error
When the pointer was omitted, e.g. `jsonpointer a.json b.json`, the first file was taken as the pointer, every file failed to resolve and the command still exited with status 0. Validate the pointer before processing any files and exit with a usage error if it is not a valid JSON pointer. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019xLCiTJTbWQ9Nt7UHq2yiU
1 parent 71f9b9e commit cb9fd8e

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

‎bin/jsonpointer‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ def parse_pointer(args):
6161
else:
6262
parser.error('a JSON pointer is required (POINTER, -p or -f)')
6363

64+
# Catches e.g. `jsonpointer a.json b.json`, where the pointer was omitted
65+
# and the first file would otherwise be used as the pointer
66+
try:
67+
jsonpointer.JsonPointer(ptr)
68+
except jsonpointer.JsonPointerException as e:
69+
parser.error('invalid JSON pointer %r: %s' % (ptr, e))
70+
6471
return ptr, files
6572

6673

‎tests.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,23 @@ def test_no_pointer(self):
529529
self.assertEqual(proc.returncode, 2)
530530
self.assertIn('a JSON pointer is required', proc.stderr)
531531

532+
def test_missing_pointer_multiple_files(self):
533+
proc = self._run('a.json', 'b.json')
534+
self.assertEqual(proc.returncode, 2)
535+
self.assertIn("invalid JSON pointer 'a.json'", proc.stderr)
536+
self.assertEqual(proc.stdout, '')
537+
538+
def test_invalid_pointer_file(self):
539+
self._write('bad.txt', 'a\n')
540+
proc = self._run('-f', 'bad.txt', 'a.json')
541+
self.assertEqual(proc.returncode, 2)
542+
self.assertIn("invalid JSON pointer 'a'", proc.stderr)
543+
544+
def test_empty_pointer(self):
545+
proc = self._run('', 'a.json')
546+
self.assertEqual(proc.returncode, 0, proc.stderr)
547+
self.assertEqual(proc.stdout, '{"a": [1, 2, 3]}\n')
548+
532549
def test_pointer_and_pointer_file_exclusive(self):
533550
proc = self._run('-p', '/a', '-f', 'ptr.txt', 'a.json')
534551
self.assertEqual(proc.returncode, 2)

0 commit comments

Comments
 (0)