Class: NanDoc::Cli::OptionMethods::OptionEnum

Inherits:
Object
  • Object
show all
Includes:
StringMethods
Defined in:
lib/nandoc/cli/option-methods/option-enum.rb

Instance Method Summary collapse

Methods included from StringMethods

#basename_no_extension, #indent, #no_blank_lines, #no_leading_ws, #no_trailing_ws, oxford_comma, #quoted, #reindent, #unindent

Constructor Details

#initialize(&block) ⇒ OptionEnum

Returns a new instance of OptionEnum.



10
11
12
# File 'lib/nandoc/cli/option-methods/option-enum.rb', line 10

def initialize(&block)
  instance_eval(&block)
end

Instance Method Details

#command(cmd) ⇒ Object



13
14
15
# File 'lib/nandoc/cli/option-methods/option-enum.rb', line 13

def command cmd
  @command = cmd
end

#default(str) ⇒ Object



16
17
18
# File 'lib/nandoc/cli/option-methods/option-enum.rb', line 16

def default str
  @default = str
end

#name(name) ⇒ Object



19
20
21
# File 'lib/nandoc/cli/option-methods/option-enum.rb', line 19

def name name
  @name = name
end

#parse(opts) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/nandoc/cli/option-methods/option-enum.rb', line 22

def parse opts
  found = nil
  if opts.key?(@name)
    v = opts[@name]
    re = /\A#{Regexp.escape(v)}/
    founds = @values.grep(re)
    case founds.size
      when 0; invalid(v)
      when 1; found = founds.first
      else found = founds.detect{|f| f==v} or too_many(founds)
    end
  elsif(@default)
    found = @default
  else
    found = nil
  end
  opts[@name] = found if found # normalize short versions
  found
end

#values(*v) ⇒ Object



41
42
43
44
# File 'lib/nandoc/cli/option-methods/option-enum.rb', line 41

def values *v
  v = v.first if v.size==1 && Array === v
  @values = v
end