class Mudguard::Infrastructure::Cli::Controller

Parses the cli arguments

Public Class Methods

new(view:) click to toggle source
# 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

Public Instance Methods

parse!(argv) click to toggle source
# 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

Private Instance Methods

check_dependencies(directories) click to toggle source
# File lib/mudguard/infrastructure/cli/controller.rb, line 55
def check_dependencies(directories)
  yield_directories(directories) do |directory, notification|
    Mudguard::Application.check(directory, notification)
  end
end
help() click to toggle source
# File lib/mudguard/infrastructure/cli/controller.rb, line 50
def help
  @view.print(@parser.to_s)
  true
end
print_allowed_dependencies(directories) click to toggle source
yield_directories(directories) { |directory, notification| ... } click to toggle source
# File lib/mudguard/infrastructure/cli/controller.rb, line 68
def yield_directories(directories)
  notification = NotificationAdapter.new(**@display_opts)
  directories = [Dir.pwd] if directories.empty?
  directories.all? do |directory|
    yield directory, notification
  end
end