Class: WifiWand::Main
- Inherits:
-
Object
- Object
- WifiWand::Main
- Defined in:
- lib/wifi-wand/main.rb
Instance Method Summary collapse
- #call ⇒ Object
-
#parse_command_line ⇒ Object
Parses the command line with Ruby’s internal ‘optparse’.
Instance Method Details
#call ⇒ Object
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/wifi-wand/main.rb', line 59 def call = parse_command_line begin WifiWand::CommandLineInterface.new().call rescue => e # require 'pry'; binding.pry puts "Error: #{e.backtrace.join("\n")}\n\n#{e.}" end end |
#parse_command_line ⇒ Object
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 |
# File 'lib/wifi-wand/main.rb', line 15 def parse_command_line = OpenStruct.new OptionParser.new do |parser| parser.on("-v", "--[no-]verbose", "Run verbosely") do |v| .verbose = v end parser.on("-s", "--shell", "Start interactive shell") do |v| .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) raise %Q{Output format "#{choice}" not in list of available formats} + " (#{formatters.keys.inspect})." end .post_processor = formatters[choice] end parser.on("-p", "--wifi-port PORT", "WiFi port name") do |v| .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! end |