Class: OptParse2
- Inherits:
-
OptParse
- Object
- OptParse
- OptParse2
- Defined in:
- lib/optparse2.rb,
lib/optparse2.rb,
lib/optparse2/fixes.rb,
lib/optparse2/version.rb
Overview
Make sure it’s a subclass
Defined Under Namespace
Modules: Helpers, Pathname, Positional
Constant Summary collapse
- VERSION =
"0.6.0"
Class Attribute Summary collapse
-
.pos_set_banner ⇒ Object
Returns the value of attribute pos_set_banner.
Instance Attribute Summary collapse
-
#into ⇒ Object
readonly
Returns the value of attribute into.
-
#pos_set_banner ⇒ Object
Returns the value of attribute pos_set_banner.
Instance Method Summary collapse
- #def_head_option ⇒ Object
-
#define(*opts, &block) ⇒ Object
(also: #def_option)
Provide support for passing keyword arguments into ‘make_switch`.
- #define_head(*opts, &block) ⇒ Object
- #group(name, default: nodefault=true) ⇒ Object
-
#initialize ⇒ OptParse2
constructor
A new instance of OptParse2.
-
#make_switch(opts, block, hidden: false, key: @group, default: nodefault=true, default_description: nil, required: false) ⇒ Object
Update ‘make_switch` to support OptParse2’s keyword arguments.
- #on ⇒ Object
- #on_head ⇒ Object
- #on_tail ⇒ Object
- #order!(argv = default_argv, into: nil, **keywords, &nonopt) ⇒ Object
- #pos(name, *a, key: name, **b, &block) ⇒ Object
- #rest(name, *description, key: name.to_s.tr(' ', '-').to_sym, required: 0, &block) ⇒ Object
-
#summary(msg) ⇒ Object
Provide a “summary” field, which just puts the message at the end of the usage.
Constructor Details
Class Attribute Details
.pos_set_banner ⇒ Object
Returns the value of attribute pos_set_banner.
12 13 14 |
# File 'lib/optparse2.rb', line 12 def @pos_set_banner end |
Instance Attribute Details
#into ⇒ Object (readonly)
Returns the value of attribute into.
16 17 18 |
# File 'lib/optparse2.rb', line 16 def into @into end |
#pos_set_banner ⇒ Object
Returns the value of attribute pos_set_banner.
235 236 237 |
# File 'lib/optparse2.rb', line 235 def @pos_set_banner end |
Instance Method Details
#def_head_option ⇒ Object
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
109 110 111 112 113 114 115 116 117 118 |
# File 'lib/optparse2.rb', line 109 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_description: nil, required: false) ⇒ Object
Update ‘make_switch` to support OptParse2’s keyword arguments
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/optparse2.rb', line 71 def make_switch(opts, block, hidden: false, key: @group, default: nodefault=true, default_description: nil, required: false) sw, *rest = super(opts, block) sw.extend Helpers if key sw.set_switch_name_possibly_block_value key end sw.set_hidden if hidden 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 not nodefault sw.set_default(default, default_description) @defaults << sw end [sw, *rest] end |
#on ⇒ Object
5 |
# File 'lib/optparse2/fixes.rb', line 5 def on(...) define(...); self end |
#on_head ⇒ Object
9 |
# File 'lib/optparse2/fixes.rb', line 9 def on_head(...) define_head(...); self end |
#on_tail ⇒ Object
13 |
# File 'lib/optparse2/fixes.rb', line 13 def on_tail(...) define_tail(...); self end |
#order!(argv = default_argv, into: nil, **keywords, &nonopt) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 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 |
# File 'lib/optparse2.rb', line 120 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| super(key, value) into[key] = value end # Really this shouldn't be on the class and should probably be passed to the block of each # parameter as requested, but that requires a _significant_ amount of tinkering with `optparse`'s # internals, which is not really in scope. @into = already_done = [] result = super(argv, into: already_done, **keywords, &.method(:<<)) argv2 = ( + result).each_with_index.flat_map { ["--*-positional-#{_2}", _1] } old_raise, self.raise_unknown = self.raise_unknown, false begin super(argv2, into: already_done, **keywords) rescue OptParse::InvalidArgument => err err.args[0] = @positional[err.args[0][/\d+/].to_i].name raise ensure self.raise_unknown = old_raise end argv2 = argv2.each_slice(2).map { _2 } if @rest if argv2.length < @rest.fetch(:required, 0) raise ParseError, "at least #{@rest[:required]} trailing arguments required (only got #{argv2.length})", caller(1) end argv2 = @rest[:block] ? @rest[:block].call(argv2) : argv2 into[@rest[:key]] = argv2.dup if @rest[:key] argv2.clear elsif !argv2.empty? && self.raise_unknown && !@positional.empty? raise ParseError, "got unexpected positional argument: #{argv2.first}", caller(1) else argv2.each(&nonopt) end @defaults.each do |sw| key = sw.switch_name.to_sym next if already_done.key? key into[key] = sw.default() end @required.each do |key| raise ParseError, "required option '#{key}' not provided" unless already_done.key? key.to_sym end argv2 ensure @into = nil # make sure we unset it when returning end |
#pos(name, *a, key: name, **b, &block) ⇒ Object
236 237 238 239 240 241 242 243 244 245 |
# File 'lib/optparse2.rb', line 236 def pos(name, *a, key: name, **b, &block) .concat " #{name}" if 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
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/optparse2.rb', line 247 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?}" .concat " #{title}" if 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
105 106 107 |
# File 'lib/optparse2.rb', line 105 def summary(msg) on_tail("\n" + msg) end |