Class: OptParseValidator::OptSmartList

Inherits:
OptArray show all
Defined in:
lib/opt_parse_validator/opts/smart_list.rb

Overview

Implementation of the SmartList Option Such option allow users to supply a list like

  • name1

  • name1,name2,name3

  • /tmp/names.txt

Instance Attribute Summary

Attributes inherited from OptBase

#attrs, #option, #required

Instance Method Summary collapse

Methods inherited from OptArray

#normalize, #separator

Methods inherited from OptBase

#advanced?, #alias?, #choices, #default, #help_message_for_default, #help_messages, #initialize, #normalize, #required?, #required_unless, #to_long, #to_s, #to_sym, #value_if_empty

Constructor Details

This class inherits a constructor from OptParseValidator::OptBase

Instance Method Details

#append_help_messagesVoid

Returns:

  • (Void)


11
12
13
14
15
16
17
18
# File 'lib/opt_parse_validator/opts/smart_list.rb', line 11

def append_help_messages
  super
  # removes the help message from OptArray about the separator as useless here
  # can't use option as it's an attr_reader only
  @option -= ["Separator to use between the values: '#{separator}'"]

  option << "Examples: 'a1', '#{%w[a1 a2 a3].join(separator)}', '/tmp/a.txt'"
end

#validate(value) ⇒ Array<String>

Parameters:

  • value (String)

Returns:

  • (Array<String>)


23
24
25
26
27
28
# File 'lib/opt_parse_validator/opts/smart_list.rb', line 23

def validate(value)
  # Might be a better way to do this especially with a big file
  File.open(value).map(&:chomp)
rescue Errno::ENOENT
  super(value)
end