Class: AnnotateRb::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/annotate_rb/parser.rb

Overview

Class for handling command line arguments

Constant Summary collapse

<<~BANNER.freeze
  Usage: annotaterb [command] [options]

  Commands:
      models [options]
      routes [options]
      help
      version
BANNER
DEFAULT_OPTIONS =
{
  target_action: :do_annotations,
  exit: false
}.freeze
ANNOTATION_POSITIONS =
%w[before top after bottom].freeze
FILE_TYPE_POSITIONS =
%w[position_in_class position_in_factory position_in_fixture position_in_test position_in_routes position_in_serializer].freeze
EXCLUSION_LIST =
%w[tests fixtures factories serializers].freeze
FORMAT_TYPES =
%w[bare rdoc yard markdown].freeze
COMMAND_MAP =
{
  "models" => :models,
  "routes" => :routes,
  "version" => :version,
  "help" => :help
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args, existing_options) ⇒ Parser

Returns a new instance of Parser.



37
38
39
40
41
42
43
# File 'lib/annotate_rb/parser.rb', line 37

def initialize(args, existing_options)
  @args = args.clone
  base_options = DEFAULT_OPTIONS.dup
  @options = base_options.merge(existing_options)
  @commands = []
  @options[:original_args] = args.clone
end

Class Method Details

.parse(args, existing_options) ⇒ Object

rubocop:disable Metrics/ClassLength



6
7
8
# File 'lib/annotate_rb/parser.rb', line 6

def self.parse(args, existing_options)
  new(args, existing_options).parse
end

Instance Method Details

#parseObject



45
46
47
48
49
50
51
52
53
# File 'lib/annotate_rb/parser.rb', line 45

def parse
  parse_command(@args)

  parser.parse!(@args)

  act_on_command

  @options
end

#remaining_argsObject



55
56
57
58
59
# File 'lib/annotate_rb/parser.rb', line 55

def remaining_args
  # `@args` gets modified throughout the lifecycle of this class.
  # It starts as a shallow clone of ARGV, then arguments matching commands and options are removed in #parse
  @args
end