Class: Fuelcell::Parser::ParsingStrategy
- Inherits:
-
BaseHandler
- Object
- BaseHandler
- Fuelcell::Parser::ParsingStrategy
- Defined in:
- lib/fuelcell/parser/parsing_strategy.rb
Instance Attribute Summary collapse
-
#arg_handler ⇒ Object
readonly
Returns the value of attribute arg_handler.
-
#ignore_handler ⇒ Object
readonly
Returns the value of attribute ignore_handler.
-
#opt_handlers ⇒ Object
readonly
Returns the value of attribute opt_handlers.
Instance Method Summary collapse
-
#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.
-
#initialize ⇒ ParsingStrategy
constructor
Arrange all the handlers in the correct order for this strategy.The order is as follows: 1.
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
#initialize ⇒ ParsingStrategy
Arrange all the handlers in the correct order for this strategy.The order is as follows:
-
short clusters
-
opts using equals
-
short opts that have no space between them and their value
-
all other option conditions
-
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_handler ⇒ Object (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_handler ⇒ Object (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_handlers ⇒ Object (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
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 |