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/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

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']

Parameters:

  • options (Array<String>) (defaults to: ARGV)

    the command-line arguments to parse (defaults to ARGV)

  • output (Output) (defaults to: Output.new)

    the console output handler for displaying messages



42
43
44
45
# File 'lib/reviewer/arguments.rb', line 42

def initialize(options = ARGV, output: Output.new)
  @output = output
  @options = Slop.parse(options) { |opts| configure_options(opts) }
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

#outputObject (readonly)

Returns the value of attribute output.



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

def output
  @output
end

Instance Method Details

#filesArguments::Files

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

Returns:

  • (Arguments::Files)

    a collection of the file arguments collected from the command-line



96
97
98
99
100
101
102
103
# File 'lib/reviewer/arguments.rb', line 96

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

#formatSymbol

The output format for results

Returns:

  • (Symbol)

    the output format (:streaming, :summary, or :json)



133
134
135
136
137
138
139
140
141
# File 'lib/reviewer/arguments.rb', line 133

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)
  :streaming
end

#help?Boolean

Whether the –help flag was passed

Returns:

  • (Boolean)

    true if help was requested



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

def help? = options[:help]

#json?Boolean

Whether to output results as JSON

Returns:

  • (Boolean)

    true if JSON output mode is requested



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

def json? = options[:json]

#keywordsArguments::Keywords

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

Returns:



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

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

#raw?Boolean

Whether to force raw/passthrough output regardless of tool count

Returns:

  • (Boolean)

    true if raw output mode is requested



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

def raw? = options[:raw]

#runner_strategy(multiple_tools:) ⇒ Class

Determines the appropriate runner strategy based on CLI flags

Parameters:

  • multiple_tools (Boolean)

    whether multiple tools are being run

Returns:

  • (Class)

    the strategy class (Captured or Passthrough)



152
153
154
155
156
157
# File 'lib/reviewer/arguments.rb', line 152

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)

Returns:

  • (Boolean)

    true if in streaming mode



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

def streaming? = format == :streaming

#tagsArguments::Tags

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

Returns:

  • (Arguments::Tags)

    a collection of the tag arguments collected from the command-line



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

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

#to_hHash Also known as: inspect

Converts the arguments to a hash for versatility

Returns:

  • (Hash)

    The files, tags, and keywords collected from the command line options



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

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

#version?Boolean

Whether the –version flag was passed

Returns:

  • (Boolean)

    true if version was requested



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

def version? = options[:version]