Class: WifiWand::CommandLineInterface

Inherits:
Object
  • Object
show all
Includes:
WifiWand::Commands::HelpSystem, WifiWand::Commands::OutputFormatter, WifiWand::Commands::Registry, WifiWand::Commands::ShellInterface
Defined in:
lib/wifi_wand/command_line_interface.rb

Constant Summary collapse

SUCCESS_EXIT_CODE =
0
FAILURE_EXIT_CODE =
1
FORMAT_DISPLAY_NAMES =
{
  'a' => 'amazing_print',
  'i' => 'inspect',
  'j' => 'json',
  'J' => 'pretty_json',
  'p' => 'puts',
  'P' => 'pp',
  'y' => 'yaml',
}.freeze

Constants included from WifiWand::Commands::ShellInterface

WifiWand::Commands::ShellInterface::STARTUP_MESSAGE

Constants included from WifiWand::Commands::HelpSystem

WifiWand::Commands::HelpSystem::HELP_BODY_WIDTH, WifiWand::Commands::HelpSystem::HELP_DESCRIPTION_INDENT, WifiWand::Commands::HelpSystem::HELP_DESCRIPTION_WIDTH, WifiWand::Commands::HelpSystem::HELP_GAP, WifiWand::Commands::HelpSystem::HELP_INDENT, WifiWand::Commands::HelpSystem::HELP_LEADER, WifiWand::Commands::HelpSystem::HELP_LEFT_COLUMN_WIDTH, WifiWand::Commands::HelpSystem::HELP_SWITCHES, WifiWand::Commands::HelpSystem::HORIZONTAL_RULE, WifiWand::Commands::HelpSystem::LOCAL_README_PATH, WifiWand::Commands::HelpSystem::REPOSITORY_URL

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from WifiWand::Commands::ShellInterface

#method_missing, #quit, #respond_to_missing?, #run_shell

Methods included from WifiWand::Commands::Registry

#attempt_command_action, #commands, #find_command, #find_command_action, #resolve_command

Methods included from WifiWand::Commands::OutputFormatter

#colorize_network_name, #colorize_status, #colorize_text, #colorize_values, #format_boolean_status, #format_internet_status, #format_object, #post_process, #post_processor, #status_line

Methods included from WifiWand::Commands::HelpSystem

#help_hint, #help_text, #print_help

Constructor Details

#initialize(options, argv: nil) ⇒ CommandLineInterface

Returns a new instance of CommandLineInterface.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/wifi_wand/command_line_interface.rb', line 31

def initialize(options, argv: nil)
  @options = options
  parsed_argv = argv || options.argv
  @argv = Array(parsed_argv).dup
  @original_out_stream = options.out_stream
  @err_stream = options.err_stream || $stderr
  @in_stream = options.in_stream || $stdin
  @command_options = options.command_options || {}

  @model_options = {
    verbose:        options.verbose,
    utc:            options.utc,
    wifi_interface: options.wifi_interface,
    out_stream:     out_stream,
    err_stream:     err_stream,
  }

  @interactive_mode = !!options.interactive_mode
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class WifiWand::Commands::ShellInterface

Instance Attribute Details

#command_optionsObject (readonly)

Returns the value of attribute command_options.



26
27
28
# File 'lib/wifi_wand/command_line_interface.rb', line 26

def command_options
  @command_options
end

#err_streamObject (readonly)

Returns the value of attribute err_stream.



26
27
28
# File 'lib/wifi_wand/command_line_interface.rb', line 26

def err_stream
  @err_stream
end

#in_streamObject (readonly)

Returns the value of attribute in_stream.



26
27
28
# File 'lib/wifi_wand/command_line_interface.rb', line 26

def in_stream
  @in_stream
end

#interactive_modeObject (readonly)

Returns the value of attribute interactive_mode.



26
27
28
# File 'lib/wifi_wand/command_line_interface.rb', line 26

def interactive_mode
  @interactive_mode
end

#optionsObject (readonly)

Returns the value of attribute options.



26
27
28
# File 'lib/wifi_wand/command_line_interface.rb', line 26

def options
  @options
end

Instance Method Details

#callObject

MAIN ENTRY POINT =====


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/wifi_wand/command_line_interface.rb', line 88

def call
  return run_shell if interactive_mode

  validation_status = validate_command_line
  return validation_status unless validation_status == SUCCESS_EXIT_CODE

  begin
    output_arguments if verbose? && !shell_command?

    # By this time, the Main class has removed the command line options, and all that is left
    # in argv is the command and its options.
    process_command_line
    SUCCESS_EXIT_CODE
  rescue WifiWand::Error => e
    @err_stream.puts(error_message_for(e))
    @err_stream.puts help_hint if append_help_hint?(e)
    FAILURE_EXIT_CODE
  end
end

#modelObject



53
54
55
# File 'lib/wifi_wand/command_line_interface.rb', line 53

def model
  @model ||= WifiWand.create_model(@model_options)
end

#out_streamObject

Dynamic output stream that respects current $stdout (for test silence_output compatibility)



58
# File 'lib/wifi_wand/command_line_interface.rb', line 58

def out_stream = @original_out_stream || $stdout

#output_supportObject



60
# File 'lib/wifi_wand/command_line_interface.rb', line 60

def output_support = @output_support ||= Commands::OutputSupport.new(self)

#process_command_line(argv = @argv) ⇒ Object

Processes the command (ARGV) and any relevant options (ARGV).

CAUTION! In interactive mode, any strings entered (e.g. a network name) MUST be in a form that the Ruby interpreter will recognize as a string, i.e. single or double quotes, %q, %Q, etc. Otherwise it will assume it's a method name and pass it to method_missing!



79
80
81
82
83
84
# File 'lib/wifi_wand/command_line_interface.rb', line 79

def process_command_line(argv = @argv)
  attempt_command_action(argv[0], *argv[1..]) do
    raise WifiWand::BadCommandError,
      "Unrecognized command. Command was #{argv.first.inspect} and options were #{argv[1..].inspect}."
  end
end

#validate_command_line(argv = @argv) ⇒ Object

Asserts that a command has been passed on the command line.



63
64
65
66
67
68
69
70
71
# File 'lib/wifi_wand/command_line_interface.rb', line 63

def validate_command_line(argv = @argv)
  if argv.empty?
    @err_stream.puts "Syntax is: #{File.basename($PROGRAM_NAME)} [options] command [args]. " \
      "#{help_hint}"
    return FAILURE_EXIT_CODE
  end

  SUCCESS_EXIT_CODE
end

#verbose?Boolean

Returns:

  • (Boolean)


51
# File 'lib/wifi_wand/command_line_interface.rb', line 51

def verbose? = options.verbose

#with_interactive_modeObject



159
160
161
162
163
164
165
# File 'lib/wifi_wand/command_line_interface.rb', line 159

def with_interactive_mode
  previous_mode = @interactive_mode
  @interactive_mode = true
  yield
ensure
  @interactive_mode = previous_mode
end