class YoptaSwitcher::CLI::OptionsParser

Constants

BROWSER_MAPPING
OPERATION_NAME_TO_CLASS_MAPPING

Attributes

argv[R]
options[R]
parser[R]

Public Class Methods

new(argv:, options:) click to toggle source
# File lib/yopta_switcher/cli/options_parser.rb, line 19
def initialize(argv:, options:)
  @options = options
  @argv = argv
  @parser = OptionParser.new
  define_options
end

Public Instance Methods

define_options() click to toggle source
# File lib/yopta_switcher/cli/options_parser.rb, line 26
def define_options
  define_login_url_option
  define_login_option
  define_password_option
  define_wait_option
  define_operation_option
  define_browser_option
end
parse!() click to toggle source
# File lib/yopta_switcher/cli/options_parser.rb, line 35
def parse!
  set_default_options
  parser.parse!(argv)
  options
end

Private Instance Methods

define_browser_option() click to toggle source
# File lib/yopta_switcher/cli/options_parser.rb, line 87
def define_browser_option
  parser.on(
    '-b Browser',
    BROWSER_MAPPING.keys,
    BROWSER_MAPPING.values.join(', ')
  ) do |browser|
    options.browser = BROWSER_MAPPING[browser]
  end
end
define_login_option() click to toggle source
# File lib/yopta_switcher/cli/options_parser.rb, line 55
def define_login_option
  parser.on('-l Login') do |login|
    options.login = login
  end
end
define_login_url_option() click to toggle source
# File lib/yopta_switcher/cli/options_parser.rb, line 49
def define_login_url_option
  parser.on('-u Login URL', URI) do |url|
    options.login_url = url.to_s
  end
end
define_operation_option() click to toggle source
# File lib/yopta_switcher/cli/options_parser.rb, line 77
def define_operation_option
  parser.on(
    '-o Operation',
    OPERATION_NAME_TO_CLASS_MAPPING.keys,
    OPERATION_NAME_TO_CLASS_MAPPING.keys.join(', ')
  ) do |operation|
    options.operation_class = OPERATION_NAME_TO_CLASS_MAPPING[operation]
  end
end
define_password_option() click to toggle source
# File lib/yopta_switcher/cli/options_parser.rb, line 61
def define_password_option
  parser.on('-p Password') do |password|
    options.password = password
  end
end
define_wait_option() click to toggle source
# File lib/yopta_switcher/cli/options_parser.rb, line 67
def define_wait_option
  parser.on(
    '-w Wait timeout in seconds',
    OptionParser::DecimalInteger,
    '10'
  ) do |wait|
    options.wait_timeout = wait
  end
end
set_default_options() click to toggle source
# File lib/yopta_switcher/cli/options_parser.rb, line 43
def set_default_options
  options.wait_timeout = 10
  options.browser = :phantomjs
  options.operation_class = YoptaSwitcher::Operations::TurnOn
end