Class: Reviewer::Arguments

Inherits:
Object
  • Object
show all
Defined in:
lib/reviewer/arguments.rb,
lib/reviewer/arguments/tags.rb,
lib/reviewer/arguments/files.rb,
lib/reviewer/arguments/options.rb,
lib/reviewer/arguments/keywords.rb

Overview

Handles option parsing for rvw and fmt commands

Examples:


`rvw`
`rvw -t ruby`
`rvw -f ./example.rb,./example_test.rb`
`rvw staged`
`rvw --files ./example.rb,./example_test.rb --tags syntax`
`rvw ruby staged`

Defined Under Namespace

Modules: Options Classes: Files, Keywords, Tags

Constant Summary collapse

KNOWN_FORMATS =

Valid output format options for the --format flag

i[streaming summary json].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = ARGV, output: Output.new) ⇒ self

Parses command-line arguments and makes them available as tags, files, and keywords.

Examples:

Using all options: rvw keyword_one keyword_two --files ./example.rb,./example_test.rb --tags syntax

reviewer = Reviewer::Arguments.new
reviewer.files.to_a # => ['./example.rb','./example_test.rb']
reviewer.tags.to_a # => ['syntax']
reviewer.keywords.to_a # => ['keyword_one', 'keyword_two']


43
44
45
46
47
48
49
50
# File 'lib/reviewer/arguments.rb', line 43

def initialize(options = ARGV, output: Output.new)
  @output = output
  @raw_options = options.to_a.dup
  @invalid_files_option = false
  @parser = Slop::Options.new
  Options.configure(@parser, files_callback: files_validation_callback)
  @options = @parser.parse(normalized_options)
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



28
29
30
# File 'lib/reviewer/arguments.rb', line 28

def options
  @options
end

#outputObject (readonly)

Returns the value of attribute output.



30
31
32
# File 'lib/reviewer/arguments.rb', line 30

def output
  @output
end

Instance Method Details

#capabilities?Boolean

Whether the --capabilities flag was passed



115
# File 'lib/reviewer/arguments.rb', line 115

def capabilities? = options[:capabilities]

#filesArguments::Files

The file arguments collected from the command line via the -f or --files flag



78
79
80
81
82
83
84
85
# File 'lib/reviewer/arguments.rb', line 78

def files
  @files ||= Arguments::Files.new(
    provided: options[:files],
    keywords: keywords.for_files,
    output: output,
    on_git_error: session_formatter.method(:git_error)
  )
end

#formatSymbol

The output format for results



125
126
127
128
129
130
131
132
133
# File 'lib/reviewer/arguments.rb', line 125

def format
  return :json if json?

  value = options[:format].to_sym
  return value if KNOWN_FORMATS.include?(value)

  session_formatter.invalid_format(options[:format], KNOWN_FORMATS) unless invalid_files_option?
  :streaming
end

#help?Boolean

Whether the --help flag was passed



95
# File 'lib/reviewer/arguments.rb', line 95

def help? = options[:help]

#invalid_files_option?Boolean

Whether a files option was supplied without a usable value



120
# File 'lib/reviewer/arguments.rb', line 120

def invalid_files_option? = @invalid_files_option

#json?Boolean

Whether to output results as JSON



110
# File 'lib/reviewer/arguments.rb', line 110

def json? = options[:json]

#keywordsArguments::Keywords

The leftover arguments collected from the command line without being associated with a flag



90
# File 'lib/reviewer/arguments.rb', line 90

def keywords = @keywords ||= Arguments::Keywords.new(options.arguments)

#raw?Boolean

Whether to force raw/passthrough output regardless of tool count



105
# File 'lib/reviewer/arguments.rb', line 105

def raw? = options[:raw]

#runner_strategy(multiple_tools:) ⇒ Class

Determines the appropriate runner strategy based on CLI flags



144
145
146
147
148
149
# File 'lib/reviewer/arguments.rb', line 144

def runner_strategy(multiple_tools:)
  return Runner::Strategies::Passthrough if raw?
  return Runner::Strategies::Captured unless streaming?

  multiple_tools ? Runner::Strategies::Captured : Runner::Strategies::Passthrough
end

#streaming?Boolean

Whether output should be streamed directly (not captured for later formatting)



138
# File 'lib/reviewer/arguments.rb', line 138

def streaming? = format == :streaming

#tagsArguments::Tags

The tag arguments collected from the command line via the -t or --tags flag



73
# File 'lib/reviewer/arguments.rb', line 73

def tags = @tags ||= Arguments::Tags.new(provided: options[:tags])

#to_hHash Also known as: inspect

Converts the arguments to a hash for versatility



61
62
63
64
65
66
67
# File 'lib/reviewer/arguments.rb', line 61

def to_h
  {
    files: files.raw,
    tags: tags.raw,
    keywords: keywords.raw
  }
end

#version?Boolean

Whether the --version flag was passed



100
# File 'lib/reviewer/arguments.rb', line 100

def version? = options[:version]