Class: Samovar::Many

Inherits:
Object
  • Object
show all
Defined in:
lib/samovar/many.rb

Overview

Represents multiple positional arguments in a command.

A Many parser extracts all arguments from the command line until it encounters a stop pattern (typically an option flag).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, description = nil, stop: /^-/, default: nil, required: false, completions: nil) ⇒ Many

Initialize a new multi-argument parser.



21
22
23
24
25
26
27
28
# File 'lib/samovar/many.rb', line 21

def initialize(key, description = nil, stop: /^-/, default: nil, required: false, completions: nil)
  @key = key
  @description = description
  @stop = stop
  @default = default
  @required = required
  @completions = completions
end

Instance Attribute Details

#completionsObject (readonly)

Completions for these arguments.



58
59
60
# File 'lib/samovar/many.rb', line 58

def completions
  @completions
end

#defaultObject

The default value if no arguments are provided.



48
49
50
# File 'lib/samovar/many.rb', line 48

def default
  @default
end

#descriptionObject (readonly)

A description of the arguments for help output.



38
39
40
# File 'lib/samovar/many.rb', line 38

def description
  @description
end

#keyObject

The name of the attribute to store the values in.



33
34
35
# File 'lib/samovar/many.rb', line 33

def key
  @key
end

#requiredObject

Whether at least one argument is required.



53
54
55
# File 'lib/samovar/many.rb', line 53

def required
  @required
end

#stopObject

A pattern that indicates the end of this argument list.



43
44
45
# File 'lib/samovar/many.rb', line 43

def stop
  @stop
end

Instance Method Details

#complete(input, context, collected) ⇒ Object

Complete this repeating positional argument.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/samovar/many.rb', line 106

def complete(input, context, collected)
  if @stop
    input.shift while input.any? && !(@stop === input.first)
    
    return nil if @stop === context.current
  else
    input.clear
  end
  
  if input.empty?
    return Completion::Result.new(collected) + Completion::Provider.new(context.with_row(self), @completions).suggestions
  end
  
  return nil
end

#parse(input, parent = nil, default = nil) ⇒ Object

Parse multiple arguments from the input.



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/samovar/many.rb', line 88

def parse(input, parent = nil, default = nil)
  if @stop and stop_index = input.index{|item| @stop === item}
    input.shift(stop_index)
  elsif input.any?
    input.shift(input.size)
  elsif default ||= @default
    return default
  elsif @required
    raise MissingValueError.new(parent, @key)
  end
end

#to_aObject

Generate an array representation for usage output.



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/samovar/many.rb', line 70

def to_a
  usage = [to_s, @description]
  
  if @default
    usage << "(default: #{@default.inspect})"
  elsif @required
    usage << "(required)"
  end
  
  return usage
end

#to_sObject

Generate a string representation for usage output.



63
64
65
# File 'lib/samovar/many.rb', line 63

def to_s
  "<#{key}...>"
end