Skip to content

Commit faac2fd

Browse files
ryan-williamsclaude
andcommitted
Add -r/--exit-code option to bmd for expected non-zero exit codes
Allows specifying an expected exit code for commands that exit non-zero as normal behavior (e.g., `diff` exits 1 when files differ). When set, `bmd` exits 0 if the command exits with the expected code, 1 otherwise. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent e6867cb commit faac2fd

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

src/bmdf/cli/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
@option('-i/-I', '--include-stderr/--no-include-stderr', is_flag=True, default=None, help=f'Capture and interleave both stdout and stderr streams; falls back to ${BMDF_INCLUDE_STDERR_VAR}')
3838
@option('-s/-S', '--shell/--no-shell', is_flag=True, default=None, help=f'Disable "shell" mode for the command; falls back to ${BMDF_SHELL_VAR}, but defaults to True if neither is set')
3939
@option('-t', '--fence-type', help="When -f/--fence is 2 or 3, this customizes the fence syntax type that the output is wrapped in")
40+
@option('-r', '--exit-code', type=int, default=None, help='Expected exit code; bmdf exits 0 if command exits with this code, non-zero otherwise (useful for diff commands that exit 1 on differences)')
4041
@option('-u/-U', '--expanduser/--no-expanduser', is_flag=True, default=None, help=f'Pass commands through `os.path.expanduser` before `subprocess`; falls back to ${BMDF_EXPANDUSER_VAR}')
4142
@option('-v/-V', '--expandvars/--no-expandvars', is_flag=True, default=None, help=f'Pass commands through `os.path.expandvars` before `subprocess`; falls back to ${BMDF_EXPANDVARS_VAR}')
4243
@option('-w', '--workdir', help=f'`cd` to this directory before executing (falls back to ${BMDF_WORKDIR_VAR}')
@@ -52,6 +53,7 @@ def bmd(
5253
include_stderr: bool = False,
5354
shell: Optional[bool] = None,
5455
fence_type: Optional[str] = None,
56+
exit_code: Optional[int] = None,
5557
expanduser: Optional[bool] = None,
5658
expandvars: Optional[bool] = None,
5759
workdir: Optional[str] = None,
@@ -211,7 +213,11 @@ def print_fenced_lines(typ: str = None):
211213

212214
file = file or stdout
213215
print(output, file=file)
214-
if returncode != 0:
216+
if exit_code is not None:
217+
# Expected exit code specified: exit 0 if it matches, 1 otherwise
218+
if returncode != exit_code:
219+
sys.exit(1)
220+
elif returncode != 0:
215221
sys.exit(returncode)
216222

217223

0 commit comments

Comments
 (0)