Class: Cl::Parser::Format

Inherits:
Struct
  • Object
show all
Defined in:
lib/cl/parser/format.rb

Constant Summary collapse

NAME =
/^(--(?:\[no-\])?)([^= ]+)/

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optObject

Returns the value of attribute opt

Returns:

  • (Object)

    the current value of opt



3
4
5
# File 'lib/cl/parser/format.rb', line 3

def opt
  @opt
end

Instance Method Details

#aliasesObject



29
30
31
# File 'lib/cl/parser/format.rb', line 29

def aliases
  opt.aliases.map { |name| "--#{name} #{ name.upcase unless opt.flag?}".strip }
end

#collect(strs, mod) ⇒ Object



33
34
35
36
# File 'lib/cl/parser/format.rb', line 33

def collect(strs, mod)
  strs = strs + strs.map { |str| send(mod, str) }
  strs.flatten.uniq
end

#dashed(str) ⇒ Object



42
43
44
# File 'lib/cl/parser/format.rb', line 42

def dashed(str)
  str =~ NAME && str.sub("#{$1}#{$2}", "#{$1}#{$2.tr('_', '-')}") || str
end

#flag?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/cl/parser/format.rb', line 58

def flag?
  opt.flag? && !opt.help?
end

#long(str) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/cl/parser/format.rb', line 11

def long(str)
  strs = [unnegate(str)]
  strs = strs.map { |str| negated(str) }.flatten if flag?
  strs = collect(strs, :dashed)
  strs = collect(strs, :underscored)
  strs = collect(strs, :valued) if flag? && Cl.flag_values
  strs.uniq
end

#long?(str) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/cl/parser/format.rb', line 54

def long?(str)
  str.start_with?('--')
end

#negated(str) ⇒ Object



38
39
40
# File 'lib/cl/parser/format.rb', line 38

def negated(str)
  str.dup.insert(2, '[no-]')
end

#short(str) ⇒ Object



20
21
22
23
# File 'lib/cl/parser/format.rb', line 20

def short(str)
  str = "#{str} #{opt.name.upcase}" unless opt.flag? || str.include?(' ')
  str
end

#strsObject



6
7
8
9
# File 'lib/cl/parser/format.rb', line 6

def strs
  strs = opt.strs + aliases
  strs.map { |str| long?(str) ? long(str) : short(str) }.flatten
end

#underscored(str) ⇒ Object



46
47
48
# File 'lib/cl/parser/format.rb', line 46

def underscored(str)
  str =~ NAME && str.sub("#{$1}#{$2}", "#{$1}#{$2.tr('-', '_')}") || str
end

#unnegate(str) ⇒ Object



25
26
27
# File 'lib/cl/parser/format.rb', line 25

def unnegate(str)
  str.sub('--[no-]', '--')
end

#valued(str) ⇒ Object



50
51
52
# File 'lib/cl/parser/format.rb', line 50

def valued(str)
  "#{str} [true|false|yes|no]"
end