Skip to content
An easier way execute command line applications and get all of the output.
Ruby Shell
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
bin initial commit Oct 13, 2019
lib
spec allow non-string arguments Oct 18, 2019
.editorconfig initial commit Oct 13, 2019
.gitignore
.rspec initial commit Oct 13, 2019
.rubocop.yml
.travis.yml make sure bundler is up to date when testing Oct 14, 2019
CHANGELOG.md add the release date for 1.1.0 Oct 23, 2019
Gemfile
LICENSE.txt
README.md update badge labels Oct 19, 2019
Rakefile
appveyor.yml only check the tests not linting Oct 14, 2019
command_line.gemspec

README.md

CommandLine

Version Linux and OS X Build Windows Build

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', '~> 1.1'

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

gem 'command_line', '~> 1.1', 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.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/DragonRuby/command_line.

License

The gem is available as open source under the terms of the MIT License.

You can’t perform that action at this time.