Class: Thor::Arguments

Inherits:
Object
  • Object
show all
Includes:
NRSER::Log::Mixin
Defined in:
lib/thor/parser/arguments.rb

Overview

rubocop:disable ClassLength

Direct Known Subclasses

Options

Constant Summary collapse

NUMERIC =
/[-+]?(\d*\.\d+|\d+)/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arguments = []) ⇒ Arguments

Takes an array of Thor::Argument objects.



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/thor/parser/arguments.rb', line 30

def initialize(arguments = [])
  @assigns = {}
  @non_assigned_required = []
  @switches = arguments

  arguments.each do |argument|
    if !argument.default.nil?
      @assigns[argument.human_name] = argument.default
    elsif argument.required?
      @non_assigned_required << argument
    end
  end
end

Class Method Details

.parse(*args) ⇒ Object



23
24
25
26
# File 'lib/thor/parser/arguments.rb', line 23

def self.parse(*args)
  to_parse = args.pop
  new(*args).parse(to_parse)
end

.split(args) ⇒ Object

Receives an array of args and returns two arrays, one with arguments and one with switches.



12
13
14
15
16
17
18
19
20
21
# File 'lib/thor/parser/arguments.rb', line 12

def self.split(args)
  arguments = []

  args.each do |item|
    break if item =~ /^-/
    arguments << item
  end

  [arguments, args[Range.new(arguments.size, -1)]]
end

Instance Method Details

#parse(args) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/thor/parser/arguments.rb', line 45

def parse args
  logger.debug __method__.to_s,
    args: args
  
  @pile = args.dup

  @switches.each do |argument|
    break unless peek
    @non_assigned_required.delete(argument)
    @assigns[argument.human_name] = send  :"parse_#{argument.type}",
                                          argument.human_name
  end

  check_requirement!
  @assigns
end

#remainingObject



63
64
65
# File 'lib/thor/parser/arguments.rb', line 63

def remaining
  @pile
end