class Split
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.
Definitions
def initialize(key, description, marker: "--", default: nil, required: false, completions: nil)
Initialize a new split parser.
Signature
-
parameter
keySymbol The name of the attribute to store the values after the split.
-
parameter
descriptionString A description of the split for help output.
-
parameter
markerString The marker that indicates the split point.
-
parameter
defaultObject The default value if no split is present.
-
parameter
requiredBoolean Whether the split is required.
-
parameter
completionsArray | Proc | Nil Completions for split arguments.
Implementation
def initialize(key, description, marker: "--", default: nil, required: false, completions: nil)
@key = key
@description = description
@marker = marker
@default = default
@required = required
@completions = completions
end
attr :key
The name of the attribute to store the values after the split.
Signature
-
attribute
Symbol
attr :description
A description of the split for help output.
Signature
-
attribute
String
attr :marker
The marker that indicates the split point.
Signature
-
attribute
String
attr :default
The default value if no split is present.
Signature
-
attribute
Object
attr :required
Whether the split is required.
Signature
-
attribute
Boolean
attr :completions
Completions for split arguments.
Signature
-
attribute
Array | Proc | Nil
def to_s
Generate a string representation for usage output.
Signature
-
returns
String The usage string.
Implementation
def to_s
"#{@marker} <#{@key}...>"
end
def to_a
Generate an array representation for usage output.
Signature
-
returns
Array The usage array.
Implementation
def to_a
usage = [to_s, @description]
if @default
usage << "(default: #{@default.inspect})"
elsif @required
usage << "(required)"
end
return usage
end
def parse(input, parent = nil, default = nil)
Parse arguments after the split marker.
Signature
-
parameter
inputArray(String) The command-line arguments.
-
parameter
parentCommand | Nil The parent command.
-
parameter
defaultObject | Nil An override for the default value.
-
returns
Array(String) | Object | Nil The arguments after the split, or the default if no split.
Implementation
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
def complete(input, context, collected)
Complete the split marker or arguments after it.
Signature
-
parameter
inputArray(String) Previously completed command-line arguments.
-
parameter
contextCompletion::Context The completion context.
-
parameter
collectedArray(Completion::Suggestion) Suggestions collected so far.
-
returns
Completion::Result | Nil A final completion result, or nil to continue.
Implementation
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.arguments.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