class Bane::ArgumentsParser

Public Class Methods

new(makeable_names) click to toggle source
# File lib/bane/arguments_parser.rb, line 7
def initialize(makeable_names)
  @makeable_names = makeable_names
  @options = {host: default_host}
  @option_parser = init_option_parser
end

Public Instance Methods

parse(args) click to toggle source
# File lib/bane/arguments_parser.rb, line 13
def parse(args)
  @option_parser.parse!(args)

  raise ConfigurationError, "Missing arguments" if args.empty?

  port = parse_port(args[0])
  behaviors = args.drop(1)
  ParsedArguments.new(port, @options[:host], behaviors)
rescue OptionParser::InvalidOption => io
  raise ConfigurationError, io.message
end
usage() click to toggle source
# File lib/bane/arguments_parser.rb, line 25
def usage
  @option_parser.help
end

Private Instance Methods

all_interfaces() click to toggle source
# File lib/bane/arguments_parser.rb, line 54
def all_interfaces
  Behaviors::Servers::ALL_INTERFACES
end
default_host() click to toggle source
# File lib/bane/arguments_parser.rb, line 58
def default_host
  Behaviors::Servers::LOCALHOST
end
init_option_parser() click to toggle source
# File lib/bane/arguments_parser.rb, line 31
def init_option_parser
  OptionParser.new do |opts|
    opts.banner = 'Usage: bane [options] port [behaviors]'
    opts.separator ''
    opts.on('-l', '--listen-on-localhost',
            "Listen on localhost, (#{default_host}). [default]") do
      @options[:host] = default_host
    end
    opts.on('-a', '--listen-on-all-hosts', "Listen on all interfaces, (#{all_interfaces})") do
      @options[:host] = all_interfaces
    end
    opts.separator ''
    opts.separator 'All behaviors:'
    opts.separator @makeable_names.sort.map { |title| " - #{title}" }.join("\n")
  end
end
parse_port(port) click to toggle source
# File lib/bane/arguments_parser.rb, line 48
def parse_port(port)
  Integer(port)
rescue ArgumentError
  raise ConfigurationError, "Invalid port number: #{port}"
end