Class: Fuelcell::Parser::ParsingStrategy

Inherits:
BaseHandler show all
Defined in:
lib/fuelcell/parser/parsing_strategy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseHandler

#arg?, #assign_opt_value, #cast, #cast_array, #cast_bool, #cast_hash, #cast_numeric, #find_opt, #found_opt_flag, #take_first_arg

Constructor Details

#initializeParsingStrategy

Arrange all the handlers in the correct order for this strategy.The order is as follows:

  1. short clusters

  2. opts using equals

  3. short opts that have no space between them and their value

  4. all other option conditions

  5. check that all required options have been processed.

The check for required options is designed to be handled last



21
22
23
24
25
26
27
28
29
30
# File 'lib/fuelcell/parser/parsing_strategy.rb', line 21

def initialize
  @ignore_handler = IgnoreHandler.new
  @opt_handlers   = [
    ShortOptNoSpaceHandler.new,
    OptValueEqualHandler.new,
    OptHandler.new
  ]

  @arg_handler = ArgHandler.new
end

Instance Attribute Details

#arg_handlerObject (readonly)

Returns the value of attribute arg_handler.



10
11
12
# File 'lib/fuelcell/parser/parsing_strategy.rb', line 10

def arg_handler
  @arg_handler
end

#ignore_handlerObject (readonly)

Returns the value of attribute ignore_handler.



10
11
12
# File 'lib/fuelcell/parser/parsing_strategy.rb', line 10

def ignore_handler
  @ignore_handler
end

#opt_handlersObject (readonly)

Returns the value of attribute opt_handlers.



10
11
12
# File 'lib/fuelcell/parser/parsing_strategy.rb', line 10

def opt_handlers
  @opt_handlers
end

Instance Method Details

#call(cmd, cmd_args, raw_args, opts = Fuelcell::Action::OptResults.new) ⇒ Array

Run all handlers in order, in a continuous loop until all raw args are empty. Handlers implement the call method as a strategy pattern so they all have the same signature. During the loop and after all the handlers have run, we check the first arg, if it’s not an opt it means it’s a arg so we move it to the args array. This process continues until there are no other args. Required options are not inforced for the help command

Parameters:

  • cmd (Fuelcell::Command)
  • raw_args (Array)

    raw args from ARGV

  • opts (Hash) (defaults to: Fuelcell::Action::OptResults.new)

    stores processed options

Returns:

  • (Array)

    the process options and processed args



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/fuelcell/parser/parsing_strategy.rb', line 44

def call(cmd, cmd_args, raw_args, opts = Fuelcell::Action::OptResults.new)
  ignores = ignore_handler.call(raw_args)
  args = run_option_handlers(cmd, raw_args, opts)
  args = arg_handler.call(cmd, args)

  unless cmd.name == 'help' || cmd.opts.callable?
    cmd.missing_opts(opts.keys) do |missing|
      fail "option [#{missing.first.cli_names}] is required"
    end
  end

  format_return(cmd, opts, cmd_args, args, ignores)
end