Class: WifiWand::Main

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

Constant Summary collapse

SUCCESS_EXIT_CODE =
0
FAILURE_EXIT_CODE =
1
INTERRUPT_EXIT_CODE =
130
CLI_BOUNDARY_ERROR =

StandardError excludes process-control and VM-level exceptions like Interrupt, SystemExit, and NoMemoryError.

StandardError

Instance Method Summary collapse

Constructor Details

#initialize(out_stream = $stdout, err_stream = $stderr, argv: ARGV, env: ENV, in_stream: $stdin) ⇒ Main

Returns a new instance of Main.



21
22
23
24
25
26
27
# File 'lib/wifi_wand/main.rb', line 21

def initialize(out_stream = $stdout, err_stream = $stderr, argv: ARGV, env: ENV, in_stream: $stdin)
  @out_stream = out_stream
  @err_stream = err_stream
  @in_stream = in_stream
  @argv = argv
  @env = env
end

Instance Method Details

#call(argv = @argv) ⇒ Object



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

def call(argv = @argv)
  options = CommandLineParser.new(argv, @env, @err_stream).parse
  if options.version_requested
    @out_stream.puts(WifiWand::VERSION)
    return SUCCESS_EXIT_CODE
  end
  # Ensure CLI and model share the main's output streams
  options.out_stream = @out_stream
  options.err_stream = @err_stream
  options.in_stream = @in_stream
  WifiWand::CommandLineInterface.new(options, argv: options.argv).call
rescue Interrupt => e
  handle_interrupt(e, options)
  INTERRUPT_EXIT_CODE
rescue CLI_BOUNDARY_ERROR => e
  # For option parsing errors, we don't have options.verbose yet, so default to false
  verbose = !!options&.verbose
  handle_error(e, verbose)
  FAILURE_EXIT_CODE
end