Class: Tucue::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/tucue/cli.rb

Overview

Command-line entry point: parses arguments and launches the TUI.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.parse_time(value) ⇒ Object

Parse “SS”, “MM:SS”, or “HH:MM:SS” (seconds may be fractional) into a number of seconds. Returns nil for nil input; raises ArgumentError on a malformed value.



39
40
41
42
43
44
45
46
47
48
# File 'lib/tucue/cli.rb', line 39

def self.parse_time(value)
  return nil if value.nil?

  parts = value.to_s.split(":", -1)
  unless (1..3).cover?(parts.size) && parts.all? { |p| p.match?(/\A\d+(\.\d+)?\z/) }
    raise ArgumentError, "invalid time: #{value.inspect} (use SS, MM:SS, or HH:MM:SS)"
  end

  parts.map(&:to_f).reduce(0.0) { |acc, part| acc * 60 + part }
end

.start(argv) ⇒ Object

Run with the given argv. Returns a process exit status (0 = success).



9
10
11
# File 'lib/tucue/cli.rb', line 9

def self.start(argv)
  new.start(argv)
end

Instance Method Details

#start(argv) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/tucue/cli.rb', line 13

def start(argv)
  options = parse(argv)
  file = argv.shift

  unless file
    warn "usage: tucue [options] FILE"
    return 1
  end

  unless File.exist?(file)
    warn "tucue: file not found: #{file}"
    return 1
  end

  player = Player.new(file, start_at: options[:start_at])
  marker = Marker.new
  UI.new(player, marker).run
  0
rescue OptionParser::ParseError, ArgumentError => e
  warn "tucue: #{e.message}"
  1
end