简体   繁体   中英

Writing a D script that can be piped input from command line

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?

Arguments passed to the main function correspond to arguments you specify after the program name on your command line. Eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM