Class: RubyLiveReload::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_live_reload/options.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#directoryObject

Returns the value of attribute directory.



18
19
20
# File 'lib/ruby_live_reload/options.rb', line 18

def directory
  @directory
end

#hostObject

Returns the value of attribute host.



18
19
20
# File 'lib/ruby_live_reload/options.rb', line 18

def host
  @host
end

#messageObject

Returns the value of attribute message.



18
19
20
# File 'lib/ruby_live_reload/options.rb', line 18

def message
  @message
end

#portObject

Returns the value of attribute port.



18
19
20
# File 'lib/ruby_live_reload/options.rb', line 18

def port
  @port
end

#proxyObject

Returns the value of attribute proxy.



18
19
20
# File 'lib/ruby_live_reload/options.rb', line 18

def proxy
  @proxy
end

Class Method Details

.parse(args) ⇒ Object



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
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ruby_live_reload/options.rb', line 20

def self.parse(args)

  unless [String, Array].include? args.class 
    raise <<~ERROR.strip
      Provide an array or a space-separated string of arguments: ["-p", 9090, "--host", "198.168.0.42"] or "-p 9090 --host 192.168.0.42"
    ERROR
  end

  args = args.split " " if args.is_a? String

  instance = Options.new

  OptionParser.new do |options|
    options.banner = "Usage: rlr [instance]"
    options.release = VERSION

    options.on("-b HOST", "--bind HOST", "Hostname") do |host|
      instance.send :host=, host
    end

    options.on("-p PORT", "--port PORT", "Port") do |port|
      instance.send :port=, port
    end

    options.on("--proxy URL", "Url of the proxied app") do |proxy|
      instance.send :proxy=,  proxy
    end

    options.on("-d PATH", "--directory PATH", "Path") do |path|
      directory = if path.start_with?("/")
        path
      else
        File.join(Dir.pwd, path)
      end

      # TODO Validate path is a directory

      instance.send :directory=, directory
    end

    options.on("-v", "--version", "Version") do
      instance.send :message=, VERSION
    end

  end.parse(args)

  return instance
end