Class: Samovar::Split
- Inherits:
-
Object
- Object
- Samovar::Split
- 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
-
#completions ⇒ Object
readonly
Completions for split arguments.
-
#default ⇒ Object
The default value if no split is present.
-
#description ⇒ Object
readonly
A description of the split for help output.
-
#key ⇒ Object
The name of the attribute to store the values after the split.
-
#marker ⇒ Object
readonly
The marker that indicates the split point.
-
#required ⇒ Object
Whether the split is required.
Instance Method Summary collapse
-
#complete(input, context, collected) ⇒ Object
Complete the split marker or arguments after it.
-
#initialize(key, description, marker: "--", default: nil, required: false, completions: nil) ⇒ Split
constructor
Initialize a new split parser.
-
#parse(input, parent = nil, default = nil) ⇒ Object
Parse arguments after the split marker.
-
#to_a ⇒ Object
Generate an array representation for usage output.
-
#to_s ⇒ Object
Generate a string representation for usage output.
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
#completions ⇒ Object (readonly)
Completions for split arguments.
58 59 60 |
# File 'lib/samovar/split.rb', line 58 def completions @completions end |
#default ⇒ Object
The default value if no split is present.
48 49 50 |
# File 'lib/samovar/split.rb', line 48 def default @default end |
#description ⇒ Object (readonly)
A description of the split for help output.
38 39 40 |
# File 'lib/samovar/split.rb', line 38 def description @description end |
#key ⇒ Object
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 |
#marker ⇒ Object (readonly)
The marker that indicates the split point.
43 44 45 |
# File 'lib/samovar/split.rb', line 43 def marker @marker end |
#required ⇒ Object
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_a ⇒ Object
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_s ⇒ Object
Generate a string representation for usage output.
63 64 65 |
# File 'lib/samovar/split.rb', line 63 def to_s "#{@marker} <#{@key}...>" end |