Skip to content

Add --json output for read, scan, and dump#8

Merged
19bk merged 1 commit into
19bk:mainfrom
zakiscoding:feat/issue-1-json-output
Mar 12, 2026
Merged

Add --json output for read, scan, and dump#8
19bk merged 1 commit into
19bk:mainfrom
zakiscoding:feat/issue-1-json-output

Conversation

@zakiscoding
Copy link
Copy Markdown
Contributor

Summary

  • add a new --json flag to
    ead, scan, and dump
  • keep existing rich table output as the default behavior
  • emit machine-readable JSON for scripting and jq pipelines
  • document JSON usage examples in README

Closes #1

Copilot AI review requested due to automatic review settings March 12, 2026 16:45
@19bk 19bk merged commit 3340edb into 19bk:main Mar 12, 2026
4 of 5 checks passed
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds a --json flag to key CLI commands so modbus-cli can emit machine-readable output suitable for scripting, while keeping the existing Rich table UI as the default.

Changes:

  • Add --json output mode for read, scan, and dump commands.
  • Emit structured JSON payloads (host/target/type/slave + values/devices/registers) when --json is used.
  • Document JSON usage examples in the README.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
modbus_cli/cli.py Adds --json flag handling and JSON emission for read, scan, and dump.
README.md Documents JSON output feature and provides example commands.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment thread modbus_cli/cli.py
Comment on lines +153 to +158
if json_output:
client = _make_client(host, port, serial, baudrate, slave, timeout)
try:
resp = _read_registers(client, detected_type, raw_address, count, slave)
finally:
client.close()
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

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

In read --json, failures from _make_client / _read_registers currently use error_panel(...) and sys.exit(1), which writes Rich-formatted output to stdout. That makes --json output non-machine-readable on error paths. Consider adding a JSON-friendly error path when json_output is set (e.g., emit a JSON error object to stderr and exit non-zero, or raise click.ClickException with err=True output) and avoid calling error_panel in JSON mode.

Copilot uses AI. Check for mistakes.
Comment thread modbus_cli/cli.py
Comment on lines +348 to +352
client = ModbusTcpClient(host=host, port=port, timeout=timeout)

if not client.connect():
continue

Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

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

In the scan --json loop, when client.connect() returns false the code continues without closing the client, which can leak sockets/file descriptors across a large scan range. Close the client before continuing (or use a try/finally that always closes) so each iteration reliably releases resources.

Copilot uses AI. Check for mistakes.
Comment thread README.md
- **Multiple formats** -- decimal, hex, binary, signed 16-bit
- **Progress bars** -- animated scan and dump with real-time feedback
- **CSV export** -- dump register maps straight to file
- **JSON output** -- pipe read/scan/dump output into jq and automation tools
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

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

README documents --json usage in examples, but the "Options" table below does not list --json anywhere. Adding it there (and noting it applies to read, scan, and dump) would keep the flag reference documentation complete and consistent.

Copilot uses AI. Check for mistakes.
Comment thread modbus_cli/cli.py
Comment on lines 524 to +527
@click.option("--csv", "csv_out", default=None, help="Export to CSV file.")
@click.option("--json", "json_output", is_flag=True, help="Output as JSON.")
@click.option("--timeout", default=3.0, help="Timeout in seconds (default: 3).")
def dump(host, start_address, end_address, port, serial, baudrate, slave, reg_type, fmt, csv_out, timeout):
def dump(host, start_address, end_address, port, serial, baudrate, slave, reg_type, fmt, csv_out, json_output, timeout):
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

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

dump --json currently still prints Rich connection/status and the progress bar to stdout before emitting the JSON payload, which will break piping to tools like jq. Consider short-circuiting earlier when json_output is set (skip console.print(), connection_header, and Progress), or redirect all non-JSON UI output to stderr so stdout remains valid JSON.

Copilot uses AI. Check for mistakes.
@19bk
Copy link
Copy Markdown
Owner

19bk commented Mar 12, 2026

Hey @zakiscoding, really appreciate you jumping in with these PRs. You're the first contributor to this project and you knocked out two features in one sitting. The JSON output and Docker image are both merged.

The float decoding PR (#6) just needs a rebase on main since it conflicts with the JSON changes. Once that's done I'll merge it too.

If you want to keep going, issue #2 (register map files) is still open and it's the most impactful feature left. Would love to see your take on it.

@19bk 19bk mentioned this pull request Mar 12, 2026
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.

Add --json output format

3 participants