Class: Foobara::CommandConnectors::ShCliConnector::InputsParser::Option
- Inherits:
-
Object
- Object
- Foobara::CommandConnectors::ShCliConnector::InputsParser::Option
- Defined in:
- foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/inputs_parser/option.rb,
foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/inputs_parser/option/flag.rb,
foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/inputs_parser/option/model.rb,
foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/inputs_parser/option/on_flag.rb,
foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/inputs_parser/option/off_flag.rb,
foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/inputs_parser/option/attributes.rb
Direct Known Subclasses
Defined Under Namespace
Classes: Attributes, Flag, Model, OffFlag, OnFlag
Instance Attribute Summary collapse
-
#always_prefix_inputs ⇒ Object
Returns the value of attribute always_prefix_inputs.
-
#attribute_name ⇒ Object
Returns the value of attribute attribute_name.
-
#attribute_type ⇒ Object
Returns the value of attribute attribute_type.
-
#default ⇒ Object
Returns the value of attribute default.
-
#is_required ⇒ Object
Returns the value of attribute is_required.
-
#prefix ⇒ Object
Returns the value of attribute prefix.
Class Method Summary collapse
Instance Method Summary collapse
- #_argument_text(prefixed_name) ⇒ Object
- #_long_option_name(prefixed_name) ⇒ Object
- #_non_colliding_path(full_paths) ⇒ Object
- #_prefixed_name(full_paths) ⇒ Object
- #_truncated_path(path, size) ⇒ Object
- #_truncated_paths(paths, size) ⇒ Object
- #array? ⇒ Boolean
- #description ⇒ Object
- #full_path ⇒ Object
- #has_default? ⇒ Boolean
-
#initialize(attribute_name:, attribute_type:, prefix:, is_required:, default:, always_prefix_inputs:) ⇒ Option
constructor
A new instance of Option.
- #required? ⇒ Boolean
- #supports_short_option? ⇒ Boolean
- #to_args(short_options_used, full_paths) ⇒ Object
Constructor Details
#initialize(attribute_name:, attribute_type:, prefix:, is_required:, default:, always_prefix_inputs:) ⇒ Option
Returns a new instance of Option.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/inputs_parser/option.rb', line 41 def initialize( attribute_name:, attribute_type:, prefix:, is_required:, default:, always_prefix_inputs: ) self.attribute_type = attribute_type self.attribute_name = attribute_name self.prefix = prefix self.is_required = is_required self.default = default self.always_prefix_inputs = always_prefix_inputs # TODO: support this # args << attributes_type.declaration_data[:one_of] if attributes_type.declaration_data.key?(:one_of) end |
Instance Attribute Details
#always_prefix_inputs ⇒ Object
Returns the value of attribute always_prefix_inputs.
39 40 41 |
# File 'foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/inputs_parser/option.rb', line 39 def always_prefix_inputs @always_prefix_inputs end |
#attribute_name ⇒ Object
Returns the value of attribute attribute_name.
39 40 41 |
# File 'foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/inputs_parser/option.rb', line 39 def attribute_name @attribute_name end |
#attribute_type ⇒ Object
Returns the value of attribute attribute_type.
39 40 41 |
# File 'foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/inputs_parser/option.rb', line 39 def attribute_type @attribute_type end |
#default ⇒ Object
Returns the value of attribute default.
39 40 41 |
# File 'foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/inputs_parser/option.rb', line 39 def default @default end |
#is_required ⇒ Object
Returns the value of attribute is_required.
39 40 41 |
# File 'foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/inputs_parser/option.rb', line 39 def is_required @is_required end |
#prefix ⇒ Object
Returns the value of attribute prefix.
39 40 41 |
# File 'foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/inputs_parser/option.rb', line 39 def prefix @prefix end |
Class Method Details
.attribute_to_options(attribute_name, attribute_type:, prefix:, is_required:, default:, always_prefix_inputs:) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/inputs_parser/option.rb', line 7 def ( attribute_name, attribute_type:, prefix:, is_required:, default:, always_prefix_inputs: ) [Model, Attributes, Flag].each do |klass| if klass.applicable?(attribute_type) return klass.( attribute_name, attribute_type:, prefix:, is_required:, default:, always_prefix_inputs: ) end end new( attribute_name:, attribute_type:, prefix:, is_required:, default:, always_prefix_inputs: ) end |
Instance Method Details
#_argument_text(prefixed_name) ⇒ Object
133 134 135 |
# File 'foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/inputs_parser/option.rb', line 133 def _argument_text(prefixed_name) Util.constantify(prefixed_name) end |
#_long_option_name(prefixed_name) ⇒ Object
129 130 131 |
# File 'foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/inputs_parser/option.rb', line 129 def _long_option_name(prefixed_name) Util.kebab_case(prefixed_name) end |
#_non_colliding_path(full_paths) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/inputs_parser/option.rb', line 60 def _non_colliding_path(full_paths) 1.upto(full_path.length - 1) do |size| candidate_path = _truncated_path(full_path, size) match_count = _truncated_paths(full_paths, size).select { |path| path == candidate_path }.size if match_count == 1 return candidate_path elsif match_count == 0 # :nocov: raise "Not expecting to reach here" # :nocov: end end full_path end |
#_prefixed_name(full_paths) ⇒ Object
121 122 123 124 125 126 127 |
# File 'foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/inputs_parser/option.rb', line 121 def _prefixed_name(full_paths) if always_prefix_inputs full_path.join("_") else _non_colliding_path(full_paths).join("_") end end |
#_truncated_path(path, size) ⇒ Object
83 84 85 |
# File 'foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/inputs_parser/option.rb', line 83 def _truncated_path(path, size) path[(-size)..] end |
#_truncated_paths(paths, size) ⇒ Object
77 78 79 80 81 |
# File 'foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/inputs_parser/option.rb', line 77 def _truncated_paths(paths, size) paths.map do |path| _truncated_path(path, size) end end |
#array? ⇒ Boolean
145 146 147 |
# File 'foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/inputs_parser/option.rb', line 145 def array? attribute_type.extends?(BuiltinTypes[:array]) end |
#description ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/inputs_parser/option.rb', line 153 def description desc = [] attributes_description = attribute_type.description if attributes_description && !BuiltinTypes.builtin?(attribute_type) desc << attributes_description end if required? desc << "Required" end one_of = attribute_type.declaration_data[:one_of] if one_of && !one_of.empty? desc << "One of: #{one_of.join(", ")}" end if has_default? desc << "Default: #{default.inspect}" end unless desc.empty? desc.map!.with_index do |d, index| break if index == desc.size - 1 if d =~ /[\.!?\]\}:]$/ d else "#{d}." end end desc.join(" ") end end |
#full_path ⇒ Object
137 138 139 |
# File 'foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/inputs_parser/option.rb', line 137 def full_path [*prefix, attribute_name] end |
#has_default? ⇒ Boolean
149 150 151 |
# File 'foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/inputs_parser/option.rb', line 149 def has_default? !default.nil? end |
#required? ⇒ Boolean
141 142 143 |
# File 'foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/inputs_parser/option.rb', line 141 def required? is_required end |
#supports_short_option? ⇒ Boolean
117 118 119 |
# File 'foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/inputs_parser/option.rb', line 117 def supports_short_option? true end |
#to_args(short_options_used, full_paths) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'foobara-sh-cli-connector-0.0.16/src/sh_cli_connector/inputs_parser/option.rb', line 87 def to_args(, full_paths) prefixed_name = _prefixed_name(full_paths) long_option_name = _long_option_name(prefixed_name) argument_text = _argument_text(prefixed_name) args = if argument_text ["--#{long_option_name} #{argument_text}"] else ["--#{long_option_name}"] end if supports_short_option? short_option = attribute_name[0] # TODO: we should prioritize required options to get the short name in case of collision? unless .include?(short_option) << short_option args << if argument_text "-#{short_option} #{argument_text}" else "-#{short_option}" end end end args << description if description args end |