Writing a D script that can be piped input from command line
command-line, d, scripting, shell, windows
Solution
Arguments passed to the `main` function correspond to arguments you specify after the program name on your command line. E.g. if you run `foo arg1 arg2`, then the `main` function will have the arguments array set to `["foo", "arg1", "arg2"]`.
Piped input is different. Here, you are telling the shell to start your program with the standard input stream attached to the output of another program, instead of the TTY (keyboard input) as usual. To process input passed in this manner, simply use `readln` and co. as if you were reading keyboard input. You can refer to the `stdin` `File` variable to access properties such as `byLine`, which allow easily iterating over every line in the input stream.
Problem
I'm currently using D as a scripting language for various command-line scripts to automate boring tasks. I'd like to be able to write scripts in D that take piped input, but as-current, I'm not having much success, because what I'm piping it doesn't seem to show up in the argument list at all! For example, suppose I have a script foo.exe, that is meant to take a single input. The idea is that I can write, for example: ``` echo bar | foo ``` and have `foo` run with input bar after bar prints to the screen. However, every time I've tried this, I never seem to have any arguments in the args array for `foo` (just foo itself). What am I not doing right here?