class Umarell::Arguments

The class responsible of parsing command line arguments

Constants

DEFAULT_TARGET
MODIFIED_FILES_TARGET

Attributes

autofix[R]
autofix?[R]
target[R]

Public Class Methods

new() click to toggle source
# File lib/umarell/arguments.rb, line 15
def initialize
  @autofix = false
  @modified = false
  @target = DEFAULT_TARGET
end

Public Instance Methods

parse() click to toggle source

Parse command line arguments

# File lib/umarell/arguments.rb, line 22
def parse
  parse_options
  parse_target
end

Private Instance Methods

exit_with_message(message, code = 0) click to toggle source
# File lib/umarell/arguments.rb, line 67
def exit_with_message(message, code = 0)
  puts "umarell: #{message}"
  exit code
end
on_autofix(opts) click to toggle source
# File lib/umarell/arguments.rb, line 49
def on_autofix(opts)
  opts.on('-a', '--autofix', 'Autofix violations (if supported)') do
    @autofix = true
  end
end
on_modified(opts) click to toggle source
# File lib/umarell/arguments.rb, line 55
def on_modified(opts)
  opts.on('-m', '--modified', 'Run on modified files') do
    @modified = true
  end
end
on_version(opts) click to toggle source
# File lib/umarell/arguments.rb, line 61
def on_version(opts)
  opts.on_tail('-v', '--version', 'Show version') do
    exit_with_message(Version::STRING)
  end
end
parse_options() click to toggle source
# File lib/umarell/arguments.rb, line 29
def parse_options
  OptionParser.new do |opts|
    opts.banner = 'Usage: umarell [options] [target]'
    on_autofix(opts)
    on_modified(opts)
    on_version(opts)
  end.parse!
rescue OptionParser::InvalidOption => e
  exit_with_message("#{e.message}, see 'umarell --help'", 1)
end
parse_target() click to toggle source
# File lib/umarell/arguments.rb, line 40
def parse_target
  last_argument = ARGV.pop || DEFAULT_TARGET
  @target = if @modified
              MODIFIED_FILES_TARGET % last_argument
            else
              last_argument
            end
end