class Fuelcell::Parser::ParsingStrategy

Attributes

arg_handler[R]
ignore_handler[R]
opt_handlers[R]

Public Class Methods

new() click to toggle source

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

# 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

Public Instance Methods

call(cmd, cmd_args, raw_args, opts = Fuelcell::Action::OptResults.new) click to toggle source

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

@param cmd [Fuelcell::Command] @param raw_args [Array] raw args from ARGV @param opts [Hash] stores processed options @return [Array] the process options and processed args

# 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

Private Instance Methods

format_return(cmd, opts, cmd_args, args, ignores) click to toggle source
# File lib/fuelcell/parser/parsing_strategy.rb, line 71
def format_return(cmd, opts, cmd_args, args, ignores)
  {
    cmd_args: cmd_args,
    cmd: cmd,
    opts: opts,
    args: args,
    ignores: ignores
  }
end
run_option_handlers(cmd, raw_args, opts) click to toggle source
# File lib/fuelcell/parser/parsing_strategy.rb, line 60
def run_option_handlers(cmd, raw_args, opts)
  args = []
  until raw_args.empty?
    opt_handlers.each do |handler|
      break if handler.call(cmd, raw_args, opts)
    end
    args << raw_args.shift if arg?(raw_args.first)
  end
  args
end