class OptionBinder
Constants
- VERSION
- BinaryInteger
- HexadecimalInteger
- OctalInteger
- ShellWords
NOTE:
ShellWords
defined in OptionParser does not handle missing argument, therefore it is redefined to work properly on switches with optional argument
Attributes
parser[R]
target[R]
Public Class Methods
new(parser: nil, target: nil, bind: nil) { |self| ... }
click to toggle source
# File lib/optbind.rb, line 9 def initialize(parser: nil, target: nil, bind: nil) target, bind = TOPLEVEL_BINDING, :to_local_variables if target == nil && bind == nil @parser = resolve_parser(parser) @target, @reader, @writer = target, *resolve_binding(target, bind) yield self if block_given? end
Public Instance Methods
argument(*opts, &handler)
click to toggle source
# File lib/optbind.rb, line 94 def argument(*opts, &handler) opts, handler, bound, variable, default = *several_variants(*opts, &handler) o, h = *loot!(opts, &handler) (@argument_parser ||= OptionParser.new).on(*(o + ["--#{(@argument_definitions || []).size}"])) do |r| raise OptionParser::InvalidArgument if opts.include?(:REQUIRED) && (r.nil? || r.respond_to?(:empty?) && r.empty?) handle! h, r, bound, variable, default end (@argument_definitions ||= []) << { opts: opts, handler: handler, bound: bound, variable: variable } (@bound_variables_with_defaults ||= {})[variable] = default if bound self end
Also aliased as: arg
assigned?(v)
click to toggle source
# File lib/optbind.rb, line 134 def assigned?(v) return nil unless bound? v (@assigned_variables_with_values || {}).key? v.to_sym end
assigned_variables()
click to toggle source
# File lib/optbind.rb, line 119 def assigned_variables return {} unless @assigned_variables_with_values @assigned_variables_with_values.dup end
bound?(v)
click to toggle source
# File lib/optbind.rb, line 130 def bound?(v) (@bound_variables_with_defaults || {}).key? v.to_sym end
bound_defaults()
click to toggle source
# File lib/optbind.rb, line 110 def bound_defaults @bound_variables_with_defaults ? @bound_variables_with_defaults.dup : {} end
bound_variables()
click to toggle source
# File lib/optbind.rb, line 114 def bound_variables return {} unless @bound_variables_with_defaults Hash[@bound_variables_with_defaults.keys.map { |v| [v, @reader.call(v)] }] end
default?(v)
click to toggle source
# File lib/optbind.rb, line 124 def default?(v) v = v.to_sym return nil unless bound? v (@bound_variables_with_defaults[v] || {}) == @reader.call(v) end
option(*opts, &handler)
click to toggle source
# File lib/optbind.rb, line 78 def option(*opts, &handler) opts, handler, bound, variable, default = *several_variants(*opts, &handler) o, h = *loot!(opts, &handler) @parser.on(*o) do |r| raise OptionParser::InvalidArgument if opts.include?(:REQUIRED) && (r.nil? || r.respond_to?(:empty?) && r.empty?) handle! h, r, bound, variable, default end (@option_definitions ||= []) << { opts: opts, handler: handler, bound: bound, variable: variable } (@bound_variables_with_defaults ||= {})[variable] = default if bound self end
Also aliased as: opt
order(*argv, &blk)
click to toggle source
# File lib/optbind/mode.rb, line 4 def order(*argv, &blk) order! argv.dup.flatten, &blk end
order!(argv = parser.default_argv, &blk)
click to toggle source
# File lib/optbind/mode.rb, line 8 def order!(argv = parser.default_argv, &blk) parse_args! @parser.order! argv, &blk end
parse(*argv)
click to toggle source
# File lib/optbind.rb, line 51 def parse(*argv) parse! argv.dup.flatten end
parse!(argv = parser.default_argv)
click to toggle source
# File lib/optbind.rb, line 55 def parse!(argv = parser.default_argv) parse_args! @parser.parse! argv end
permute(*argv)
click to toggle source
# File lib/optbind/mode.rb, line 12 def permute(*argv) permute! argv.dup.flatten end
permute!(argv = parser.default_argv)
click to toggle source
# File lib/optbind/mode.rb, line 16 def permute!(argv = parser.default_argv) parse_args! @parser.permute! argv end
usage(*args)
click to toggle source
# File lib/optbind.rb, line 63 def usage(*args) line = (args * ' ') << "\n" if @parser.banner =~ /\Ausage:.+\n\n/i @parser.banner = "usage: #{program} #{line}" @parser.separator "\n" else @parser.banner += " or: #{program} #{line}" end self end
Also aliased as: use
Private Instance Methods
handle!(handler, raw, bound, variable, default)
click to toggle source
# File lib/optbind.rb, line 256 def handle!(handler, raw, bound, variable, default) (handler || -> (r) { r }).call(raw == nil ? default : raw).tap do |x| return x unless bound @writer.call variable, x (@assigned_variables_with_values ||= {})[variable] = x end end
loot!(opts, &handler)
click to toggle source
# File lib/optbind.rb, line 246 def loot!(opts, &handler) return opts, handler unless opts.include? :MULTIPLE o = opts.dup t = o.delete o.find { |a| a != Array && a.is_a?(Module) } o << Array unless o.include? Array return o, handler if t == nil || t == String require 'optbind/handler' return o, -> (a) { (handler || -> (r) { r }).call listed_as(t).call a } end
parse_args!(argv)
click to toggle source
# File lib/optbind.rb, line 227 def parse_args!(argv) return argv unless @argument_parser k = @argument_definitions.find_index { |a| a[:opts].include? :MULTIPLE } p = k ? argv[0...k].map { |r| [r] } << argv[k..-1] : argv.map { |r| [r] } p = (p.empty? ? p << [] : p).each_with_index.map do |r, i| a = @argument_definitions[i] raise TooManyArguments unless a raise MissingArguments if a[:opts].include?(:REQUIRED) && r.empty? "--#{i}=#{r * ','}" if a[:opts].include?(:REQUIRED) || !(r.empty? || r.find(&:empty?)) end @argument_parser.order! p argv.shift argv.size - p.size argv rescue OptionParser::InvalidArgument raise $!.tap { |e| e.args[0] = e.args[0].sub(/\A--\d+=/, '') } end
resolve_binding(target, bind)
click to toggle source
# File lib/optbind.rb, line 16 def resolve_binding(target, bind) case bind when :to_local_variables raise ArgumentError unless target.is_a? Binding return -> (v) { target.local_variable_get v }, -> (v, x) { target.local_variable_set v, x } when :to_instance_variables return -> (v) { target.instance_variable_get "@#{v}" }, -> (v, x) { target.instance_variable_set "@#{v}", x } when :to_class_variables return -> (v) { target.class_variable_get "@@#{v}" }, -> (v, x) { target.class_variable_set "@@#{v}", x } else return -> (v) { target[v] }, -> (v, x) { target[v] = x } if target.respond_to? :[] return -> (v) { target.public_send v }, -> (v, x) { target.public_send "#{v}=", x } end end
resolve_parser(parser = nil)
click to toggle source
Calls superclass method
# File lib/optbind.rb, line 31 def resolve_parser(parser = nil) parser ||= OptionParser.new do |p| p.define_singleton_method(:banner) { (b = super()) !~ /\AU/ ? b : "usage: #{program_name} [<options>]\n\n" } p.define_singleton_method(:help) { super().gsub(/(-\S+)=\[/, '\1[=') << " -h, --help\n --version\n\n" } end parser.tap do |p| p.program_name = ::PROGRAM if defined? ::PROGRAM p.version = ::VERSION if defined? ::VERSION end end
several_variants(*opts, &handler)
click to toggle source
# File lib/optbind.rb, line 191 def several_variants(*opts, &handler) bound, variable, default = false, nil, nil if opts.size == 1 case opts[0] when Hash hash = opts[0].dup variable = hash.delete(:variable) || hash.delete(:bind) bound = !(opts.delete(:bound) === false) && !!variable default = hash.delete(:default) || (@reader.call(variable.to_sym) if variable) opts, handler = Switch.parser_opts_from_hash hash, &handler when String string, variable = *(opts[0] !~ /\A\s*-/ ? opts[0].split(/\s+/, 2).reverse : [opts[0], nil]) bound, default = !!variable, (@reader.call(variable.to_sym) if variable) opts, handler = Switch.parser_opts_from_string string, &handler end end variable = variable.to_sym if variable return opts, handler, bound, variable, default end
Also aliased as: several_variants_without_default_description