diff --git a/rexi/cli.py b/rexi/cli.py index ae8e98a..448865e 100644 --- a/rexi/cli.py +++ b/rexi/cli.py @@ -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()) @@ -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 ""