Add --json output for read, scan, and dump#8
Conversation
There was a problem hiding this comment.
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
--jsonoutput mode forread,scan, anddumpcommands. - Emit structured JSON payloads (host/target/type/slave + values/devices/registers) when
--jsonis 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.
| 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() |
There was a problem hiding this comment.
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.
| client = ModbusTcpClient(host=host, port=port, timeout=timeout) | ||
|
|
||
| if not client.connect(): | ||
| continue | ||
|
|
There was a problem hiding this comment.
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.
| - **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 |
There was a problem hiding this comment.
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.
| @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): |
There was a problem hiding this comment.
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.
|
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. |
Summary
ead, scan, and dump
Closes #1