pry icon indicating copy to clipboard operation
pry copied to clipboard

Multiline argument via $stdin and pipe operator using custom commands

Open atschwarz opened this issue 9 years ago • 1 comments

Hi,

I'm trying to implement a custom command for pry, which accepts a multi-line argument via the pipe operator. The following works fine:

echo "s = \"Hello
World\"" | pry

[1] pry(main)> s = "Hello
[1] pry(main)* World"
=> "Hello\nWorld"

Pry recognize the multi-line string declaration and increment the nest level. But how can I achieve this behavior with my custom command? Equivalent to the previous example the call with my custom command looks like this one:

echo "custom_command \"foo bar ...
nextline...\" | pry

But this does not work. Only the first line "foo bar is accepted as argument. The second line is interpreted as command.

In my custom command class I tried something like this:

def process
    input = args.join(' ')
    if !$stdin.tty?
        if input =~ /^".*/ && input !~ /^".*"$/
            while true
                line = $stdin.read
                input = "#{input} #{line}"
                if line =~ /.*"$/
                    break
                end
            end
            puts "#{input}"
        end
    end
end

But this approach does not work, because there's nothing to read from $stdin. The variable line is always empty. Does someone know how I can solve this problem? I want, that the complete argument "foo bar ... nextline ..." is read by my custom command.

Thank you in advance and best regards, Andi

atschwarz avatar Feb 23 '16 11:02 atschwarz

In your example

echo "custom_command \"foo bar ...
nextline...\" | pry

Are you escaping the newline inserted by your shell like this?

echo "custom command \"foo bar \
nextline...\" | pry

Have you tried wrapping the echo string in single quotes so that you don't need to escape the double quotes?

magikid avatar Dec 03 '18 16:12 magikid