Class: BBLib::OptsParser::Option
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from TypeInit
included
Methods included from Effortless
#_attrs, included
Class Method Details
.types ⇒ Object
27
28
29
|
# File 'lib/bblib/cli/option.rb', line 27
def self.types
descendants.flat_map(&:type)
end
|
Instance Method Details
#flag_match?(str, index = 0) ⇒ 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
|
#retrieve(args, parsed) ⇒ Object
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 31
def retrieve(args, parsed)
result = singular? ? nil : []
index = 0
until index >= args.size
begin
unless flag_match?(args[index].to_s, index)
index += 1
next
end
values = split((index, args))
values.each do |value|
valid!(value)
if singular?
result = value
index = args.size
else
result << value
end
end
rescue OptsParserException => e
raise e if raise_errors?
end
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
|
#singular? ⇒ Boolean
59
60
61
|
# File 'lib/bblib/cli/option.rb', line 59
def singular?
singular && !delimiter
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_s ⇒ Object
23
24
25
|
# File 'lib/bblib/cli/option.rb', line 23
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
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
|