Sha256: 821ecc10f798554b302c1a02e4a0d92935a0dc751cdcdb1e6127150da3779c3a

Contents?: true

Size: 1.26 KB

Versions: 4

Compression:

Stored size: 1.26 KB

Contents

require "argy/parameter"

module Argy
  # An option to be parsed from the command line
  class Option < Parameter
    # A list of alternative flags
    # @return [Array<String>]
    attr_reader :aliases

    # Create a new Option
    # @param name [Symbol] name of the parameter
    # @param aliases [Array<String>] a list of alternative flags
    # @param desc [String,nil] description for the parameter
    # @param type [Symbol,#call] type of parameter
    # @param default [Object] default value for the parameter
    # @param required [TrueClass,FalseClass] whether or not the field is required
    def initialize(*args, aliases: [], **opts)
      super(*args, **opts)
      @aliases = aliases
    end

    # The display label for the argument
    # @return [String]
    def label
      case type
      when :boolean
        "--[no-]#{name.to_s.tr("_", "-")}"
      else
        "--#{name.to_s.tr("_", "-")}"
      end
    end

    # @private
    def to_option_parser
      options = []
      options << aliases.join(" ") unless aliases.empty?

      case type
      when :boolean
        options << label
      else
        options << "#{label}=#{name.to_s.upcase}"
        options << "#{label} #{name.to_s.upcase}"
      end

      options << desc if desc
      options
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
argy-0.2.3 lib/argy/option.rb
argy-0.0.0 lib/argy/option.rb
argy-0.2.2 lib/argy/option.rb
argy-0.2.1 lib/argy/option.rb