Sha256: 369926975a519c10f9a351c07f95daa5fa2c741b4006634e0c4911f77bcb8725

Contents?: true

Size: 1.83 KB

Versions: 2

Compression:

Stored size: 1.83 KB

Contents

require 'command_kit/arguments/argument_value'

module CommandKit
  module Arguments
    #
    # Represents a defined argument.
    #
    class Argument < ArgumentValue

      # The argument's name.
      #
      # @return [Symbol]
      attr_reader :name

      # The argument's description.
      #
      # @return [String]
      attr_reader :desc

      #
      # Initializes the argument.
      #
      # @param [Symbol] name
      #   The name of the argument.
      #
      # @param [String, nil] usage
      #   The usage string for the argument. Defaults to the argument's name.
      #
      # @param [Boolean] required
      #   Specifies whether the argument is required or optional.
      #
      # @param [Boolean] repeats
      #   Specifies whether the argument can be repeated multiple times.
      #
      # @param [String] desc
      #   The description for the argument.
      #
      # @yield [(value)]
      #   If a block is given, it will be used to parse the argument's value.
      #   Note: not currently used.
      #
      # @yieldparam [Object, nil] value
      #
      def initialize(name, usage:    name.to_s.upcase,
                           required: true,
                           repeats:  false,
                           desc:     )
        super(
          usage:    usage,
          required: required
        )

        @name    = name
        @repeats = repeats
        @desc    = desc
      end

      #
      # Specifies whether the argument can be repeated repeat times.
      #
      # @return [Boolean]
      #
      def repeats?
        @repeats
      end

      #
      # The usage string for the argument.
      #
      # @return [String]
      #
      def usage
        string = @usage
        string = "#{string} ..." if repeats?
        string = "[#{string}]" if optional?
        string
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
command_kit-0.1.0.rc1 lib/command_kit/arguments/argument.rb
command_kit-0.1.0.pre2 lib/command_kit/arguments/argument.rb