Class: Argy::Option

Inherits:
Parameter show all
Defined in:
lib/argy/option.rb

Instance Attribute Summary collapse

Attributes inherited from Parameter

#default, #desc, #name, #type

Instance Method Summary collapse

Methods inherited from Parameter

#coerce, #required?, #validate

Constructor Details

#initialize(*args, aliases: [], **opts) ⇒ Option

Returns a new instance of Option.



7
8
9
10
# File 'lib/argy/option.rb', line 7

def initialize(*args, aliases: [], **opts)
  super(*args, **opts)
  @aliases = aliases
end

Instance Attribute Details

#aliasesObject (readonly)

Returns the value of attribute aliases.



5
6
7
# File 'lib/argy/option.rb', line 5

def aliases
  @aliases
end

Instance Method Details

#labelObject



12
13
14
15
16
17
18
19
# File 'lib/argy/option.rb', line 12

def label
  case type
  when :boolean
    "--[no-]#{name.to_s.tr("_", "-")}"
  else
    "--#{name.to_s.tr("_", "-")}"
  end
end

#to_option_parserObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/argy/option.rb', line 21

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