Module: WifiWand::Commands::ShellInterface

Included in:
WifiWand::CommandLineInterface
Defined in:
lib/wifi_wand/commands/shell_interface.rb

Constant Summary collapse

STARTUP_MESSAGE =
[
  "For help, type 'h[Enter]' or 'help[Enter]'.",
  "To exit the shell, type 'q', 'x', 'exit', or 'quit', or press Ctrl-D.",
  '',
  'When in interactive shell mode:',
  '  * remember to quote string literals.',
  '  * for pry commands, use prefix `%`, e.g. `%ls`.',
  '  * Type `qr` to display a Wi-Fi QR code in the shell.',
].join("\n")

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *method_args) ⇒ Object

For use by the shell when the user types the DSL commands



48
49
50
51
52
53
54
55
# File 'lib/wifi_wand/commands/shell_interface.rb', line 48

def method_missing(method_name, *method_args)
  attempt_command_action(method_name.to_s, *method_args) do
    raise NoMethodError, <<~MESSAGE
      "#{method_name}" is not a valid command or option.
      If you intended it as an argument to a command, it may be invalid or need quotes.
      MESSAGE
  end
end

Instance Method Details

#quitObject



61
62
63
64
65
66
67
68
69
# File 'lib/wifi_wand/commands/shell_interface.rb', line 61

def quit
  if interactive_mode
    throw(:wifiwand_shell_exit, 0)
  else
    io = @err_stream || $stderr
    io.puts 'This command can only be run in shell mode.'
    1
  end
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/wifi_wand/commands/shell_interface.rb', line 57

def respond_to_missing?(method_name, include_private = false)
  !!find_command_action(method_name.to_s) || super
end

#run_shellObject

Runs a pry session in the context of this object. Commands and options specified on the command line can also be specified in the shell.



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
# File 'lib/wifi_wand/commands/shell_interface.rb', line 20

def run_shell
  out_stream.puts STARTUP_MESSAGE
  out_stream.puts
  require 'pry'
  require 'amazing_print'

  # Enable the line below if you have any problems with pry configuration being loaded
  # that is messing up this runtime use of pry:
  # Pry.config.should_load_rc = false

  # Strangely, this is the only thing I have found that successfully suppresses the
  # code context output, which is not useful here. Anyway, this will differentiate
  # a pry command from a DSL command, which _is_ useful here.
  Pry.config.command_prefix = '%'
  Pry.config.print = ->(output, value, _pry) do
    output.puts(value.ai) unless value.equal?(WifiWand::Commands::SILENT_RESULT)
  end
  Pry.config.exception_handler = proc do |output, exception, _pry_|
    output.puts exception.message
  end

  catch(:wifiwand_shell_exit) do
    binding.pry # rubocop:disable Lint/Debugger
    0
  end
end