Skip to content

Improve jsonpointer CLI: support positional pointer argument - #81

Open
stefankoegl wants to merge 4 commits into
masterfrom
claude/modest-allen-i9u4xu
Open

stefankoegl wants to merge 4 commits into
masterfrom
claude/modest-allen-i9u4xu

Conversation

@stefankoegl

Copy link
Copy Markdown
Owner

Summary

This PR enhances the jsonpointer command-line utility to accept the JSON pointer as a positional argument, in addition to the existing -f (file) and -p (option) methods. This makes the CLI more intuitive and user-friendly.

Key Changes

  • Flexible pointer specification: The JSON pointer can now be provided in three ways:

    • As the first positional argument: jsonpointer /a file.json
    • Via -p option: jsonpointer -p /a file.json
    • Via -f option reading from file: jsonpointer -f ptr.txt file.json
  • Improved argument parsing:

    • Made the mutually exclusive group optional (no longer required=True)
    • Added -p/--pointer option for explicit pointer specification
    • Changed FILE argument from FileType('r') to str to allow flexible file handling
    • Implemented parse_pointer() function to intelligently determine which argument is the pointer and which are files
  • Better error handling:

    • Proper error message when no pointer is provided
    • Validation that -p and -f are mutually exclusive
    • File opening errors are caught and reported via parser.error()
  • Comprehensive test coverage: Added CommandLineTests class with 6 test cases covering:

    • Positional pointer with single and multiple files
    • Pointer file (-f) with single and multiple files
    • Pointer option (-p) with multiple files
    • Error cases (missing pointer, conflicting options)
  • Updated documentation: Clarified usage examples and help text to reflect the new positional argument capability

Implementation Details

The key logic change is in parse_pointer() which now:

  1. Checks if -p or -f options are provided
  2. If so, treats the POINTER positional argument as the first file
  3. If neither option is provided, uses POINTER as the actual pointer
  4. Returns both the pointer and the complete file list for processing

https://claude.ai/code/session_019xLCiTJTbWQ9Nt7UHq2yiU

`jsonpointer -f ptr.txt a.json b.json` failed because argparse assigned
a.json to the optional positional POINTER, which was in a mutually
exclusive group with -f.

Move POINTER out of the group and treat it as the first file whenever the
pointer is given via -f or the new -p/--pointer option. The existing
`jsonpointer /a a.json b.json` usage keeps working unchanged.

Fixes #43, backwards compatible alternative to #44.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019xLCiTJTbWQ9Nt7UHq2yiU

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Multi-file invocations without a pointer can incorrectly exit successfully after processing arguments incorrectly.

Review effort: Balanced
Findings: None

What changed in this PR

Adds positional JSON pointer support while retaining -p and -f input methods.

Changes:

  • Adds flexible pointer and file argument parsing.
  • Improves CLI error handling and tests.
  • Updates command-line documentation and examples.
File Description
bin/​jsonpointer Implements positional pointer parsing and deferred file opening.
tests.py Adds CLI integration tests.
doc/​commandline.rst Documents the expanded CLI syntax.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

A bare -f bypasses the intended mutual exclusion with -p.

Review effort: Balanced
Findings: None

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants