class Mago::Cli::Command

CLI command.

@example

command = Mago::Cli::Command('-s', '-i', '0,1,2', './lib')
command.execute

Public Class Methods

new(arguments) click to toggle source

@param arguments [Array<String>] command arguments

# File lib/mago/cli/command.rb, line 10
def initialize(arguments)
  @arguments = arguments
  @config    = Config.new
end

Public Instance Methods

execute() click to toggle source

Execute command.

@return [void]

# File lib/mago/cli/command.rb, line 18
def execute
  parse_arguments
  run
end

Private Instance Methods

parse_arguments() click to toggle source

Parse arguments and build config.

@return [void]

# File lib/mago/cli/command.rb, line 30
def parse_arguments
  while arg = @arguments.shift
    case arg
    when /^--/
      process_option(arg)
    when /^-/
      arg[1..-1].each_char {|opt| process_option("-#{opt}") }
    else
      @config.files << arg
    end
  end

  @config.files << '.' if @config.files.empty?
end
print_help() click to toggle source

Show help message.

@return [void]

print_version() click to toggle source

Show program version.

@return [void]

process_option(option) click to toggle source

Process option.

@param option [String] option

@return [void]

# File lib/mago/cli/command.rb, line 50
def process_option(option)
  case option
  when '--help', '-h'
    print_help
    exit 0
  when '--version', '-v'
    print_version
    exit 0
  when '--ignore', '-i'
    val = @arguments.shift
    abort "#{option} option requires comma separated numbers" unless val =~ /^[0-9]/
    nums = val.to_s.split(',').map{ |n| n.include?('.') ? n.to_f : n.to_i }
    @config.ignore = nums
  when '--color', '--colour', '-c'
    @config.color = true
  when '--source', '-s'
    @config.source = true
  else /^-/
    abort("Unknown option `#{option}'")
  end
end
run() click to toggle source

Run command using settings from config.

@return [void]

# File lib/mago/cli/command.rb, line 75
def run
  ruby_files = Mago::Cli::FileFinder.new(@config.files).find
  detector   = Mago::Detector.new(ruby_files, :ignore => @config.ignore)

  formatter_class = @config.source ? SourceFormatter : Formatter
  formatter  = formatter_class.new(:color => @config.color)

  detector.on_file do |file|
    print formatter.format_file(file)
  end

  detector.on_error do |error|
    warn formatter.format_error(error)
  end

  detector.run
end