0.02
No release in over 3 years
Low commit activity in last 3 years
An easier way execute command line applications and get all of the output.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 2.0
~> 13.0
~> 3.5.0
~> 3.9
~> 0.81.0
~> 0.9.20
 Project Readme

CommandLine

Version Test

CommandLine provides an easier way to run command-line applications. It captures all outputs, can handle applications that require stdin, and can pass environment variables. It's also helpful for testing commmand-line applications.

This project is tested against and works on Linux, OS X, and Windows.

Installation

This project uses Semantic Versioning.

Add this line to your application's Gemfile:

gem 'command_line', '~> 2.0'

If you want command_line available globally you can add this line to your application's Gemfile:

gem 'command_line', '~> 2.0', require: 'command_line/global'

Or manually install it yourself with:

$ gem install command_line

In a script you can make command_line available globally with:

require 'command_line/global'

Usage

All examples below assume that command_line has been made available globally. If not, simply call CommandLine.command_line instead of command_line.

With command_line you to easily run commands and check their stdout, stderr, and exit status.

>> result = command_line('echo', 'hello')
=> #<CommandLine::Result ...>
>> result.stdout
=> "hello\n"
>> result.stderr
=> ""
>> result.exited?
=> true
>> result.exitstatus
=> 0
>> result.success?
=> true
>> result.failure?
=> false

If your application requires input use a block to send input.

command_line('command_expecting_input') do |stdin|
  stdin.puts "first input"
  stdin.puts "second input"
end

Environment variables can be passed after the command and arguments are passed.

command_line('some_webserver', env: { PORT: '80' })

If you're concerned about the command running too long you can set a :timeout. Exceeding the timeout will cause a CommandLine::TimeoutError to be raised.

>> command_line('sleep', 5, timeout: 2)
CommandLine::TimeoutError (execution expired)

You can use command_line! if you want to raise an error on an exit failure. The contents of stderr will be the error message.

>> command_line!('grep')
CommandLine::ExitFailureError (usage: grep [-abc....

RSpec

To have direct access to CommandLine.command_line you can include it in spec/spec_helper.rb.

require 'command_line'

RSpec.configure do |config|
  config.include CommandLine
end

This will make command_line available in your test suite.