Class: Camping::Server::Options

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

Constant Summary collapse

DB =
nil
RC =
nil
HOME =
File.expand_path(home) + '/'

Instance Method Summary collapse

Instance Method Details

#parse!(args) ⇒ Object



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
86
87
88
89
90
91
92
93
94
95
# File 'lib/camping/server.rb', line 42

def parse!(args)
  args = args.dup
  options = {}
  
  opt_parser = OptionParser.new("", 24, '  ') do |opts|
    opts.banner = "Usage: 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 }
    
    db = DB.sub(HOME, '~/') if DB
    opts.on("-d", "--database FILE",
    "SQLite3 database path (defaults to #{db ? db : '<none>'})") { |db_path| options[:database] = db_path }
    
    opts.on("-C", "--console",
    "Run in console mode with IRB") { options[:server] = "console" }
    
    server_list = ["thin", "webrick", "console"]
    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_tail("-?", "--help", "Show this message") do
      puts opts
      exit
    end

    # Another typical switch to print the version.
    opts.on_tail("-v", "--version", "Show version") do
      puts Gem.loaded_specs['camping'].version
      exit
    end
  end
  
  opt_parser.parse!(args)
  
  if args.empty?
    puts opt_parser
    exit
  end
  
  options[:script] = args.shift
  options
end