Module: Clin::CommandMixin::Options::ClassMethods

Defined in:
lib/clin/command_mixin/options.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#general_optionsObject

Returns the value of attribute general_options.



23
24
25
# File 'lib/clin/command_mixin/options.rb', line 23

def general_options
  @general_options
end

#specific_optionsObject

Returns the value of attribute specific_options.



22
23
24
# File 'lib/clin/command_mixin/options.rb', line 22

def specific_options
  @specific_options
end

Instance Method Details

#add_option(option) ⇒ Object

Add a new option.



70
71
72
73
# File 'lib/clin/command_mixin/options.rb', line 70

def add_option(option)
  # Need to use += instead of << otherwise the parent class will also be changed

  @specific_options << option
end

#auto_option(name, usage, &block) ⇒ Object



64
65
66
# File 'lib/clin/command_mixin/options.rb', line 64

def auto_option(name, usage, &block)
  add_option Clin::Option.parse(name, usage, &block)
end

#execute_general_options(options) ⇒ Object

Call #execute on each of the general options. This is called during the command initialization e.g. A verbose general option execute would be: “‘ def execute(params)

MyApp.verbose = true if params[:verbose]

end “‘



113
114
115
116
117
# File 'lib/clin/command_mixin/options.rb', line 113

def execute_general_options(options)
  general_options.each do |_cls, gopts|
    gopts.execute(options)
  end
end

#find_option(value) ⇒ Object



124
125
126
# File 'lib/clin/command_mixin/options.rb', line 124

def find_option(value)
  find_option_by(name: value)
end

#find_option_by(hash) ⇒ Object



128
129
130
131
# File 'lib/clin/command_mixin/options.rb', line 128

def find_option_by(hash)
  key, value = hash.first
  options.find { |x| x.send(key) == value }
end

#flag_option(name, description, **config, &block) ⇒ Object

For an option that does not have an argument Same as .option except it will default argument to false “‘

option :verbose, 'Use verbose' #=> -v --verbose will be added to the option of this command

“‘



47
48
49
# File 'lib/clin/command_mixin/options.rb', line 47

def flag_option(name, description, **config, &block)
  add_option Clin::Option.new(name, description, **config.merge(argument: false), &block)
end

#general_option(option_cls, config = {}) ⇒ Object

Add a general option



78
79
80
81
# File 'lib/clin/command_mixin/options.rb', line 78

def general_option(option_cls, config = {})
  option_cls = option_cls.constantize if option_cls.is_a? String
  @general_options[option_cls] = option_cls.new(config)
end

#list_flag_option(name, description, **config) ⇒ Object

Add a list options that don’t take arguments Same as .list_option but set argument to false



60
61
62
# File 'lib/clin/command_mixin/options.rb', line 60

def list_flag_option(name, description, **config)
  add_option Clin::OptionList.new(name, description, **config.merge(argument: false))
end

#list_option(name, description, **config) ⇒ Object

Add a list option.



53
54
55
# File 'lib/clin/command_mixin/options.rb', line 53

def list_option(name, description, **config)
  add_option Clin::OptionList.new(name, description, **config)
end

#option(name, description, **config, &block) ⇒ Object

Add an option. Helper method that just create a new Clin::Option with the argument then call add_option “‘

option :show, 'Show some message'
# => -s --show              SHOW Show some message
option :require, 'Require a library', short: false, optional: true, argument: 'LIBRARY'
# => --require [LIBRARY]    Require a library
option :help, 'Show the help', argument: false do
  puts opts
  exit
end
# => -h --help              Show the help

“‘



38
39
40
# File 'lib/clin/command_mixin/options.rb', line 38

def option(name, description, **config, &block)
  add_option Clin::Option.new(name, description, **config, &block)
end

#option_defaultsHash

To be called inside OptionParser block Extract the option in the command line using the OptionParser and map it to the out map.



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/clin/command_mixin/options.rb', line 93

def option_defaults
  out = {}
  @specific_options.each do |option|
    option.load_default(out)
  end

  @general_options.each do |_cls, option|
    out.merge! option.class.option_defaults
  end
  out
end

#option_helpObject



133
134
135
136
137
138
139
140
141
142
143
# File 'lib/clin/command_mixin/options.rb', line 133

def option_help
  Clin::Text.new do |t|
    options.each do |option|
      t.line option.banner
    end

    general_options.each do |cls, _|
      t.text cls.option_help
    end
  end
end

#optionsObject

Return all options



120
121
122
# File 'lib/clin/command_mixin/options.rb', line 120

def options
  specific_options + general_options.keys.map(&:options).flatten
end

#remove_general_option(option_cls) ⇒ Object

Remove a general option Might be useful if a parent added the option but is not needed in this child.



85
86
87
88
# File 'lib/clin/command_mixin/options.rb', line 85

def remove_general_option(option_cls)
  option_cls = option_cls.constantize if option_cls.is_a? String
  @general_options.delete(option_cls)
end