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
-
#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
-
#initialize(key, description, marker: "--", default: nil, required: false) ⇒ 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) ⇒ Split
Initialize a new split parser.
18 19 20 21 22 23 24 |
# File 'lib/samovar/split.rb', line 18 def initialize(key, description, marker: "--", default: nil, required: false) @key = key @description = description @marker = marker @default = default @required = required end |
Instance Attribute Details
#default ⇒ Object
The default value if no split is present.
44 45 46 |
# File 'lib/samovar/split.rb', line 44 def default @default end |
#description ⇒ Object (readonly)
A description of the split for help output.
34 35 36 |
# File 'lib/samovar/split.rb', line 34 def description @description end |
#key ⇒ Object
The name of the attribute to store the values after the split.
29 30 31 |
# File 'lib/samovar/split.rb', line 29 def key @key end |
#marker ⇒ Object (readonly)
The marker that indicates the split point.
39 40 41 |
# File 'lib/samovar/split.rb', line 39 def marker @marker end |
#required ⇒ Object
Whether the split is required.
49 50 51 |
# File 'lib/samovar/split.rb', line 49 def required @required end |
Instance Method Details
#parse(input, parent = nil, default = nil) ⇒ Object
Parse arguments after the split marker.
79 80 81 82 83 84 85 86 87 |
# File 'lib/samovar/split.rb', line 79 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.
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/samovar/split.rb', line 61 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.
54 55 56 |
# File 'lib/samovar/split.rb', line 54 def to_s "#{@marker} <#{@key}...>" end |