class HammerCLI::Options::OptionFamily
Constants
- IDS_REGEX
Attributes
children[R]
Public Class Methods
new(options = {})
click to toggle source
# File lib/hammer_cli/options/option_family.rb, line 17 def initialize(options = {}) @all = [] @children = [] @options = options @creator = options[:creator] || self @prefix = options[:prefix] @root = options[:root] || options[:aliased_resource] || options[:referenced_resource] @creator.family_registry.register(self) if @creator != self end
Public Instance Methods
adopt(child)
click to toggle source
# File lib/hammer_cli/options/option_family.rb, line 100 def adopt(child) raise ArgumentError, 'Parent cannot be a child within the same family' if child == @parent raise ArgumentError, 'Child is already in the family' if @children.include?(child) child.family = self @children << child end
all()
click to toggle source
# File lib/hammer_cli/options/option_family.rb, line 82 def all @children + [@parent].compact end
child(switches, type, description, opts = {}, &block)
click to toggle source
# File lib/hammer_cli/options/option_family.rb, line 92 def child(switches, type, description, opts = {}, &block) child = new_member(switches, type, description, opts, &block) return unless child @children << child child end
description()
click to toggle source
# File lib/hammer_cli/options/option_family.rb, line 27 def description types = all.map(&:type).map { |s| s.split('_').last.to_s } .map(&:downcase).join('/') parent_desc = @parent.help[1].gsub(IDS_REGEX) { |w| w.gsub(/\b.+\b/, types) } desc = @options[:description] || parent_desc.strip.empty? ? @options[:description] : parent_desc if @options[:deprecation].class <= String format_deprecation_msg(desc, _('Deprecated: %{deprecated_msg}') % { deprecated_msg: @options[:deprecation] }) elsif @options[:deprecation].class <= Hash full_msg = @options[:deprecation].map do |flag, msg| _('%{flag} is deprecated: %{deprecated_msg}') % { flag: flag, deprecated_msg: msg } end.join(', ') format_deprecation_msg(desc, full_msg) else desc end end
find_option(switch)
click to toggle source
# File lib/hammer_cli/options/option_family.rb, line 116 def find_option(switch) all.find { |m| m.handles?(switch) } end
formats()
click to toggle source
# File lib/hammer_cli/options/option_family.rb, line 59 def formats return [@options[:format].class] if @options[:format] all.map(&:value_formatter).map(&:class).uniq end
head()
click to toggle source
# File lib/hammer_cli/options/option_family.rb, line 78 def head @parent end
help()
click to toggle source
# File lib/hammer_cli/options/option_family.rb, line 44 def help [help_lhs, help_rhs] end
help_lhs()
click to toggle source
# File lib/hammer_cli/options/option_family.rb, line 48 def help_lhs return @parent&.help_lhs if @children.empty? types = all.map(&:value_formatter).map { |f| f.completion_type[:type].to_s.upcase } switch + ' ' + types.uniq.join('/') end
help_rhs()
click to toggle source
# File lib/hammer_cli/options/option_family.rb, line 55 def help_rhs description || @parent.help[1] end
option(*args)
click to toggle source
# File lib/hammer_cli/options/option_family.rb, line 112 def option(*args) HammerCLI::Apipie::OptionDefinition.new(*args) end
parent(switches, type, description, opts = {}, &block)
click to toggle source
# File lib/hammer_cli/options/option_family.rb, line 86 def parent(switches, type, description, opts = {}, &block) raise StandardError, 'Option family can have only one parent' if @parent @parent = new_member(switches, type, description, opts, &block) end
root()
click to toggle source
# File lib/hammer_cli/options/option_family.rb, line 108 def root @root || @parent&.aliased_resource || @parent&.referenced_resource || common_root end
switch()
click to toggle source
# File lib/hammer_cli/options/option_family.rb, line 65 def switch return if @parent.nil? && @children.empty? return @parent.switches.join(', ').strip if @children.empty? switch_start = main_switch.each_char .zip(*all.map(&:switches).flatten.map(&:each_char)) .select { |a, b| a == b }.transpose.first.join suffixes = all.map do |m| m.switches.map { |s| s.gsub(switch_start, '') } end.flatten.reject(&:empty?).sort { |x, y| x.size <=> y.size } "#{switch_start}[#{suffixes.join('|')}]" end
Private Instance Methods
common_root()
click to toggle source
# File lib/hammer_cli/options/option_family.rb, line 144 def common_root switches = all.map(&:switches).flatten shortest = switches.min_by(&:length) max_len = shortest.length max_len.downto(0) do |curr_len| 0.upto(max_len - curr_len) do |start| root = shortest[start, curr_len] return root[2..-1].chomp('-') if switches.all? { |switch| switch.include?(root) } end end end
format_deprecation_msg(option_desc, deprecation_msg)
click to toggle source
# File lib/hammer_cli/options/option_family.rb, line 122 def format_deprecation_msg(option_desc, deprecation_msg) "#{option_desc} (#{deprecation_msg})" end
main_switch()
click to toggle source
# File lib/hammer_cli/options/option_family.rb, line 140 def main_switch "--#{@prefix}#{root}".tr('_', '-') end
new_member(switches, type, description, opts = {}, &block)
click to toggle source
# File lib/hammer_cli/options/option_family.rb, line 126 def new_member(switches, type, description, opts = {}, &block) opts = opts.merge(@options) opts[:family] = self if opts[:deprecated] handles = Array(switches) opts[:deprecated] = opts[:deprecated].select do |switch, _msg| handles.include?(switch) end end @creator.instance_eval do option(switches, type, description, opts, &block) unless Array(switches).any? { |s| find_option(s) } end end