Stdin¶
Interfacy can route piped stdin into command parameters.
from interfacy import Interfacy
def echo(message: str) -> str:
"""Echo a message."""
return message
Interfacy(print_result=True, pipe_targets="message").run(echo)
$ echo "hello" | python app.py
hello
Per-command routing¶
Pipe targets can be configured on a single command.
parser = Interfacy(print_result=True)
parser.add_command(echo, pipe_targets="message")
parser.run()
Multiple targets¶
When more than one parameter is targeted, stdin is split into chunks. Newlines are the default delimiter.
def pair(left: str, right: str) -> tuple[str, str]:
return left, right
parser.add_command(pair, pipe_targets=("left", "right"))
$ printf "one\ntwo" | python app.py
The command receives ("one", "two").
Custom delimiter¶
A dict configures delimiter and priority settings.
parser.add_command(
pair,
pipe_targets={"bindings": ("left", "right"), "delimiter": ","},
)
$ printf "one,two" | python app.py
parameters and bindings are both accepted as the target-key name.
Lists¶
If the target is a list, piped chunks become list elements.
def collect(items: list[int]) -> list[int]:
return items
parser.add_command(collect, pipe_targets="items")
$ printf "1\n2\n3" | python app.py
The command receives [1, 2, 3].
Priority¶
CLI values win over stdin by default.
parser.add_command(echo, pipe_targets="message")
$ echo piped | python app.py cli
The command receives "cli".
priority="pipe" makes stdin override explicit CLI values.
parser.add_command(
echo,
pipe_targets={"bindings": "message", "priority": "pipe"},
)
Partial chunks¶
By default, Interfacy reports an error if fewer chunks are provided than required targets. With allow_partial=True, missing chunks become None and are ignored for optional parameters.
parser.add_command(
pair,
pipe_targets={"bindings": ("left", "right"), "allow_partial": True},
)