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 9 def initialize(options = {}) @all = [] @children = [] @options = options @creator = options[:creator] || Class.new(HammerCLI::Apipie::Command) @prefix = options[:prefix] @root = options[:root] || options[:aliased_resource] || options[:referenced_resource] end
Public Instance Methods
adopt(child)
click to toggle source
# File lib/hammer_cli/options/option_family.rb, line 74 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 58 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 68 def child(switches, type, description, opts = {}, &block) child = new_member(switches, type, description, opts, &block) @children << child child end
description()
click to toggle source
# File lib/hammer_cli/options/option_family.rb, line 18 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(/\w+/, types) } desc = 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
formats()
click to toggle source
# File lib/hammer_cli/options/option_family.rb, line 35 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 54 def head @parent end
parent(switches, type, description, opts = {}, &block)
click to toggle source
# File lib/hammer_cli/options/option_family.rb, line 62 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
switch()
click to toggle source
# File lib/hammer_cli/options/option_family.rb, line 41 def switch return if @parent.nil? && @children.empty? return @parent.help_lhs.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 107 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 84 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 102 def main_switch root = @root || @parent.aliased_resource || @parent.referenced_resource || common_root "--#{@prefix}#{root}".tr('_', '-') end
new_member(switches, type, description, opts = {}, &block)
click to toggle source
# File lib/hammer_cli/options/option_family.rb, line 88 def new_member(switches, type, description, opts = {}, &block) opts = opts.merge(@options) opts[:family] = self if opts[:deprecated] handles = [switches].flatten opts[:deprecated] = opts[:deprecated].select do |switch, _msg| handles.include?(switch) end end @creator.instance_eval do option(switches, type, description, opts, &block) end end