Class: Launchy::Cli

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

Overview

Internal: Command line interface for Launchy

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCli

Returns a new instance of Cli.



11
12
13
# File 'lib/launchy/cli.rb', line 11

def initialize
  @options = {}
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#error_output(error) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/launchy/cli.rb', line 70

def error_output(error)
  $stderr.puts "ERROR: #{error}"
  Launchy.log "ERROR: #{error}"
  error.backtrace.each do |bt|
    Launchy.log bt
  end
  $stderr.puts "Try `#{parser.program_name} --help' for more information."
  false
end

#good_run(argv, env) ⇒ Object



63
64
65
66
67
68
# File 'lib/launchy/cli.rb', line 63

def good_run(argv, env)
  return false unless parse(argv, env)

  Launchy.open(argv.shift, options) { |e| error_output(e) }
  true
end

#parse(argv, _env) ⇒ Object



56
57
58
59
60
61
# File 'lib/launchy/cli.rb', line 56

def parse(argv, _env)
  parser.parse!(argv)
  true
rescue ::OptionParser::ParseError => e
  error_output(e)
end

#parserObject



15
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
51
52
53
54
# File 'lib/launchy/cli.rb', line 15

def parser
  @parser ||= OptionParser.new do |op|
    op.banner = "Usage: launchy [options] thing-to-launch"

    op.separator ""
    op.separator "Launch Options:"

    op.on("-a", "--application APPLICATION",
          "Explicitly specify the application class to use in the launch") do |app|
      @options[:application] = app
    end

    op.on("-d", "--debug",
          "Force debug. Output lots of information.") do |_d|
      @options[:debug] = true
    end

    op.on("-n", "--dry-run", "Don't launchy, print the command to be executed on stdout") do |_x|
      @options[:dry_run] = true
    end

    op.on("-o", "--host-os HOST_OS",
          "Force launchy to behave as if it was on a particular host os.") do |os|
      @options[:host_os] = os
    end

    op.separator ""
    op.separator "Standard Options:"

    op.on("-h", "--help", "Print this message.") do |_h|
      $stdout.puts op.to_s
      exit 0
    end

    op.on("-v", "--version", "Output the version of Launchy") do |_v|
      $stdout.puts "Launchy version #{Launchy::VERSION}"
      exit 0
    end
  end
end

#run(argv = ARGV, env = ENV) ⇒ Object



80
81
82
# File 'lib/launchy/cli.rb', line 80

def run(argv = ARGV, env = ENV)
  exit 1 unless good_run(argv, env)
end