Class: WifiWand::Main

Inherits:
Object
  • Object
show all
Defined in:
lib/wifi-wand/main.rb

Instance Method Summary collapse

Instance Method Details

#callObject



61
62
63
64
65
66
67
68
69
70
# File 'lib/wifi-wand/main.rb', line 61

def call
  options = parse_command_line

  begin
    WifiWand::CommandLineInterface.new(options).call
  rescue => e
    # require 'pry'; binding.pry
    puts "Error: #{e.backtrace.join("\n")}\n\n#{e.message}"
  end
end

#parse_command_lineObject

Parses the command line with Ruby’s internal ‘optparse’. optparse removes what it processes from ARGV, which simplifies our command parsing.



15
16
17
18
19
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
# File 'lib/wifi-wand/main.rb', line 15

def parse_command_line
  options = OpenStruct.new

  OptionParser.new do |parser|
    parser.on("-v", "--[no-]verbose", "Run verbosely") do |v|
      options.verbose = v
    end

    parser.on("-s", "--shell", "Start interactive shell") do |v|
      options.interactive_mode = true
    end

    parser.on("-o", "--output_format FORMAT", "Format output data") do |v|

      formatters = {
          'i' => ->(object) { object.inspect },
          'j' => ->(object) { object.to_json },
          'k' => ->(object) { JSON.pretty_generate(object) },
          'p' => ->(object) { sio = StringIO.new; sio.puts(object); sio.string },
          'y' => ->(object) { object.to_yaml }
      }

      choice = v[0].downcase

      unless formatters.keys.include?(choice)
        message = %Q{Output format "#{choice}" not in list of available formats} <<
            " (#{formatters.keys})."
        puts; puts message; puts
        raise Error.new(message)
      end

      options.post_processor = formatters[choice]
    end

    parser.on("-p", "--wifi-port PORT", "WiFi port name") do |v|
      options.wifi_port = v
    end

    parser.on("-h", "--help", "Show help") do |_help_requested|
      ARGV << 'h' # pass on the request to the command processor
    end
  end.parse!
  options
end