Class: AppTester::Parser Abstract

Inherits:
OptionParser
  • Object
show all
Defined in:
lib/app-tester/parser.rb

Overview

This class is abstract.

Parser handler for command line options. This uses optparse ruby gem

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ AppTester::Parser

Build Parser object. Automatically builds with –server argument

Parameters:

  • options (AppTester::Options)

    the options that the user defined when he created the framework



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/app-tester/parser.rb', line 18

def initialize options
  @options = { }
  @test_options = options
  @mandatory_arguments = {}
  @missing_arguments = []
  super do |x|
    x.separator ''
  end

  # Fallback to the first entry on the environments list if there's not default environment selected
  default_environment = @test_options.default_environment.nil? ? @test_options.environments.keys.first : @test_options.default_environment
  @options[:server] = @test_options.environments[default_environment]

  set_option(:server, "-s", "--server OPT", @test_options.environments.keys, "Server to connect. Default: #{default_environment}")

  options.default_options.each do |opt|
    set_option(opt[:symbol].to_sym, *opt[:opts], &opt[:block])
  end
end

Instance Attribute Details

#optionsHash (readonly)

command line arguments that were set when executing the script

Returns:

  • (Hash)

    the current value of options



8
9
10
# File 'lib/app-tester/parser.rb', line 8

def options
  @options
end

#test_optionsAppTester::Options (readonly)

the options that the user defined when he created the framework

Returns:



8
9
10
# File 'lib/app-tester/parser.rb', line 8

def test_options
  @test_options
end

Instance Method Details

#check_mandatory_argumentsObject



62
63
64
65
66
# File 'lib/app-tester/parser.rb', line 62

def check_mandatory_arguments
  @mandatory_arguments.each{|a| a = a[1]; @missing_arguments << "Please supply #{a[:argument]} / #{a[:switch]}" unless @options[a[:key]] }
  @missing_arguments.each{|a| puts a }
  exit(1) if @missing_arguments.any?
end

#set_option(symbol, *opts, &block) ⇒ Object

Add a new option to our optparser

Parameters:

  • symbol (Symbol)

    identifier that will be used on the yielded block on define_test

  • opts (Array)

    command line options definition

  • block (Proc)

    custom code to be executed. Optional

See Also:



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/app-tester/parser.rb', line 46

def set_option(symbol, *opts, &block)
  @mandatory_arguments[symbol] = {:key => symbol, :argument => opts[0], :switch => opts[1]} if opts[3] == true
  if block.nil?
    on(*opts) do |x|
      case symbol
        when :server
          @options[symbol] = @test_options.environments[x]
        else
          @options[symbol] = x
      end
    end
  else
    on(*opts, &block)
  end
end