Class: OptParse2

Inherits:
OptParse
  • Object
show all
Defined in:
lib/optparse2.rb,
lib/optparse2.rb,
lib/optparse2/fixes.rb,
lib/optparse2/version.rb

Overview

require_relative “optparse2/helpers”

Defined Under Namespace

Modules: Globals, Helpers

Constant Summary collapse

VERSION =
"0.1.2"

Instance Method Summary collapse

Constructor Details

#initializeOptParse2

Returns a new instance of OptParse2.



12
13
14
15
# File 'lib/optparse2.rb', line 12

def initialize(...)
  @defaults = Set[]
  super
end

Instance Method Details

#def_head_optionObject



8
# File 'lib/optparse2/fixes.rb', line 8

def define_head(*opts, **, &block) top.prepend(*(sw = make_switch(opts, block, **))); sw[0] end

#define(*opts, &block) ⇒ Object Also known as: def_option

Provide support for passing keyword arguments into ‘make_switch`



3
# File 'lib/optparse2/fixes.rb', line 3

def define(*opts, **, &block) top.append(*(sw = make_switch(opts, block, **))); sw[0] end

#define_head(*opts, &block) ⇒ Object



7
# File 'lib/optparse2/fixes.rb', line 7

def define_head(*opts, **, &block) top.prepend(*(sw = make_switch(opts, block, **))); sw[0] end

#make_switch(opts, block, hidden: false, key: nil, default: nodefault=true, default_description: nil) ⇒ Object

Update ‘make_switch` to support OptParse2’s keyword arguments



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/optparse2.rb', line 50

def make_switch(opts, block, hidden: false, key: nil, default: nodefault=true, default_description: nil)
  sw, *rest = super(opts, block)

  sw.extend Helpers

  sw.switch_name = key if key
  sw.set_hidden if hidden

  if nodefault && default_description != nil
    raise ArgumentError, "default: not supplied, but default_description: given"
  elsif not nodefault
    sw.set_default(default, default_description)
    @defaults << sw
  end

  [sw, *rest]
end

#onObject



5
# File 'lib/optparse2/fixes.rb', line 5

def on(...) define(...); self end

#on_headObject



9
# File 'lib/optparse2/fixes.rb', line 9

def on_head(...) define_head(...); self end

#on_tailObject



13
# File 'lib/optparse2/fixes.rb', line 13

def on_tail(...) define_tail(...); self end

#order!(argv = default_argv, into: nil, **keywords, &nonopt) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/optparse2.rb', line 73

def order!(argv = default_argv, into: nil, **keywords, &nonopt)
  if into.nil? && !@defaults.empty?
    raise "cannot call `order!` without an `into:` if there are default values"
  end

  already_done = {}
  already_done.define_singleton_method(:[]=) do |key, value|
    key = key.to_s
    super(key, value)
    into[key] = value
  end

  result = super(argv, into: already_done, **keywords, &nonopt)

  @defaults.each do |sw|
    key = sw.switch_name
    next if already_done.key? key
    into[key] = sw.default()
  end

  result
end

#summary(msg) ⇒ Object

Provide a “summary” field, which just puts the message at the end of the usage



69
70
71
# File 'lib/optparse2.rb', line 69

def summary(msg)
  on_tail("\n" + msg)
end