Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiline argument via $stdin and pipe operator using custom commands #1521

Open
atschwarz opened this issue Feb 23, 2016 · 1 comment
Open

Comments

@atschwarz
Copy link

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 atschwarz changed the title Multiline argument via $stdin and pipe operator Multiline argument via $stdin and pipe operator using custom commands Feb 23, 2016
@magikid
Copy link

magikid commented Dec 3, 2018

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants