Class: BBLib::OptsParser::Option

Inherits:
Object
  • Object
show all
Includes:
Effortless, TypeInit
Defined in:
lib/bblib/cli/option.rb

Direct Known Subclasses

BasicOption, Command, Toggle

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TypeInit

included

Methods included from Effortless

#_attrs, included

Class Method Details

.typesObject



26
27
28
# File 'lib/bblib/cli/option.rb', line 26

def self.types
  descendants.flat_map(&:type)
end

Instance Method Details

#flag_match?(str, index = 0) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/bblib/cli/option.rb', line 63

def flag_match?(str, index = 0)
  text_match = if flags.empty? && position
    true
  elsif argument_delimiter == ' '
    if !str.include?(' ') && str =~ /^\-[^\-]/
      flags.any? do |flag|
        flag =~ /^\-[\w\d]/ && str.include?(flag[1])
      end
    else
      flags.include?(str)
    end
  else
    flags.any? do |flag|
      flag.start_with?("#{str}#{argument_delimiter}")
    end
  end
  return text_match unless text_match && position
  position === index
end

#multi_value?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/bblib/cli/option.rb', line 59

def multi_value?
  !singular || delimiter
end

#retrieve(args, parsed) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/bblib/cli/option.rb', line 30

def retrieve(args, parsed)
  result = multi_value? ? [] : nil
  index = 0
  until index >= args.size
    begin
      if args[index].nil? || !flag_match?(args[index].to_s, index)
        index += 1
        next
      end
      values = split(extract(index, args))
      values.each do |value|
        valid!(value)
        if multi_value?
          result << value
        else
          result = value
        end
        index = args.size if singular?
      end
    rescue OptsParserException => e
      raise e if raise_errors?
    end
    index += 1
  end
  raise MissingArgumentException, "A required argument is missing: #{name}" if required? && result.nil?
  result = processor.call(result) if !result.nil? && processor
  process_result(result.nil? ? default : result, args, parsed)
end

#split(value) ⇒ Object



94
95
96
97
# File 'lib/bblib/cli/option.rb', line 94

def split(value)
  return [value] unless delimiter
  value.msplit(delimiter)
end

#to_sObject



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

def to_s
  (flags.sort_by(&:size).join(', ') + " #{placeholder}").strip.ljust(40, ' ') + "\t#{description}"
end

#valid!(value) ⇒ Object



90
91
92
# File 'lib/bblib/cli/option.rb', line 90

def valid!(value)
  raise InvalidArgumentException, "Invalid argument for #{name}" unless valid?(value)
end

#valid?(value) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
86
87
88
# File 'lib/bblib/cli/option.rb', line 83

def valid?(value)
  return true if validators.empty?
  validators.all? do |validator|
    validator.call(value)
  end
end