class Samovar::Many
A ‘Many` parser extracts all arguments from the command line until it encounters a stop pattern (typically an option flag).
Represents multiple positional arguments in a command.
def initialize(key, description = nil, stop: /^-/, default: nil, required: false)
@parameter default [Object] The default value if no arguments are provided.
@parameter stop [Regexp] A pattern that indicates the end of this argument list.
@parameter description [String | Nil] A description of the arguments for help output.
@parameter key [Symbol] The name of the attribute to store the values in.
Initialize a new multi-argument parser.
def initialize(key, description = nil, stop: /^-/, default: nil, required: false) @key = key @description = description @stop = stop @default = default @required = required end
def parse(input, parent = nil, default = nil)
@parameter default [Object | Nil] An override for the default value.
@parameter parent [Command | Nil] The parent command.
@parameter input [Array(String)] The command-line arguments.
Parse multiple arguments from the input.
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 to_a
Generate an array representation for usage output.
def to_a usage = [to_s, @description] if @default usage << "(default: #{@default.inspect})" elsif @required usage << "(required)" end return usage end
def to_s
Generate a string representation for usage output.
def to_s "<#{key}...>" end