Skip to content

feat: add --exit-status / -e flag (exit code 1 for false/null/empty output)#1823

Open
sridhar-3009 wants to merge 1 commit into
harehare:mainfrom
sridhar-3009:feat/exit-status-flag
Open

feat: add --exit-status / -e flag (exit code 1 for false/null/empty output)#1823
sridhar-3009 wants to merge 1 commit into
harehare:mainfrom
sridhar-3009:feat/exit-status-flag

Conversation

@sridhar-3009
Copy link
Copy Markdown

Summary

Closes #1799.

Adds -e / --exit-status to mq, mirroring jq's --exit-status behaviour:

Output Exit code
At least one truthy value 0
Only false or null values 1
No output 1

Everything else — empty string "", 0, [], {} — is truthy and exits 0.

Usage

# Exits 1 — heading not found
mq -e 'select(.type == "heading" and .depth == 99)' doc.md

# Exits 0 — found something
mq -e 'select(.type == "heading")' doc.md

# Useful in shell scripts
if mq -e 'select(.type == "code")' README.md > /dev/null; then
  echo "has code blocks"
fi

Implementation

  • OutputArgs.exit_status: bool — new clap flag (-e / --exit-status)
  • is_falsy() helper — matches RuntimeValue::None | Boolean(false)
  • thread_local! { HAD_TRUTHY_OUTPUT } — tracks across print() calls without refactoring the streaming output architecture
  • After process_batch / process_streaming, calls std::process::exit(1) when the flag is set and no truthy value was seen

…ty output

Mirrors jq's --exit-status behaviour:

- exit 1 if the output contains only false/null values
- exit 1 if no output was produced
- exit 0 for any other output (including empty string, 0, [], {})

Implementation:
- Add OutputArgs.exit_status: bool (-e / --exit-status)
- Add is_falsy() helper (matches RuntimeValue::None | Boolean(false))
- Use thread_local! HAD_TRUTHY_OUTPUT to track across print() calls
  without requiring a large refactor of the streaming output architecture
- After process_batch/streaming, call std::process::exit(1) if
  exit_status is set and no truthy value was seen

Closes harehare#1799
@harehare
Copy link
Copy Markdown
Owner

harehare commented Jun 5, 2026

@sridhar-3009
Thanks for the PR!

It looks like some tests are failing in CI. Could you take a look when you get a chance?

Let me know if you need any help!

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 -e / --exit-status flag to return exit code 1 for None/false/empty results

2 participants