Class: LintTrappings::ArgumentsParser

Inherits:
Object
  • Object
show all
Defined in:
lib/lint_trappings/arguments_parser.rb

Overview

Handles option parsing for the command line application.

Instance Method Summary collapse

Constructor Details

#initialize(application) ⇒ ArgumentsParser

Returns a new instance of ArgumentsParser.



6
7
8
# File 'lib/lint_trappings/arguments_parser.rb', line 6

def initialize(application)
  @application = application
end

Instance Method Details

#parse(args) ⇒ Hash

Parses command line options into an options hash.

Parameters:

  • args (Array<String>)

    arguments passed via the command line

Returns:

  • (Hash)

    parsed options



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/lint_trappings/arguments_parser.rb', line 15

def parse(args)
  @options = {}
  @options[:command] = :scan # Default command is to scan for lints

  OptionParser.new do |parser|
    parser.banner = "Usage: #{@application.executable_name} [options] [file1, file2, ...]"

    add_linter_options parser
    add_file_options parser
    add_misc_options parser
    add_info_options parser
  end.parse!(args)

  # Any remaining arguments are assumed to be files that should be linted
  @options[:included_paths] = args

  @options
rescue OptionParser::InvalidOption => ex
  raise InvalidCliOptionError,
        "#{ex.message}\nRun `#{@application.executable_name} --help` to " \
        'see a list of available options.'
end