Module: Rubycli::TypeUtils
- Included in:
- ArgumentParser, Documentation::MetadataParser, HelpRenderer
- Defined in:
- lib/rubycli/type_utils.rb
Class Method Summary collapse
- .analyze_placeholder(value_placeholder) ⇒ Object
- .boolean_string?(value) ⇒ Boolean
- .boolean_type?(type) ⇒ Boolean
- .convert_boolean(value) ⇒ Object
- .default_placeholder_for(keyword) ⇒ Object
- .determine_requires_value(value_placeholder:, types:, boolean_flag:, optional_value:) ⇒ Object
- .infer_types_from_placeholder(types, placeholder_info, include_optional_boolean: true) ⇒ Object
- .nil_type?(type) ⇒ Boolean
- .normalize_long_option(option) ⇒ Object
- .normalize_short_option(option) ⇒ Object
- .normalize_type_list(types) ⇒ Object
- .normalize_type_token(token) ⇒ Object
- .parse_list(value) ⇒ Object
Class Method Details
.analyze_placeholder(value_placeholder) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/rubycli/type_utils.rb', line 39 def analyze_placeholder(value_placeholder) return { optional: false, list: false, base: nil } unless value_placeholder trimmed = value_placeholder.strip optional = trimmed.start_with?('[') && trimmed.end_with?(']') core = optional ? trimmed[1..-2].strip : trimmed.dup sanitized = core.gsub(/\[|\]/, '') sanitized = sanitized.gsub('...', '') list = sanitized.include?(',') || core.include?('...') || core.include?('[,') token = sanitized.split(',').first.to_s.strip token = token.gsub(/[^A-Za-z0-9_]/, '') token = nil if token.empty? { optional: optional, list: list, base: token } end |
.boolean_string?(value) ⇒ Boolean
151 152 153 154 155 |
# File 'lib/rubycli/type_utils.rb', line 151 def boolean_string?(value) return false if value.nil? %w[true false t f yes no y n 1 0].include?(value.to_s.strip.downcase) end |
.boolean_type?(type) ⇒ Boolean
11 12 13 |
# File 'lib/rubycli/type_utils.rb', line 11 def boolean_type?(type) %w[Boolean TrueClass FalseClass].include?(type) end |
.convert_boolean(value) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/rubycli/type_utils.rb', line 137 def convert_boolean(value) return value if [true, false].include?(value) str = value.to_s.strip.downcase case str when 'true', 't', 'yes', 'y', '1' true when 'false', 'f', 'no', 'n', '0' false else raise ArgumentError, "Cannot convert to boolean: #{value}" end end |
.default_placeholder_for(keyword) ⇒ Object
126 127 128 |
# File 'lib/rubycli/type_utils.rb', line 126 def default_placeholder_for(keyword) keyword.to_s.upcase end |
.determine_requires_value(value_placeholder:, types:, boolean_flag:, optional_value:) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/rubycli/type_utils.rb', line 98 def determine_requires_value(value_placeholder:, types:, boolean_flag:, optional_value:) return nil if optional_value return false if boolean_flag value_present = !value_placeholder.nil? non_boolean_types = types.reject { |type| boolean_type?(type) || nil_type?(type) } if value_present true elsif non_boolean_types.any? true else false end end |
.infer_types_from_placeholder(types, placeholder_info, include_optional_boolean: true) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/rubycli/type_utils.rb', line 55 def infer_types_from_placeholder(types, placeholder_info, include_optional_boolean: true) working = types.dup if working.empty? if placeholder_info[:optional] inferred = if placeholder_info[:list] include_optional_boolean ? ['Boolean', 'String[]'] : ['String[]'] else include_optional_boolean ? %w[Boolean String] : ['String'] end working.concat(inferred) elsif placeholder_info[:list] working << 'String[]' elsif placeholder_info[:base] working << 'String' end elsif placeholder_info[:optional] && include_optional_boolean working.unshift('Boolean') unless working.any? { |type| boolean_type?(type) } end if placeholder_info[:list] array_type_present = false working = working.map do |type| next type if type.nil? || type.empty? if (boolean_type?(type) && placeholder_info[:optional]) || nil_type?(type) type elsif type.end_with?('[]') || type.start_with?('Array<') || type.casecmp('Array').zero? array_type_present = true type else array_type_present = true "#{type}[]" end end working << 'String[]' unless array_type_present end working.compact.uniq end |
.nil_type?(type) ⇒ Boolean
7 8 9 |
# File 'lib/rubycli/type_utils.rb', line 7 def nil_type?(type) %w[nil NilClass].include?(type) end |
.normalize_long_option(option) ⇒ Object
114 115 116 117 118 |
# File 'lib/rubycli/type_utils.rb', line 114 def normalize_long_option(option) return nil unless option option.start_with?('--') ? option : "--#{option.delete_prefix('-')}" end |
.normalize_short_option(option) ⇒ Object
120 121 122 123 124 |
# File 'lib/rubycli/type_utils.rb', line 120 def normalize_short_option(option) return nil unless option option.start_with?('-') ? option : "-#{option}" end |
.normalize_type_list(types) ⇒ Object
15 16 17 |
# File 'lib/rubycli/type_utils.rb', line 15 def normalize_type_list(types) Array(types).compact.flat_map { |type| normalize_type_token(type) }.map(&:strip).reject(&:empty?).uniq end |
.normalize_type_token(token) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/rubycli/type_utils.rb', line 19 def normalize_type_token(token) trimmed = token.to_s.strip trimmed = trimmed.delete_prefix('@') return '' if trimmed.empty? trimmed = trimmed[1..-2].strip if trimmed.start_with?('(') && trimmed.end_with?(')') trimmed = trimmed.sub(/\Atype\s*:\s*/i, '').strip return '' if trimmed.empty? if trimmed.include?('<') && trimmed.end_with?('>') trimmed elsif trimmed.end_with?('[]') base = trimmed[0..-3] base = base.capitalize if base == base.downcase "#{base}[]" else trimmed end end |
.parse_list(value) ⇒ Object
130 131 132 133 134 135 |
# File 'lib/rubycli/type_utils.rb', line 130 def parse_list(value) return [] if value.nil? return value if value.is_a?(Array) value.to_s.split(',').map(&:strip).reject(&:empty?) end |