Skip to content

Commit

Permalink
first parse stdin, otherwise it is passed to textual
Browse files Browse the repository at this point in the history
  • Loading branch information
getzze committed Dec 4, 2024
1 parent e47e315 commit 14324e9
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions rexi/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,9 @@ def rexi_cli(
If `--input FILE` is used, read the file content.
"""
# Input file provided
if input_file:
# Incompatible with a string argument
if text:
msg = (
"TEXT argument should be empty if in input is provided with "
"the `-i` flag."
)
raise typer.BadParameter(msg)

input_text = input_file.read()

# Read stdin
elif not is_stdin_a_tty():
# Should be first, otherwise stdin is passed to textual
if not is_stdin_a_tty():
input_text = sys.stdin.read()
try:
os.close(sys.stdin.fileno())
Expand All @@ -78,11 +67,28 @@ def rexi_cli(
# Windows uses "con:" for stdin device name
sys.stdin = open("con:" if os.name == "nt" else "/dev/tty", "rb") # type: ignore[assignment]

# Incompatible with an input file argument
if input_file:
msg = "INPUT option should not be defined if text is piped through stdin."
raise typer.BadParameter(msg)

# Incompatible with a string argument
if text:
msg = "TEXT argument should be empty if text is piped through stdin."
raise typer.BadParameter(msg)

# Input file provided
elif input_file:
# Incompatible with a string argument
if text:
msg = (
"TEXT argument should be empty if in input is provided with "
"the `-i` flag."
)
raise typer.BadParameter(msg)

input_text = input_file.read()

# Input string provided or fallback to empty string
else:
input_text = text or ""
Expand Down

0 comments on commit 14324e9

Please sign in to comment.