Class: Samovar::Split

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

Overview

Represents a split point in the command-line arguments.

A ‘Split` parser divides the argument list at a marker (typically `–`), allowing you to separate arguments meant for your command from those passed to another tool.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, description, marker: "--", default: nil, required: false, completions: nil) ⇒ Split

Initialize a new split parser.



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

def initialize(key, description, marker: "--", default: nil, required: false, completions: nil)
	@key = key
	@description = description
	@marker = marker
	@default = default
	@required = required
	@completions = completions
end

Instance Attribute Details

#completionsObject (readonly)

Completions for split arguments.



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

def completions
  @completions
end

#defaultObject

The default value if no split is present.



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

def default
  @default
end

#descriptionObject (readonly)

A description of the split for help output.



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

def description
  @description
end

#keyObject

The name of the attribute to store the values after the split.



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

def key
  @key
end

#markerObject (readonly)

The marker that indicates the split point.



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

def marker
  @marker
end

#requiredObject

Whether the split is required.



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

def required
  @required
end

Instance Method Details

#complete(input, context, collected) ⇒ Object

Complete the split marker or arguments after it.



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/samovar/split.rb', line 104

def complete(input, context, collected)
	if offset = input.index(@marker)
		input.shift(offset + 1)
		
		if @completions == :executable && input.any?
			return Completion::Result.new(collected + [
				Completion::Suggestion.new(
					input.first,
					description: "Delegate completion",
					type: :delegate,
					index: context.words.index(@marker) + 1,
				),
			])
		end
		
		return Completion::Result.new(collected) + Completion::Provider.new(context.with_row(self), @completions).suggestions
	end
	
	return Completion::Result.new(collected) unless input.empty?
	
	suggestions = []
	
	if @marker.start_with?(context.current)
		suggestions << Completion::Suggestion.new(@marker, description: @description, type: :split)
	end
	
	return Completion::Result.new(collected + suggestions)
end

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

Parse arguments after the split marker.



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

def parse(input, parent = nil, default = nil)
	if offset = input.index(@marker)
		input.pop(input.size - offset).tap(&:shift)
	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/split.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/split.rb', line 63

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