0

Consider this snippet from Ruby's open4:

  def spawn arg, *argv
    argv.unshift(arg)
    opts = ((argv.size > 1 and Hash === argv.last) ? argv.pop : {})
    argv.flatten!
    cmd = argv.join(' ')


    getopt = getopts opts

    ignore_exit_failure = getopt[ 'ignore_exit_failure', getopt['quiet', false] ]
    ignore_exec_failure = getopt[ 'ignore_exec_failure', !getopt['raise', true] ]
    exitstatus = getopt[ %w( exitstatus exit_status status ) ]
    stdin = getopt[ %w( stdin in i 0 ) << 0 ]
    stdout = getopt[ %w( stdout out o 1 ) << 1 ]
    stderr = getopt[ %w( stderr err e 2 ) << 2 ]
    pid = getopt[ 'pid' ]
    timeout = getopt[ %w( timeout spawn_timeout ) ]
    stdin_timeout = getopt[ %w( stdin_timeout ) ]
    stdout_timeout = getopt[ %w( stdout_timeout io_timeout ) ]
    stderr_timeout = getopt[ %w( stderr_timeout ) ]
    status = getopt[ %w( status ) ]
    cwd = getopt[ %w( cwd dir ) ]

Suppose now that I would like to make ignore_exit_failure == true when calling this function from code that uses the gem. How would I do that?

EDIT: I guess I had just assumed the getopts thing was a standard Ruby module thing for passing options around. Here is the definition of getopts per Alex Kliuchnikau's comment:

  def getopts opts = {}
    lambda do |*args|
      keys, default, ignored = args
      catch(:opt) do
        [keys].flatten.each do |key|
          [key, key.to_s, key.to_s.intern].each do |key|
            throw :opt, opts[key] if opts.has_key?(key)
          end
        end
        default
      end
    end
  end
  module_function :getopts

Perhaps this is just a question for someone already familiar with the open4 module.

jeffcook2150
  • 4,028
  • 4
  • 39
  • 51

1 Answers1

0

You need to pass parameters this way:

open4.spawn cmd, 'ignore_exit_failure' => true

If you don't specify it, it will set ignore_exit_failure to the value of quiet parameter.

open4.spawn cmd, 'quiet' => true

And if it is not specified as well, it will set ignore_exit_failure to false.

Aliaksei Kliuchnikau
  • 13,589
  • 4
  • 59
  • 72
  • Unfortunately this doesn't appear to work. I get the same complaint from spawn: `/usr/share/ruby-rvm/gems/ruby-1.9.3-p0/gems/open4-1.3.0/lib/open4.rb:361:in `spawn': cmd failed with status <1> signals <> (Open4::SpawnError) from /usr/share/ruby-rvm/gems/ruby-1.9.3-p0/gems/open4-1.3.0/lib/open4.rb:384:in `block in background'` – jeffcook2150 Feb 03 '12 at 18:23
  • @cookiecaper, can you please update the question with the full code to call `open4` (created instance, passed parameters) – Aliaksei Kliuchnikau Feb 03 '12 at 18:25
  • I emailed the creator of open4 and got no response. I have given up on this approach. Thanks for your willingness to help though. The code is all available online at https://github.com/ahoward/open4 if you are still curious. – jeffcook2150 Feb 04 '12 at 22:50