class Many
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).
Definitions
def initialize(key, description = nil, stop: /^-/, default: nil, required: false, completions: nil)
Initialize a new multi-argument parser.
Signature
-
parameter
keySymbol The name of the attribute to store the values in.
-
parameter
descriptionString | Nil A description of the arguments for help output.
-
parameter
stopRegexp A pattern that indicates the end of this argument list.
-
parameter
defaultObject The default value if no arguments are provided.
-
parameter
requiredBoolean Whether at least one argument is required.
-
parameter
completionsArray | Proc | Nil Completions for these arguments.
Implementation
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
attr :key
The name of the attribute to store the values in.
Signature
-
attribute
Symbol
attr :description
A description of the arguments for help output.
Signature
-
attribute
String | Nil
attr :stop
A pattern that indicates the end of this argument list.
Signature
-
attribute
Regexp
attr :default
The default value if no arguments are provided.
Signature
-
attribute
Object
attr :required
Whether at least one argument is required.
Signature
-
attribute
Boolean
attr :completions
Completions for these 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
"<#{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 multiple arguments from the input.
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 parsed values, or the default if none match.
Implementation
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
def complete(input, context, collected)
Complete this repeating positional argument.
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 @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