Class: SleepRoom::CLI

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

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ CLI

Returns a new instance of CLI.

Parameters:

  • argv (Array)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/sleeproom/cli.rb', line 10

def initialize(argv)
  SleepRoom.reload_config
  @options = {}
  build
  unless argv.empty?
    @parser.parse!(argv)
    action = argv.shift
    if action == "status"
      SleepRoom::Record::Tasks.status
    elsif action == "start"
      SleepRoom::Record::Tasks.start
    elsif action == "exit"
      SleepRoom::Record::Tasks.stop
    end
    exit(0)
  else
    puts @parser
    exit(0)
  end
end

Instance Method Details

#buildvoid

This method returns an undefined value.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/sleeproom/cli.rb', line 37

def build
  @parser = OptionParser.new do |opt|
    opt.version = "SleepRoom / #{SleepRoom::VERSION}"
    opt.banner = "#{opt.version}"
    opt.banner += "\nUsage: sleeproom [Options]\n\n"

    opt.banner += "Action:\n"
    opt.banner += "status".rjust(10)
    opt.banner += "显示任务状态".rjust(33)
    opt.banner += "\n"
    opt.banner += "exit".rjust(8)
    opt.banner += "关闭任务队列".rjust(35)
    opt.banner += "\n\nCommands:\n"

    opt.on("-a ROOM, NAME", "--add ROOM, GROUP", Array, "添加到监视列表") do |room|
      SleepRoom::Record::Tasks.add(room[0].to_s, room[1].to_s)
    end

    opt.on("-r", "--remove [ROOM]", "从监视列表移除") do |room|
      SleepRoom::Record::Tasks.remove(room)
    end

    opt.on("-d", "--download [ROOM]", "录制指定房间") do |room|
      raise Error.new("房间名不能为空") if room.nil?
      if room.match?("https://www.showroom-live.com/")
        room = room.match(/https:\/\/www.showroom-live.com\/(.*)/)[1]
      end
      write_status = SleepRoom::Record::WriteStatus.new
      record = SleepRoom::Record::Showroom.new(room: room, group: "download", queue: write_status)
      record.record
    end

    opt.on("-v", "--verbose", "Print log") do
      @options[:verbose] = true
    end
    
    opt.on_tail("--version", "Print version") do
      STDOUT.puts(opt.version)
    end

    opt.on_tail("-h", "--help", "Print help") do
      STDOUT.puts(opt)
    end
  end
end

#runvoid

This method returns an undefined value.



32
33
34
# File 'lib/sleeproom/cli.rb', line 32

def run
  SleepRoom::Record::Tasks.start
end