Following is a working example from ruby:
#!/usr/bin/env ruby
# -*- coding: utf-8; mode: ruby; -*-
if $stdin.stat.pipe?
puts $stdin.read
else
puts "not pipe"
end
$: ./test # => not pipe
$: echo hello |./test # => hello
I can use BASH shell like this:
p `test -p /proc/$$/fd/0; echo $?`.chomp # 0 if read from pipe, otherwize 1.
But i don’t know if this hack works on MacOS.
What i want it write following code Crystal way.
is_pipe = `test -p /proc/$$/fd/0; echo $?`.chomp == "0"
if is_pipe
puts STDIN.gets
else
ARGV << "--help" if ARGV.empty?
puts ARGV
end
Thank you.