shikhalev/drctrl

View on GitHub
bin/drctrl

Summary

Maintainability
Test Coverage
#!/usr/bin/ruby
# encoding: utf-8

require 'optparse'
require 'current_spec'
require_relative '../lib/drctrl/client'

verbose = false

options = {
  uri: nil,
  path: nil,
  name: nil,
  pid: nil
}

opts = OptionParser.new do |o|

  o.banner = 'drctrl [<options>] <command> [<args>]'

  o.on '-h', '-?', '--help', 'Show this help and exit.' do
    puts opts.help
    exit
  end

  o.on '-V', '--version', 'Show version info and exit' do
    puts(Gem.current_spec_version || DRCtrl::VERSION.join('.'))
    exit
  end

  o.on '-v', '--[no-]verbose', 'Verbose mode.' do |value|
    verbose = value
  end

  o.separator ''

  o.on '-U', '--uri', '=URI', String do |value|
    options[:uri] = value
  end

  o.on '-P', '--path', '=PATH', String do |value|
    options[:path] = value
  end

  o.on '-N', '--name', '=NAME', String do |value|
    options[:name] = value
  end

  o.on '-I', '--pid', '=PID', Integer do |value|
    options[:pid] = value
  end

end

command, *args = opts.parse ARGV

obj = DRCtrl.connect options

if Array === obj
  obj.each do |o|
    if verbose
      puts "Send #{command.intern.inspect} to #{o.__drburi.inspect}..."
    end
    o.send command.intern, *args
  end
else
  if verbose
    puts "Send #{command.intern.inspect} to #{obj.__drburi.inspect}..."
  end
  obj.send command.intern, *args
end