Class: WinToaster::CLI

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

Overview

Command-line front-end for win_toaster.

win_toaster TITLE MESSAGE [--detail DETAIL] [--app-id APP_ID]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.run(argv) ⇒ Object

Runs the CLI and returns the process exit code.



12
13
14
# File 'lib/win_toaster/cli.rb', line 12

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

Instance Method Details

#run(argv) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/win_toaster/cli.rb', line 16

def run(argv)
  options = { detail: nil, image: nil, hero: nil, app_id: Notifier::DEFAULT_APP_ID }

  parser = OptionParser.new do |o|
    o.banner = "Usage: win_toaster TITLE MESSAGE [options]"
    o.on("-d", "--detail DETAIL", "Third line shown below the message") { |v| options[:detail] = v }
    o.on("-i", "--image PATH", "Icon image (appLogoOverride)") { |v| options[:image] = v }
    o.on("--hero PATH", "Hero (large banner) image") { |v| options[:hero] = v }
    o.on("-a", "--app-id APP_ID", "AppUserModelId the toast is shown under") { |v| options[:app_id] = v }
    o.on("-v", "--version", "Show version and exit") do
      puts WinToaster::VERSION
      return 0
    end
    o.on("-h", "--help", "Show this help and exit") do
      puts o
      return 0
    end
  end

  title, message = parser.parse(argv)

  if title.nil? || message.nil?
    warn parser.banner
    return 1
  end

  WinToaster.notify(
    title: title, message: message, detail: options[:detail],
    image: options[:image], hero: options[:hero], app_id: options[:app_id]
  )
  0
rescue OptionParser::ParseError, WinToaster::Error => e
  warn "win_toaster: #{e.message}"
  1
end