Class: ArgParser::RestArgument

Inherits:
ValueArgument show all
Defined in:
lib/arg-parser/argument.rb

Overview

A command-line argument that takes 0 to N values from the command-line.

Instance Attribute Summary

Attributes inherited from ValueArgument

#sensitive, #usage_value, #validation

Attributes inherited from Argument

#default, #description, #key, #on_parse, #short_key, #usage_break

Instance Method Summary collapse

Methods inherited from Argument

lookup, register, to_key

Constructor Details

#initialize(key, desc, opts = {}, &block) ⇒ RestArgument

Creates a new rest argument, which is an argument that consumes all remaining positional argument values.

Options Hash (opts):

  • :min_values (Fixnum)

    The minimum number of rest values that must be supplied. Defaults to 1 if the RestArgument is required, or 0 if it is not.



472
473
474
475
476
# File 'lib/arg-parser/argument.rb', line 472

def initialize(key, desc, opts = {}, &block)
    super
    @min_values = opts.fetch(:min_values, opts.fetch(:required, true) ? 1 : 0)
    @default = [@default] if @default.is_a?(String)
end

Instance Method Details

#requiredObject



478
479
480
# File 'lib/arg-parser/argument.rb', line 478

def required
  @min_values > 0
end

#to_sString



484
485
486
# File 'lib/arg-parser/argument.rb', line 484

def to_s
    usage_value
end

#to_useString



490
491
492
# File 'lib/arg-parser/argument.rb', line 490

def to_use
    required? ? "#{usage_value} [...]" : "[#{usage_value} [...]]"
end