Class: Brigadier::Runner

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/brigadier/runner.rb

Instance Method Summary collapse

Methods included from Helper

#available_params_for, #display_help_for, #display_help_if_requested, #help, #help_requested?, #inverse_toggle_arg?, #option_or_toggle?, #sub_command?

Constructor Details

#initialize(args) ⇒ Runner

Returns a new instance of Runner.



7
8
9
# File 'lib/brigadier/runner.rb', line 7

def initialize(args)
  @args = args
end

Instance Method Details

#run(*klasses) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/brigadier/runner.rb', line 11

def run(*klasses)
  remaining_args = []
  args_to_process = []
  sub_command_to_execute = false
  full_args = args.dup

  collect_execute_blocks(klasses)

  klasses.each do |klass|
    remaining_args = args.dup

    until remaining_args.empty?
      arg = remaining_args.shift

      klass.sub_commands.each do |names, sub_command|
        next unless names.include?(arg)
        sub_command_to_execute = sub_command
        break
      end

      break if sub_command_to_execute

      args_to_process << arg
    end

    break if sub_command_to_execute
  end

  if sub_command_to_execute
    args_to_process += remaining_args
    sub_command_to_execute.execute(args_to_process, full_args)
  elsif default_command
    default_command.execute(args, full_args, klasses)
  elsif help_requested?(args)
    $stderr.puts
    help(klasses)
  else
    raise Exceptions::ExecuteBlockMissing.new(self), 'There is no execute {} block defined'
  end
rescue Exceptions::ExecuteBlockMissing, Exceptions::Base => e
  $stderr.puts "ERROR: #{e.message}"
  exit(Exceptions::ERROR_EXIT_CODE)
end