Class: OptParse2

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

Overview

Make sure it’s a subclass

Defined Under Namespace

Modules: Helpers, Pathname, Positional Classes: Context

Constant Summary collapse

DONT_ASSIGN =

A constant that, when returned, will not actually assign objects inside ‘into:`s.

Object.new.freeze
VERSION =
"0.7.0"

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOptParse2

Returns a new instance of OptParse2.



18
19
20
21
22
23
24
25
26
# File 'lib/optparse2.rb', line 18

def initialize(...)
  @defaults = Set[]
  @positional = []
  @required = Set[]
  @rest = nil
  @group = nil
  self.pos_set_banner = OptParse2.pos_set_banner
  super
end

Class Attribute Details

.pos_set_bannerObject

Returns the value of attribute pos_set_banner.



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

def pos_set_banner
  @pos_set_banner
end

Instance Attribute Details

#pos_set_bannerObject

Returns the value of attribute pos_set_banner.



236
237
238
# File 'lib/optparse2.rb', line 236

def pos_set_banner
  @pos_set_banner
end

Instance Method Details

#_super_order!Object



93
# File 'lib/optparse2.rb', line 93

alias _super_order! order!

#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

#group(name, default: nodefault=true) ⇒ Object



82
83
84
85
86
87
88
89
90
91
# File 'lib/optparse2.rb', line 82

def group(name, default: nodefault=true)
  old_group, @group = @group, name
  yield
  if !nodefault && !@defaults.any? { |x| x.switch_name.to_sym == name }
    (orig_default = default; default = proc { orig_default }) unless default.respond_to?(:call)
    @defaults << Struct.new(:switch_name, :default_){ def default = default_.() }.new(name, default) # TODO: This should probably be extracted out into a class lol
  end
ensure
  @group = old_group
end

#make_switch(opts, block, hidden: false, key: @group, default: nodefault=true, default_bypass: false, default_description: nil, required: false, multiple: nil) ⇒ Object

Update ‘make_switch` to support OptParse2’s keyword arguments



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/optparse2.rb', line 32

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

  sw.extend Helpers
  if key
    sw.set_switch_name_possibly_block_value key
  end
  sw.set_hidden if hidden
  sw.set_multiple multiple if multiple

  if (not_style = rest[2])
    not_style.extend Helpers
    not_style.switch_name = key if key
    not_style.set_hidden if hidden
  end

  if required
    unless nodefault
      raise ArgumentError, "cannot supply both a default with required: true"
    end
    @required << sw.switch_name
  end

  if nodefault && default_description != nil
    raise ArgumentError, "default: not supplied, but default_description: given"
  elsif nodefault && default_bypass
    raise ArgumentError, "default: not supplied, but default_bypass: given"
  elsif not nodefault
    sw.set_default(default, default_description, default_bypass)
    @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



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/optparse2.rb', line 155

def order!(argv = default_argv, into: nil, **keywords, &nonopt)
  Context.with_context into:, nonopt: do |context|

    # Parse all normal options in the command line
    non_options = []
    trailing_options = super(argv, into: context, **keywords, &non_options.method(:<<))
    not_matched_options = non_options + trailing_options

    # Now parse positional arguments and the "rest" argument
    parse_positional_arguments!(not_matched_options, context, keywords)
    parse_rest_argument!(not_matched_options, context)

    context.handle_deferred!

    # Now handle defaults---anything with a default that hasn't been assigned so far is set
    assign_defaults!(context)

    # Now that all arguments are parsed, and the defaults have been handled, check to make sure
    # that all required arguments are handled.
    ensure_all_required_arguments_were_supplied!(context)

    # For each non-option argument, call the `nonopt` block
    not_matched_options.each(&nonopt)

    # Replace the original argv with the resulting options
    argv.replace not_matched_options
  end
end

#pos(name, *a, key: name, **b, &block) ⇒ Object



237
238
239
240
241
242
243
244
245
246
# File 'lib/optparse2.rb', line 237

def pos(name, *a, key: name, **b, &block)
  banner.concat " #{name}" if pos_set_banner

  sw, *rest = make_switch ["--*-positional-#{@positional.length} #{name}", *a], block, key:, **b
  sw.extend Positional
  sw.name = name
  sw.switch_name = key
  top.append(sw, *rest)
  @positional.append sw
end

#rest(name, *description, key: name.to_s.tr(' ', '-').to_sym, required: 0, &block) ⇒ Object



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/optparse2.rb', line 248

def rest(name, *description, key: name.to_s.tr(' ', '-').to_sym, required: 0, &block)
  required = case required
             when true then 1
             when false then 0
             when ->x{ Integer === x && !x.negative? } then required
             else raise TypeError, "invalid type for required: #{required.class} (must be bool or positive integer)"
             end

  required = 1 if required == true
  title = "#{'[' if required.zero?}#{name} ...#{']' if required.zero?}"
  banner.concat " #{title}" if pos_set_banner
  title += " (#{required} arg minimum)" if required > 0

  on sprintf "%s%-*s %s", summary_indent, summary_width, title, description.first
  description[1..]&.each do |descr|
    on sprintf "%s%-*s %s", summary_indent, summary_width, '', descr
  end

  @rest = { name:, key:, required: required || 0, block: }
end

#summary(msg) ⇒ Object

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



78
79
80
# File 'lib/optparse2.rb', line 78

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