Class: LilUtils::CLI::Option

Inherits:
Object
  • Object
show all
Defined in:
lib/lilutils/cli/cli.rb

Overview

Models an arbitrary option that user has choice to choose. Every Option has a name and a key. The name of an Option is its given name capitalized and its key is first character in its name in lower case. For example, for an option constructed with the string “separate”, the actual name is “Separate” and key is ‘s’. Provides behavior for such an Option as far as its display, equality, validity etc. is concerned.

Direct Known Subclasses

NumberedOption, SingleLetterOption

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = self.class.simple_name) ⇒ Option

constructs this option with a name. The first character of this is regarded its key, e.g. “Yes” and ‘y’. Unless an option has a proper name (e.g. “Mozart”), do not use the default value of the parameter



25
26
27
28
# File 'lib/lilutils/cli/cli.rb', line 25

def initialize(name=self.class.simple_name)
  @name = name.capitalize
  @name.freeze
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



22
23
24
# File 'lib/lilutils/cli/cli.rb', line 22

def name
  @name
end

Instance Method Details

#==(other) ⇒ Object



30
31
32
# File 'lib/lilutils/cli/cli.rb', line 30

def ==(other)
  (other.is_a? Option) && (@name == other.name)
end

#as_defaultObject



38
39
40
# File 'lib/lilutils/cli/cli.rb', line 38

def as_default
  "*#{@name} (#{key})*"
end

#as_non_defaultObject



42
43
44
# File 'lib/lilutils/cli/cli.rb', line 42

def as_non_default
  "#{@name} (#{key})"
end

#keyObject



50
51
52
# File 'lib/lilutils/cli/cli.rb', line 50

def key
  @name[0].downcase
end

#to_sObject



46
47
48
# File 'lib/lilutils/cli/cli.rb', line 46

def to_s
  @name
end

#valid_response(r) ⇒ Object



34
35
36
# File 'lib/lilutils/cli/cli.rb', line 34

def valid_response(r)
  key == r
end