Class: WiFiddler::CLI::ArgumentParser

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

Defined Under Namespace

Classes: InvalidOption

Constant Summary collapse

DEFAULT_OPTIONS =
{
  device_id: "en0",
  interval: 10,
  attempts: 100,
  timeout:  10 * 60,
  verbose:  false
}

Instance Method Summary collapse

Instance Method Details

#parse(arguments) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/wifiddler.rb', line 104

def parse(arguments)
  options = DEFAULT_OPTIONS.dup
  optparse = OptionParser.new do |opts|
    opts.banner = <<-BANNER.gsub(/^\s{4}/, '')
      Cycle airport off & on until there is a connection
      Usage: wifiddler [options]
      Example: yourkarma --verbose
    BANNER

    opts.on('-i', '--interval=INTERVAL', "Seconds to wait after first cycle (default: #{DEFAULT_OPTIONS[:interval]})", Float) do |interval|
      options[:interval] = interval
    end
    opts.on('-t', '--timeout=TIMEOUT', "Seconds to wait before exiting (default: #{DEFAULT_OPTIONS[:timeout]})", Float) do |timeout|
      options[:timeout] = timeout
    end
    opts.on('-a', '--attempts=attempts', "Number of cycle attempts (default: #{DEFAULT_OPTIONS[:attempts]})", Integer) do |attempts|
      options[:attempts] = attempts
    end
    opts.on('-d', '--device=DEVICE_ID', "Airport device ID (default: #{DEFAULT_OPTIONS[:device_id]})", String) do |device_id|
      options[:device_id] = device_id
    end
    opts.on('-v', '--[no-]verbose', "Run verbosely (default: #{DEFAULT_OPTIONS[:verbose]})") do |v|
      options[:verbose] = true
    end
    opts.on('-h', '--help', 'Display this screen') do
      raise InvalidOption, opts
    end
  end

  optparse.parse!(arguments)

  options
rescue OptionParser::InvalidOption, OptionParser::MissingArgument
  raise InvalidOption, optparse
end