Class: Mudguard::Infrastructure::Cli::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/mudguard/infrastructure/cli/controller.rb

Overview

Parses the cli arguments

Instance Method Summary collapse

Constructor Details

#initialize(view:) ⇒ Controller

rubocop:disable Metrics/MethodLength



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mudguard/infrastructure/cli/controller.rb', line 12

def initialize(view:) # rubocop:disable Metrics/MethodLength
  @cmd = :analyse
  @view = view
  @display_opts = {
    view: view,
    compressed: false
  }
  @parser = ::OptionParser.new do |opts|
    opts.banner = "Usage: mudguard [options] [directory]"
    opts.on("-h", "--help", "Prints this help") do
      @cmd = :help
    end
    opts.on("-p", "--print", "Prints all allowed dependencies") do
      @cmd = :print_allowed
    end
    opts.on("-c", "--compressed", "Omits printing the same dependency more than once") do
      @display_opts[:compressed] = true
    end
  end
end

Instance Method Details

#parse!(argv) ⇒ Object

rubocop:disable Metrics/MethodLength



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/mudguard/infrastructure/cli/controller.rb', line 33

def parse!(argv) # rubocop:disable Metrics/MethodLength
  directories = @parser.parse!(argv)

  case @cmd
  when :print_allowed
    print_allowed_dependencies(directories)
  when :help
    help
  when :analyse
    check_dependencies(directories)
  else
    raise StandardError, "unknown command #{@cmd}"
  end
end