README.md in tty-command-0.2.0 vs README.md in tty-command-0.3.0

- old
+ new

@@ -53,18 +53,20 @@ * [3. Advanced Interface](#3-advanced-interface) * [3.1. Environment variables](#31-environment-variables) * [3.2. Options](#32-options) * [3.2.1. Current directory](#321-current-directory) * [3.2.2. Redirection](#322-redirection) - * [3.2.3. Timeout](#323-timeout) - * [3.2.4. User](#324-user) - * [3.2.5. Group](#325-group) - * [3.2.6. Umask](#326-umask) + * [3.2.3. Handling input](#323-handling-input) + * [3.2.4. Timeout](#324-timeout) + * [3.2.5. User](#324-user) + * [3.2.6. Group](#325-group) + * [3.2.7. Umask](#326-umask) * [3.3. Result](#33-result) * [3.3.1. success?](#331-success) * [3.3.2. failure?](#332-failure) * [3.3.3. exited?](#333-exited) + * [3.3.4. each](#334-each) * [3.4. Custom printer](#34-custom-printer) * [4. Example](#4-example) ## 1. Usage @@ -237,15 +239,21 @@ cmd.run(:echo, 'hello', chdir: '/var/tmp') ``` #### 3.2.2 Redirection -The streams can be redirected using hash keys `:in`, `:out`, `:err`, a fixnum, an IO and array. The keys specify a given file descriptor for the child process. +There are few ways you can redirect commands output. -You can specify a filename for redirection as a hash value: +You can directly use shell redirection facility like so: ```ruby +cmd.run("ls 1&>2") +``` + +You can provide the streams as additional hash options where the key is one of `:in`, `:out`, `:err`, a fixnum(a file descriptor for the child process), an IO or array. The pair value can be a filename for redirection. + +```ruby cmd.run(:ls, :in => "/dev/null") # read mode cmd.run(:ls, :out => "/dev/null") # write mode cmd.run(:ls, :err => "log") # write mode cmd.run(:ls, [:out, :err] => "/dev/null") # write mode cmd.run(:ls, 3 => "/dev/null") # read mode @@ -262,37 +270,52 @@ ```ruby cmd.run(:ls, '-la', :stderr => :stdout) cmd.run(:ls, '-la', 2 => 1) ``` -#### 3.2.3 Timeout +#### 3.2.3 Handling Input +You can pass input via the :in option, by passing a StringIO Object. This object might have more than one line, if the executed command reads more than once from STDIN. + +Assume you have run a program, that first asks for your email address and then for a password: + +```ruby +in_stream = StringIO.new +in_stream.puts "username@example.com" +in_stream.puts "password" +in_stream.rewind + +TTY::Command.new.run("my_cli_program", "login", in: in_stream).out +``` + +#### 3.2.4 Timeout + You can timeout command execuation by providing the `:timeout` option in seconds: ```ruby cmd.run("while test 1; sleep 1; done", timeout: 5) ``` Please run `examples/timeout.rb` to see timeout in action. -#### 3.2.4 User +#### 3.2.5 User To run command as a given user do: ```ruby cmd.run(:echo, 'hello', user: 'piotr') ``` -#### 3.2.5 Group +#### 3.2.6 Group To run command as part of group do: ```ruby cmd.run(:echo, 'hello', group: 'devs') ``` -#### 3.2.6 Umask +#### 3.2.7 Umask To run command with umask do: ```ruby cmd.run(:echo, 'hello', umask: '007') @@ -341,10 +364,41 @@ result = cmd.run(:echo, 'Hello') result.exited? # => true result.complete? # => true ``` +#### 3.3.4 each + +The result itself is an enumerable and allows you to iterate over the stdout output: + +```ruby +result = cmd.run(:ls, '-1') +result.each { |line| puts line } +# => +# CHANGELOG.md +# CODE_OF_CONDUCT.md +# Gemfile +# Gemfile.lock +# ... +# lib +# pkg +# spec +# tasks +``` + +By default the linefeed character `\n` is used as a delimiter but this can be changed either globally by calling `record_separator`: + +```ruby +TTY::Command.record_separator = "\n\r" +``` + +or configured per `each` call by passing delimiter as an argument: + +```ruby +cmd.run(:ls, '-1').each("\t") { ... } +``` + ### 3.4 Custom printer If the built-in printers do not meet your requirements you can create your own. At the very minimum you need to specify the `write` method that will be called during the lifecycle of command execution: ```ruby @@ -357,10 +411,11 @@ printer = CustomPrinter cmd = TTY::Command.new(printer: printer) ``` + ## 4. Example Here's a slightly more elaborate example to illustrate how tty-command can improve on plain old shell scripts. This example installs a new version of Ruby on an Ubuntu machine. ```ruby @@ -396,6 +451,6 @@ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT). ## Copyright -Copyright (c) 2016 Piotr Murach. See LICENSE for further details. +Copyright (c) 2016-2017 Piotr Murach. See LICENSE for further details.