Created
April 6, 2026 16:06
-
-
Save daturkel/1fde99f92b0e32dc078083c2ae716372 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| # /// script | |
| # requires-python = ">=3.10" | |
| # /// | |
| """Collapse a shell command with line-continuation backslashes into a single line.""" | |
| import re | |
| import sys | |
| def fix_command(raw: str) -> str: | |
| """Join shell line continuations into a single executable command. | |
| Args: | |
| raw: Multi-line shell command with trailing backslash continuations. | |
| Returns: | |
| Single-line command ready to paste into a terminal. | |
| """ | |
| # Strip trailing whitespace (including invisible chars) from each line, | |
| # then join continuation lines (ending in backslash) with a space. | |
| lines = raw.splitlines() | |
| joined: list[str] = [] | |
| for line in lines: | |
| line = line.rstrip() | |
| if line.endswith("\\"): | |
| joined.append(line[:-1].rstrip()) | |
| else: | |
| joined.append(line) | |
| # Re-join: each stripped continuation becomes a space-separated token | |
| result = " ".join(part.strip() for part in joined if part.strip()) | |
| return result | |
| def main() -> None: | |
| if not sys.stdin.isatty(): | |
| raw = sys.stdin.read() | |
| elif len(sys.argv) > 1: | |
| raw = " ".join(sys.argv[1:]) | |
| else: | |
| print("Usage: fixcmd < command.txt OR pbpaste | fixcmd", file=sys.stderr) | |
| sys.exit(1) | |
| print(fix_command(raw)) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment