Class: OptParse2
- Inherits:
-
OptParse
- Object
- OptParse
- OptParse2
- 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: Helpers, Pathname, Positional
Constant Summary collapse
- VERSION =
"0.5.0"
Class Attribute Summary collapse
-
.pos_set_banner ⇒ Object
Returns the value of attribute pos_set_banner.
Instance Attribute Summary collapse
-
#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
-
#initialize ⇒ OptParse2
constructor
A new instance of OptParse2.
-
#make_switch(opts, block, hidden: false, key: nil, default: nodefault=true, default_description: nil, required: false) ⇒ Object
Update
make_switchto 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.
13 14 15 |
# File 'lib/optparse2.rb', line 13 def @pos_set_banner end |
Instance Attribute Details
#pos_set_banner ⇒ Object
Returns the value of attribute pos_set_banner.
216 217 218 |
# File 'lib/optparse2.rb', line 216 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 |
#make_switch(opts, block, hidden: false, key: nil, default: nodefault=true, default_description: nil, required: false) ⇒ Object
Update make_switch to support OptParse2’s keyword arguments
70 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 70 def make_switch(opts, block, hidden: false, key: nil, default: nodefault=true, default_description: nil, required: false) sw, *rest = super(opts, block) sw.extend Helpers if key sw.switch_name = key sw.set_block_name_because_of_switch_name 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
109 110 111 112 113 114 115 116 117 118 119 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 |
# File 'lib/optparse2.rb', line 109 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 = [] result = super(argv, into: already_done, **keywords, &.method(:<<)) argv2 = .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 if @rest[:key] 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 end |
#pos(name, *a, key: name, **b, &block) ⇒ Object
217 218 219 220 221 222 223 224 225 226 |
# File 'lib/optparse2.rb', line 217 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
228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/optparse2.rb', line 228 def rest(name, *description, key: name.to_s.tr(' ', '-').to_sym, required: 0, &block) 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 |