Class: Camping::Server::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/camping/server.rb

Instance Method Summary collapse

Instance Method Details

#parse!(args) ⇒ Object



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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/camping/server.rb', line 32

def parse!(args)
  args = args.dup
  options = {}
  opt_parser = OptionParser.new("", 24, '  ') do |opts|
    opts.banner = "Usage: camping Or: camping my-camping-app.rb"

    # opts.define_head "#{File.basename($0)}, the microframework ON-button for ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"

    opts.separator ""
    opts.separator "Specific options:"

    opts.on("-h", "--host HOSTNAME",
    "Host for web server to bind to (default is all IPs)") { |v| options[:Host] = v }

    opts.on("-p", "--port NUM",
    "Port for web server (defaults to 3301)") { |v| options[:Port] = v }

    opts.on("-c", "--console",
    "Run in console mode with IRB") { options[:server] = "console" }

    opts.on("-e", "--env ENVIRONMENT",
    "Sets the environment. (defaults: development)") { |v| options[:environment] = ENV['environment'] = v }

    server_list = ["thin", "webrick", "console", "puma", "tipi", "falcon"]
    opts.on("-s", "--server NAME",
    "Server to force (#{server_list.join(', ')})") { |v| options[:server] = v }

    opts.separator ""
    opts.separator "Common options:"

    # No argument, shows at tail.  This will print an options summary.
    # Try it and see!
    opts.on("-?", "--help", "Show this message") do
      puts opts
      exit
    end

    # Another typical switch to print the version.
    opts.on("-v", "--version", "Show version") { options[:version] = true }

    # Show Routes
    opts.on("-r", "--routes", "Show Routes") { options[:routes] = true }

  end

  opt_parser.parse!(args)

  if args.empty?
    args << "camp.rb"
  end

  options[:script] = args.shift
  options
end