Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

126 lines (92 sloc) 3.384 kb


A small library for doing (command) lines.


Question? Idea? Problem? Bug? Something else? Comment? Concern? Like use question marks?

GitHub Issues For All!


The basic, normal stuff.

line ="command", "some 'crazy' options")
line.command      # => "command some 'crazy' options"
output = # => Get you some output!

Allowing arguments to be dynamic.

line ="convert", ":in -scale :resolution :out",
                                :in => "omg.jpg",
                                :resolution => "32x32",
                                :out => omg_thumb.jpg")
line.command # => "convert 'omg.jpg' -scale '32x32' 'omg_thumb.jpg'"

It prevents attempts at being bad.

line ="cat", ":file", :file => "haha`rm -rf /`.txt")
line.command # => "cat 'haha`rm -rf /`.txt'"

line ="cat", ":file", :file => "ohyeah?'`rm -rf /`.ha!")
line.command # => "cat 'ohyeah?'\\''`rm -rf /`.ha!'"

You can ignore the result.

line ="noisy", "--extra-verbose", :swallow_stderr => true)
line.command # => "noisy --extra-verbose 2>/dev/null"

# ... and on Windows...
line.command # => "noisy --extra-verbose 2>NUL"

If your command errors, you get an exception.

line ="git", "commit")
rescue Cocaine::ExitStatusError => e
  e.message # => "Command 'git commit' returned 1. Expected 0"

You don't have the command? You get an exception.

line ="lolwut")
rescue Cocaine::CommandNotFoundError => e
  e # => the command isn't in the $PATH for this process.

But don't fear, you can specify where to look for the command.

Cocaine::CommandLine.path = "/opt/bin"
line ="lolwut")
line.command # => "lolwut", but it looks in /opt/bin for it.

You can even give it a bunch of places to look.

`echo 'echo Hello' > /usr/local/bin/lolwut`
Cocaine::CommandLine.path = ["/opt/bin", "/usr/local/bin"]
line ="lolwut") # => prints 'Hello', because it searches the path

Or, just, you know, put it in the command.

line ="/opt/bin/lolwut")
line.command # => "/opt/bin/lolwut"

If your command might return something non-zero, and you expect that, it's cool.

line ="/usr/bin/false", "", :expected_outcodes => [0, 1])
rescue Cocaine::ExitStatusError => e
  # => You never get here!

You can see what's getting run. The 'Command' part it logs is in green for visibility!

line ="echo", ":var", :var => "LOL!", :logger => # => Logs this with #info -> Command :: echo 'LOL!'

But you don't have to, as you saw above where it doesn't use this. But you CAN log every command!

Cocaine::CommandLine.logger ="date").run # => Logs this -> Command :: date


Copyright 2011 Jon Yurek and thoughtbot, inc. This is free software, and may be redistributed under the terms specified in the LICENSE file.

Jump to Line
Something went wrong with that request. Please try again.