Class: OptParse2
- Inherits:
-
OptParse
- Object
- OptParse
- OptParse2
- 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.2"
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
- #_super_order! ⇒ Object
- #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_bypass: false, default_description: nil, required: false, multiple: nil) ⇒ Object
Update ‘make_switch` to support OptParse2’s keyword arguments.
- #on ⇒ Object
- #on_head ⇒ Object
- #on_tail ⇒ Object
- #order!(argv = default_argv, into: nil, abort: false, **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.
14 15 16 |
# File 'lib/optparse2.rb', line 14 def @pos_set_banner end |
Instance Attribute Details
#pos_set_banner ⇒ Object
Returns the value of attribute pos_set_banner.
238 239 240 |
# File 'lib/optparse2.rb', line 238 def @pos_set_banner end |
Instance Method Details
#_super_order! ⇒ Object
93 |
# File 'lib/optparse2.rb', line 93 alias _super_order! order! |
#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
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 |
#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, abort: false, **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 183 184 |
# File 'lib/optparse2.rb', line 155 def order!(argv = default_argv, into: nil, abort: false, **keywords, &nonopt) Context.with_context into:, nonopt: do |context| # Parse all normal options in the command line = [] = super(argv, into: context, **keywords, &.method(:<<)) = + # Now parse positional arguments and the "rest" argument parse_positional_arguments!(, context, keywords) parse_rest_argument!(, 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 .each(&nonopt) # Replace the original argv with the resulting options argv.replace end rescue OptionParser::ParseError => err abort ? abort(err) : raise end |
#pos(name, *a, key: name, **b, &block) ⇒ Object
239 240 241 242 243 244 245 246 247 248 |
# File 'lib/optparse2.rb', line 239 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
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/optparse2.rb', line 250 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
78 79 80 |
# File 'lib/optparse2.rb', line 78 def summary(msg) on_tail("\n" + msg) end |