Class: OptionBinder

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Handler, OptionParser::Acceptables
Defined in:
lib/optbind.rb,
lib/optbind/mode.rb,
lib/optbind/type.rb,
lib/optbind/default.rb

Defined Under Namespace

Modules: Arguable, Handler, Switch Classes: MissingArguments, TooManyArguments

Constant Summary collapse

BinaryInteger =
/\A[-+]?#{binary}\z/io
OctalInteger =
/\A[-+]?#{octal}\z/io
HexadecimalInteger =
/\A[-+]?#{hexadecimal}\z/io
ShellWords =

NOTE: ShellWords defined in OptionParser does not handle missing argument, therefore it is redefined to work properly on switches with optional argument

Shellwords

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Handler

#included_in, #listed_as, #matched_by

Constructor Details

#initialize(parser: nil, target: nil, bind: nil) {|_self| ... } ⇒ OptionBinder

Returns a new instance of OptionBinder.

Yields:

  • (_self)

Yield Parameters:

  • _self (OptionBinder)

    the object that the method was called on



9
10
11
12
13
14
# 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

Instance Attribute Details

#parserObject (readonly)

Returns the value of attribute parser.



7
8
9
# File 'lib/optbind.rb', line 7

def parser
  @parser
end

#targetObject (readonly)

Returns the value of attribute target.



7
8
9
# File 'lib/optbind.rb', line 7

def target
  @target
end

Instance Method Details

#argument(*opts, &handler) ⇒ Object Also known as: arg



94
95
96
97
98
99
100
101
102
103
104
105
106
# 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

#assigned?(v) ⇒ Boolean

Returns:

  • (Boolean)


134
135
136
137
# File 'lib/optbind.rb', line 134

def assigned?(v)
  return nil unless bound? v
  (@assigned_variables_with_values || {}).key? v.to_sym
end

#assigned_variablesObject



119
120
121
122
# File 'lib/optbind.rb', line 119

def assigned_variables
  return {} unless @assigned_variables_with_values
  @assigned_variables_with_values.dup
end

#bound?(v) ⇒ Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/optbind.rb', line 130

def bound?(v)
  (@bound_variables_with_defaults || {}).key? v.to_sym
end

#bound_defaultsObject



110
111
112
# File 'lib/optbind.rb', line 110

def bound_defaults
  @bound_variables_with_defaults ? @bound_variables_with_defaults.dup : {}
end

#bound_variablesObject



114
115
116
117
# 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) ⇒ Boolean

Returns:

  • (Boolean)


124
125
126
127
128
# 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) ⇒ Object Also known as: opt



78
79
80
81
82
83
84
85
86
87
88
89
90
# 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

#order(*argv, &blk) ⇒ Object



4
5
6
# File 'lib/optbind/mode.rb', line 4

def order(*argv, &blk)
  order! argv.dup.flatten, &blk
end

#order!(argv = parser.default_argv, &blk) ⇒ Object



8
9
10
# File 'lib/optbind/mode.rb', line 8

def order!(argv = parser.default_argv, &blk)
  parse_args! @parser.order! argv, &blk
end

#parse(*argv) ⇒ Object



51
52
53
# File 'lib/optbind.rb', line 51

def parse(*argv)
  parse! argv.dup.flatten
end

#parse!(argv = parser.default_argv) ⇒ Object



55
56
57
# File 'lib/optbind.rb', line 55

def parse!(argv = parser.default_argv)
  parse_args! @parser.parse! argv
end

#permute(*argv) ⇒ Object



12
13
14
# File 'lib/optbind/mode.rb', line 12

def permute(*argv)
  permute! argv.dup.flatten
end

#permute!(argv = parser.default_argv) ⇒ Object



16
17
18
# File 'lib/optbind/mode.rb', line 16

def permute!(argv = parser.default_argv)
  parse_args! @parser.permute! argv
end

#several_variants_without_default_descriptionObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/optbind/default.rb', line 4

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

#usage(*args) ⇒ Object Also known as: use



63
64
65
66
67
68
69
70
71
72
73
74
# 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