Class: Slop
Defined Under Namespace
Classes: Error, InvalidArgumentError, InvalidOptionError, MissingArgumentError, MissingOptionError, Option, Options
Constant Summary collapse
- VERSION =
Returns The current version string.
'2.3.1'
Instance Attribute Summary collapse
-
#aliases ⇒ Array
A list of aliases this command uses.
-
#banner(text = nil) ⇒ String
Set or return banner text.
- #commands ⇒ Hash readonly
-
#description(text = nil) ⇒ String
Set or return the description.
-
#longest_flag ⇒ Integer
The length of the longest flag slop knows of.
- #options ⇒ Options readonly
-
#summary(text = nil) ⇒ String
Set or return the summary.
Class Method Summary collapse
-
.optspec(optspec, *options) ⇒ Slop
Build options from an optspec string.
-
.parse(items = ARGV, options = {}, &block) ⇒ Slop
Parses the items from a CLI format into a friendly object.
-
.parse!(items = ARGV, options = {}, &block) ⇒ Slop
Identical to Slop.parse, but removes parsed options from the original Array.
Instance Method Summary collapse
-
#[](key) ⇒ Object
(also: #get)
Returns the value associated with that option.
-
#command(label, options = {}, &block) ⇒ Slop
Namespace options depending on what command is executed.
-
#each(&block) ⇒ Object
Enumerable interface.
-
#execute(args = []) {|Slop| ... } ⇒ Object
Add an execution block (for commands).
-
#initialize(*opts, &block) ⇒ Slop
constructor
A new instance of Slop.
-
#inspect ⇒ String
This Slop object will options and configuration settings revealed.
-
#method_missing(meth, *args, &block) ⇒ Boolean
Allows you to check whether an option was specified in the parsed list.
-
#missing ⇒ Array
Fetch a list of options which were missing from the parsed list.
-
#on_empty(obj = nil, &block) ⇒ Object
(also: #on_empty=)
Trigger an event when Slop has no values to parse.
-
#on_noopts(obj = nil, &block) ⇒ Object
(also: #on_optionless)
Trigger an event when the arguments contain no options.
-
#option(*args, &block) ⇒ Slop::Option
(also: #opt, #on)
Specify an option with a short or long version, description and type.
-
#parse(items = ARGV, &block) ⇒ Object
Parse a list of options, leaving the original Array unchanged.
-
#parse!(items = ARGV, &block) ⇒ Object
Parse a list of options, removing parsed options from the original Array.
-
#present?(*option_names) ⇒ Boolean
Check if an option is specified in the parsed list.
-
#to_hash(symbols = true) ⇒ Hash
(also: #to_h)
Returns the parsed list into a option/value hash.
-
#to_s ⇒ String
(also: #help)
Returns the banner followed by available options listed on the next line.
-
#to_struct(name = nil) ⇒ Class
Return parsed items as a new Class.
Methods included from Enumerable
Constructor Details
#initialize(*opts, &block) ⇒ Slop
Returns a new instance of Slop.
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 |
# File 'lib/slop-2.3.1/lib/slop.rb', line 389 def initialize(*opts, &block) = opts.last.is_a?(Hash) ? opts.pop : {} [:banner] = opts.shift if opts[0].respond_to?(:to_str) opts.each { |o| [o] = true } @options = Options.new @commands = {} @execution_block = nil @longest_flag = 0 @invalid_options = [] @banner = [:banner] @strict = [:strict] @ignore_case = [:ignore_case] @multiple_switches = .fetch(:multiple_switches, true) @autocreate = [:autocreate] @completion = .fetch(:completion, true) @arguments = [:arguments] @on_empty = [:on_empty] @io = .fetch(:io, $stderr) @on_noopts = [:on_noopts] || [:on_optionless] @sloptions = if block_given? block.arity == 1 ? yield(self) : instance_eval(&block) end if [:help] on :h, :help, 'Print this help message', :tail => true do @io.puts help exit unless [:exit_on_help] == false end end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Boolean
Allows you to check whether an option was specified in the parsed list
Merely sugar for ‘present?`
691 692 693 694 695 696 697 698 699 700 |
# File 'lib/slop-2.3.1/lib/slop.rb', line 691 def method_missing(meth, *args, &block) super unless meth.to_s[-1, 1] == '?' present = present? meth.to_s.chomp('?') (class << self; self; end).instance_eval do define_method(meth) { present } end present end |
Instance Attribute Details
#aliases ⇒ Array
Returns A list of aliases this command uses.
339 340 341 |
# File 'lib/slop-2.3.1/lib/slop.rb', line 339 def aliases @aliases end |
#banner(text = nil) ⇒ String
Set or return banner text
433 434 435 436 |
# File 'lib/slop-2.3.1/lib/slop.rb', line 433 def (text=nil) @banner = text if text @banner end |
#commands ⇒ Hash (readonly)
318 319 320 |
# File 'lib/slop-2.3.1/lib/slop.rb', line 318 def commands @commands end |
#description(text = nil) ⇒ String
Set or return the description
459 460 461 462 |
# File 'lib/slop-2.3.1/lib/slop.rb', line 459 def description(text=nil) @description = text if text @description end |
#longest_flag ⇒ Integer
Returns The length of the longest flag slop knows of.
336 337 338 |
# File 'lib/slop-2.3.1/lib/slop.rb', line 336 def longest_flag @longest_flag end |
#options ⇒ Options (readonly)
315 316 317 |
# File 'lib/slop-2.3.1/lib/slop.rb', line 315 def @options end |
#summary(text = nil) ⇒ String
Set or return the summary
446 447 448 449 |
# File 'lib/slop-2.3.1/lib/slop.rb', line 446 def summary(text=nil) @summary = text if text @summary end |
Class Method Details
.optspec(optspec, *options) ⇒ Slop
Build options from an optspec string
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/slop-2.3.1/lib/slop.rb', line 295 def self.optspec(optspec, *) if optspec[/^--+$/] , optspec = optspec.split(/^--+$/, 2) end lines = optspec.split("\n").reject(&:empty?) opts = Slop.new(, *) lines.each do |line| opt, description = line.split(' ', 2) short, long = opt.split(',').map { |s| s.sub(/\A--?/, '') } argument = long && long[/\=$/] long.sub!(/\=$/, '') if argument opts.on short, long, description, argument end opts end |
.parse(items = ARGV, options = {}, &block) ⇒ Slop
Parses the items from a CLI format into a friendly object
278 279 280 |
# File 'lib/slop-2.3.1/lib/slop.rb', line 278 def self.parse(items=ARGV, ={}, &block) initialize_and_parse items, false, , &block end |
Instance Method Details
#[](key) ⇒ Object Also known as: get
Returns the value associated with that option. If an option doesn’t exist, a command will instead be searched for
489 490 491 492 |
# File 'lib/slop-2.3.1/lib/slop.rb', line 489 def [](key) option = @options[key] option ? option.argument_value : @commands[key] end |
#command(label, options = {}, &block) ⇒ Slop
Namespace options depending on what command is executed
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 |
# File 'lib/slop-2.3.1/lib/slop.rb', line 548 def command(label, ={}, &block) if @commands.key?(label) raise ArgumentError, "command `#{label}` already exists" end slop = Slop.new @sloptions.merge() slop.aliases = Array(.delete(:aliases) || .delete(:alias)) @commands[label] = slop slop.aliases.each { |a| @commands[a] = @commands[label] } if block_given? block.arity == 1 ? yield(slop) : slop.instance_eval(&block) end slop end |
#each(&block) ⇒ Object
Enumerable interface
479 480 481 |
# File 'lib/slop-2.3.1/lib/slop.rb', line 479 def each(&block) @options.each(&block) end |
#execute(args = []) {|Slop| ... } ⇒ Object
Add an execution block (for commands)
610 611 612 613 614 615 616 |
# File 'lib/slop-2.3.1/lib/slop.rb', line 610 def execute(args=[], &block) if block_given? @execution_block = block elsif @execution_block.respond_to?(:call) @execution_block.call(self, args) end end |
#inspect ⇒ String
Returns This Slop object will options and configuration settings revealed.
746 747 748 749 |
# File 'lib/slop-2.3.1/lib/slop.rb', line 746 def inspect "#<Slop config_options=#{@sloptions.inspect}\n " + .map(&:inspect).join("\n ") + "\n>" end |
#missing ⇒ Array
Fetch a list of options which were missing from the parsed list
677 678 679 |
# File 'lib/slop-2.3.1/lib/slop.rb', line 677 def missing @options.select { |opt| not present?(opt.key) }.map { |opt| opt.key } end |
#on_empty(obj = nil, &block) ⇒ Object Also known as: on_empty=
Trigger an event when Slop has no values to parse
575 576 577 |
# File 'lib/slop-2.3.1/lib/slop.rb', line 575 def on_empty(obj=nil, &block) @on_empty ||= (obj || block) end |
#on_noopts(obj = nil, &block) ⇒ Object Also known as: on_optionless
Trigger an event when the arguments contain no options
589 590 591 |
# File 'lib/slop-2.3.1/lib/slop.rb', line 589 def on_noopts(obj=nil, &block) @on_noopts ||= (obj || block) end |
#option(*args, &block) ⇒ Slop::Option Also known as: opt, on
Specify an option with a short or long version, description and type
516 517 518 519 520 521 522 523 524 525 526 527 528 |
# File 'lib/slop-2.3.1/lib/slop.rb', line 516 def option(*args, &block) = args.last.is_a?(Hash) ? args.pop : {} short, long, desc, arg, extras = (args) .merge!(extras) [:argument] = true if @sloptions[:all_accept_arguments] option = Option.new(self, short, long, desc, arg, , &block) @options << option option end |
#parse(items = ARGV, &block) ⇒ Object
Parse a list of options, leaving the original Array unchanged
467 468 469 |
# File 'lib/slop-2.3.1/lib/slop.rb', line 467 def parse(items=ARGV, &block) parse_items items, &block end |
#parse!(items = ARGV, &block) ⇒ Object
Parse a list of options, removing parsed options from the original Array
474 475 476 |
# File 'lib/slop-2.3.1/lib/slop.rb', line 474 def parse!(items=ARGV, &block) parse_items items, true, &block end |
#present?(*option_names) ⇒ Boolean
Check if an option is specified in the parsed list
Does the same as Slop#option? but a convenience method for unacceptable method names
710 711 712 |
# File 'lib/slop-2.3.1/lib/slop.rb', line 710 def present?(*option_names) option_names.all? { |opt| @options[opt] && @options[opt].count > 0 } end |
#to_hash(symbols = true) ⇒ Hash Also known as: to_h
Returns the parsed list into a option/value hash
626 627 628 629 630 631 632 633 |
# File 'lib/slop-2.3.1/lib/slop.rb', line 626 def to_hash(symbols=true) @options.reduce({}) do |hsh, option| key = option.key key = key.to_sym if symbols hsh[key] = option.argument_value hsh end end |
#to_s ⇒ String Also known as: help
Returns the banner followed by available options listed on the next line
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 |
# File 'lib/slop-2.3.1/lib/slop.rb', line 723 def to_s parts = [] parts << if parts << summary if summary parts << wrap_and_indent(description, 80, 4) if description if .size > 0 parts << "options:" heads = @options.reject(&:tail) tails = @options.select(&:tail) all = (heads + tails).select(&:help) parts << all.map(&:to_s).join("\n") end parts.join("\n\n") end |
#to_struct(name = nil) ⇒ Class
Return parsed items as a new Class
658 659 660 661 |
# File 'lib/slop-2.3.1/lib/slop.rb', line 658 def to_struct(name=nil) hash = to_hash Struct.new(name, *hash.keys).new(*hash.values) unless hash.empty? end |