module MultiTimeout::CLI

Public Class Methods

consume_command(argv) click to toggle source
# File lib/multi_timeout.rb, line 82
def consume_command(argv)
  argv = argv.dup
  options = []
  while argv.first =~ /^-/
    options << argv.shift
  end
  return argv, options
end
consume_signals(argv) click to toggle source
# File lib/multi_timeout.rb, line 91
def consume_signals(argv)
  timeouts = {}
  signal = nil
  argv = argv.map do |item|
    if !signal && item =~ VALID_SIGNAL
      signal = $1
      next
    elsif signal
      signal = signal.sub("-", "")
      signal = signal.to_i if signal =~ /^\d+$/
      timeouts[signal] = human_value_to_seconds(item)
      signal = nil
      next
    else
      item
    end
  end.compact

  return timeouts, argv
end
human_value_to_seconds(t) click to toggle source
# File lib/multi_timeout.rb, line 112
def human_value_to_seconds(t)
  unit =
    case t
    when /^\d+s$/ then 1
    when /^\d+m$/ then 60
    when /^\d+h$/ then 60 * 60
    when /^\d+$/  then 1
    else raise "Unknown format for time #{t}"
    end
  t.to_i * unit
end
parse_options(argv) click to toggle source
# File lib/multi_timeout.rb, line 58
      def parse_options(argv)
        options = {:timeouts => []}
        options[:timeouts], argv = consume_signals(argv)
        command, argv = consume_command(argv)

        OptionParser.new do |opts|
          opts.banner = <<-BANNER.gsub(/^ {10}/, "")
            Use multiple timeouts to soft and then hard kill a command

            Usage:
                multi-timeout -9 5s -2 4s sleep 20

            Options:
          BANNER
          opts.on("-SIGNAL TIME", Integer, "Kill with this SIGNAL after TIME") { raise } # this is never used, just placeholder for docs
          opts.on("-h", "--help", "Show this.") { puts opts; exit }
          opts.on("-v", "--version", "Show Version"){ puts MultiTimeout::VERSION; exit}
        end.parse!(argv)

        raise "No timeouts given" if options[:timeouts].empty?

        [command, options]
      end
run(argv) click to toggle source
# File lib/multi_timeout.rb, line 54
def run(argv)
  MultiTimeout.run(*parse_options(argv))
end