forked from leejarvis/slop
/
slop.rb
669 lines (588 loc) · 19.3 KB
/
slop.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
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
103
104
105
106
107
108
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
require 'slop/options'
require 'slop/option'
class Slop
include Enumerable
# Raised when an option expects an argument and none is given
class MissingArgumentError < RuntimeError; end
# Raised when an option specifies the `:match` attribute and this
# options argument does not match this regexp
class InvalidArgumentError < RuntimeError; end
# Raised when the `:strict` option is enabled and an unknown
# or unspecified option is used
class InvalidOptionError < RuntimeError; end
# @return [String] The current version string
VERSION = '1.9.0'
# Parses the items from a CLI format into a friendly object
#
# @param [Array] items Items to parse into options.
# @example Specifying three options to parse:
# opts = Slops.parse do
# on :v, :verbose, 'Enable verbose mode'
# on :n, :name, 'Your name'
# on :a, :age, 'Your age'
# end
# @return [Slop] Returns an instance of Slop
def self.parse(items=ARGV, options={}, &block)
initialize_and_parse items, false, options, &block
end
# Identical to {Slop.parse}, but removes parsed options from the
# original Array
#
# @return [Slop] Returns an instance of Slop
def self.parse!(items=ARGV, options={}, &block)
initialize_and_parse items, true, options, &block
end
# @return [Options]
attr_reader :options
# @return [Hash]
attr_reader :commands
# @overload banner=(string)
# Set the banner
# @param [String] string The text to set the banner to
attr_writer :banner
# @overload summary=(string)
# Set the summary
# @param [String] string The text to set the summary to
attr_writer :summary
# @overload description=(string)
# Set the description
# @param [String] string The text to set the description to
attr_writer :description
# @return [Integer] The length of the longest flag slop knows of
attr_accessor :longest_flag
# @return [Array] A list of aliases this command uses
attr_accessor :aliases
# @option opts [Boolean] :help
# * Automatically add the `help` option
#
# @option opts [Boolean] :strict
# * Raises when a non listed option is found, false by default
#
# @option opts [Boolean] :multiple_switches
# * Allows `-abc` to be processed as the options 'a', 'b', 'c' and will
# force their argument values to true. By default Slop with parse this
# as 'a' with the argument 'bc'
#
# @option opts [String] :banner
# * The banner text used for the help
#
# @option opts [Proc, #call] :on_empty
# * Any object that respondes to `call` which is executed when Slop has
# no items to parse
#
# @option opts [IO, #puts] :io ($stderr)
# * An IO object for writing to when :help => true is used
#
# @option opts [Boolean] :exit_on_help (true)
# * When false and coupled with the :help option, Slop will not exit
# inside of the `help` option
#
# @option opts [Boolean] :ignore_case (false)
# * Ignore options case
#
# @option opts [Proc, #call] :on_noopts
# * Trigger an event when no options are found
#
# @option opts [Boolean] :autocreate (false)
# * Autocreate options depending on the Array passed to {#parse}
#
# @option opts [Boolean] :arguments (false)
# * Set to true to enable all specified options to accept arguments
# by default
#
# @option opts [Array] :aliases ([])
# * Primary uses by commands to implement command aliases
# @option opts [Boolean] :completion (true)
# * When true, commands will be auto completed. Ie `foobar` will be
# executed simply when `foo` `fo` or `foob` are used
def initialize(*opts, &block)
sloptions = opts.last.is_a?(Hash) ? opts.pop : {}
sloptions[:banner] = opts.shift if opts[0].respond_to? :to_str
opts.each { |o| sloptions[o] = true }
@options = Options.new
@commands = {}
@execution_block = nil
@longest_flag = 0
@invalid_options = []
@banner = sloptions[:banner]
@strict = sloptions[:strict]
@ignore_case = sloptions[:ignore_case]
@multiple_switches = sloptions[:multiple_switches]
@autocreate = sloptions[:autocreate]
@completion = sloptions.fetch(:completion, true)
@arguments = sloptions[:arguments]
@on_empty = sloptions[:on_empty]
@io = sloptions.fetch(:io, $stderr)
@on_noopts = sloptions[:on_noopts] || sloptions[:on_optionless]
@sloptions = sloptions
if block_given?
block.arity == 1 ? yield(self) : instance_eval(&block)
end
if sloptions[:help]
on :h, :help, 'Print this help message', :tail => true do
@io.puts help
exit unless sloptions[:exit_on_help] == false
end
end
end
# Set or return banner text
#
# @param [String] text Displayed banner text
# @example
# opts = Slop.parse do
# banner "Usage - ruby foo.rb [arguments]"
# end
# @return [String] The current banner
def banner(text=nil)
@banner = text if text
@banner
end
# Set or return the summary
#
# @param [String] text Displayed summary text
# @example
# opts = Slop.parse do
# summary "do stuff with more stuff"
# end
# @return [String] The current summary
def summary(text=nil)
@summary = text if text
@summary
end
# Set or return the description
#
# @param [String] text Displayed description text
# @example
# opts = Slop.parse do
# description "This command does a lot of stuff with other stuff."
# end
# @return [String] The current description
def description(text=nil)
@description = text if text
@description
end
# Parse a list of options, leaving the original Array unchanged
#
# @param [Array] items A list of items to parse
def parse(items=ARGV, &block)
parse_items items, &block
end
# Parse a list of options, removing parsed options from the original Array
#
# @param [Array] items A list of items to parse
def parse!(items=ARGV, &block)
parse_items items, true, &block
end
# Enumerable interface
def each(&block)
@options.each(&block)
end
# @param [Symbol] key Option symbol
# @example
# opts[:name] #=> "Emily"
# opts.get(:name) #=> "Emily"
# @return [Object] Returns the value associated with that option. If an
# option doesn't exist, a command will instead be searched for
def [](key)
option = @options[key]
option ? option.argument_value : @commands[key]
end
alias :get :[]
# Specify an option with a short or long version, description and type
#
# @param [*] args Option configuration.
# @option args [Symbol, String] :short_flag Short option name.
# @option args [Symbol, String] :long_flag Full option name.
# @option args [String] :description Option description for use in Slop#help
# @option args [Boolean] :argument Specifies whether this option requires
# an argument
# @option args [Hash] :options Optional option configurations.
# @example
# opts = Slop.parse do
# on :n, :name, 'Your username', true # Required argument
# on :a, :age, 'Your age (optional)', :optional => true
# on :g, :gender, 'Your gender', :optional => false
# on :V, :verbose, 'Run in verbose mode', :default => true
# on :P, :people, 'Your friends', true, :as => Array
# on :h, :help, 'Print this help screen' do
# puts help
# end
# end
# @return [Slop::Option]
def option(*args, &block)
options = args.last.is_a?(Hash) ? args.pop : {}
short, long, desc, arg, extras = clean_options args
options.merge!(extras)
option = Option.new self, short, long, desc, arg, options, &block
@options << option
option
end
alias :opt :option
alias :on :option
# Namespace options depending on what command is executed
#
# @param [Symbol, String] label
# @param [Hash] options
# @example
# opts = Slop.new do
# command :create do
# on :v, :verbose
# end
# end
#
# # ARGV is `create -v`
# opts.commands[:create].verbose? #=> true
# @since 1.5.0
# @raise [ArgumentError] When this command already exists
# @return [Slop] a new instance of Slop namespaced to +label+
def command(label, options={}, &block)
if @commands[label]
raise ArgumentError, "command `#{label}` already exists"
end
slop = Slop.new @sloptions.merge(options)
slop.aliases = Array(options.delete(:aliases) || options.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
# Trigger an event when Slop has no values to parse
#
# @param [Object, #call] obj The object (which can be anything
# responding to `call`)
# @example
# Slop.parse do
# on_empty { puts 'No argument given!' }
# end
# @since 1.5.0
def on_empty(obj=nil, &block)
@on_empty ||= (obj || block)
end
alias :on_empty= :on_empty
# Trigger an event when the arguments contain no options
#
# @param [Object, #call] obj The object to be triggered (anything
# responding to `call`)
# @example
# Slop.parse do
# on_noopts { puts 'No options here!' }
# end
# @since 1.6.0
def on_noopts(obj=nil, &block)
@on_noopts ||= (obj || block)
end
alias :on_optionless :on_noopts
# Add an execution block (for commands)
#
# @example
# opts = Slop.new do
# command :foo do
# on :v, :verbose
#
# execute { |o| p o.verbose? }
# end
# end
# opts.parse %w[foo --verbose] #=> true
#
# @param [Array] args The list of arguments to send to this command
# is invoked
# @since 1.8.0
# @yields [Slop] an instance of Slop for this command
def execute(args=[], &block)
if block_given?
@execution_block = block
elsif @execution_block.respond_to?(:call)
@execution_block.call(self, args)
end
end
# Returns the parsed list into a option/value hash
#
# @example
# opts.to_hash #=> { 'name' => 'Emily' }
#
# # symbols!
# opts.to_hash(true) #=> { :name => 'Emily' }
# @return [Hash]
def to_hash(symbols=false)
@options.to_hash symbols
end
alias :to_h :to_hash
# Allows you to check whether an option was specified in the parsed list
#
# Merely sugar for `present?`
#
# @example
# #== ruby foo.rb -v
# opts.verbose? #=> true
# opts.name? #=> false
# @see Slop#present?
# @return [Boolean] true if this option is present, false otherwise
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
# Check if an option is specified in the parsed list
#
# Does the same as Slop#option? but a convenience method for unacceptable
# method names
#
# @param [Object] The object name to check
# @since 1.5.0
# @return [Boolean] true if this option is present, false otherwise
def present?(option_name)
!!get(option_name)
end
# Returns the banner followed by available options listed on the next line
#
# @example
# opts = Slop.parse do
# banner "Usage - ruby foo.rb [arguments]"
# on :v, :verbose, "Enable verbose mode"
# end
# puts opts
# @return [String] Help text.
def to_s
parts = []
parts << banner if banner
parts << summary if summary
parts << wrap_and_indent(description, 80, 4) if description
parts << "options:" if options.size > 0
parts << options.to_help if options.size > 0
parts.join("\n\n")
end
alias :help :to_s
# @return [String] This Slop object will options and configuration
# settings revealed
def inspect
"#<Slop config_options=#{@sloptions.inspect}\n " +
options.map(&:inspect).join("\n ") + "\n>"
end
private
class << self
private
def initialize_and_parse(items, delete, options, &block)
if items.is_a?(Hash) && options.empty?
options = items
items = ARGV
end
slop = new(options, &block)
delete ? slop.parse!(items) : slop.parse(items)
slop
end
end
# traverse through the list of items sent to parse() or parse!() and
# attempt to do the following:
#
# * Find an option object
# * Assign an argument to this option
# * Validate an option and/or argument depending on configuration options
# * Remove non-parsed items if `delete` is true
# * Yield any non-options to the block (if one is given)
def parse_items(items, delete=false, &block)
if items.empty? && @on_empty.respond_to?(:call)
@on_empty.call self
return items
elsif !items.any? {|i| i.to_s[/\A--?/] } && @on_noopts.respond_to?(:call)
@on_noopts.call self
return items
elsif execute_command(items, delete)
return items
end
trash = []
ignore_all = false
items.each_with_index do |item, index|
item = item.to_s
flag = item.sub(/\A--?/, '')
if item == '--'
trash << index
ignore_all = true
end
next if ignore_all
autocreate(flag, index, items) if @autocreate
option, argument = extract_option(item, flag)
if @multiple_switches && !option
trash << index if item[/\A-[^-]/]
next
end
if option
option.count += 1
trash << index
next if option.forced
option.argument_value = true
if option.expects_argument? || option.accepts_optional_argument?
argument ||= items.at(index + 1)
trash << index + 1
if !option.accepts_optional_argument? && argument =~ /\A--?[a-zA-Z][a-zA-Z0-9_-]*\z/
raise MissingArgumentError, "'#{option.key}' expects an argument, none given"
end
if argument
if option.match && !argument.match(option.match)
raise InvalidArgumentError, "'#{argument}' does not match #{option.match.inspect}"
end
option.argument_value = argument
option.call option.argument_value unless option.omit_exec?(items)
else
option.argument_value = nil
check_optional_argument!(option, flag)
end
else
option.call unless option.omit_exec?(items)
end
else
@invalid_options << flag if item[/\A--?/] && @strict
block.call(item) if block_given? && !trash.include?(index)
end
end
items.reject!.with_index { |o, i| trash.include?(i) } if delete
raise_if_invalid_options!
items
end
def check_optional_argument!(option, flag)
if option.accepts_optional_argument?
option.call
else
raise MissingArgumentError, "'#{flag}' expects an argument, none given"
end
end
def raise_if_invalid_options!
return if !@strict || @invalid_options.empty?
message = "Unknown option#{'s' if @invalid_options.size > 1}"
message << ' -- ' << @invalid_options.map { |o| "'#{o}'" }.join(', ')
raise InvalidOptionError, message
end
# if multiple_switches is enabled, this method filters through an items
# characters and attempts to find an Option object for each flag.
#
# Raises if a flag expects an argument or strict mode is enabled and a
# flag was not found
def enable_multiple_switches(item)
item[1..-1].each_char do |switch|
if option = @options[switch]
if option.expects_argument?
raise MissingArgumentError, "'-#{switch}' expects an argument, used in multiple_switch context"
end
option.argument_value = true
else
raise InvalidOptionError, "Unknown option '-#{switch}'" if @strict
end
end
end
# wrap and indent a string, used to wrap and indent a description string
def wrap_and_indent(string, width, indentation)
string.lines.map do |paragraph|
lines = []
line = ''
paragraph.split(/\s/).each do |word|
# Begin new line if it's too long
if (line + ' ' + word).length >= width
lines << line
line = ''
end
# Add word to line
line << (line == '' ? '' : ' ' ) + word
end
lines << line
lines.map { |l| ' '*indentation + l }.join("\n")
end.join("\n")
end
# attempt to extract an option from an argument, this method allows us
# to parse things like 'foo=bar' and '--no-value' for negative values
# returns an array of the Option object and an argument if one was found
def extract_option(item, flag)
if item[0, 1] == '-'
option = @options[flag]
option ||= @options[flag.downcase] if @ignore_case
end
unless option
case item
when /\A-[^-]/
if @multiple_switches
enable_multiple_switches(item)
else
flag, argument = flag.split('', 2)
option = @options[flag]
end
when /\A--([^=]+)=(.+)\z/
option, argument = @options[$1], $2
when /\A--no-(.+)\z/
option = @options[$1]
option.force_argument_value(false) if option
end
end
[option, argument]
end
# attempt to execute a command if one exists, returns a positive (tru-ish)
# result if the command was found and executed. If completion is enabled
# and a flag is found to be ambiguous, this method prints an error message
# to the @io object informing the user
def execute_command(items, delete)
str = items[0]
if str
command = @commands.keys.find { |c| c.to_s == str.to_s }
if !command && @completion
cmds = @commands.keys.select { |c| c.to_s[0, str.length] == str }
if cmds.size > 1
@io.puts "Command '#{str}' is ambiguous:"
@io.puts " " + cmds.map(&:to_s).sort.join(', ')
else
command = cmds.shift
end
end
end
if command
items.shift
opts = @commands[command]
delete ? opts.parse!(items) : opts.parse(items)
opts.execute(items.reject { |i| i == '--' })
end
end
# If autocreation is enabled this method simply generates an option
# and add's it to the existing list of options
def autocreate(flag, index, items)
return if present? flag
short, long = clean_options Array(flag)
arg = (items[index + 1] && items[index + 1] !~ /\A--?/)
@options << Option.new(self, short, long, nil, arg, {})
end
# Clean up arguments sent to `on` and return a list of 5 elements:
# * short flag (or nil)
# * long flag (or nil)
# * description (or nil)
# * true/false if this option takes an argument or not
# * extra options (ie: :as, :optional, and :help)
def clean_options(args)
options = []
extras = {}
extras[:as] =args.find {|c| c.is_a? Class }
args.delete(extras[:as])
extras.delete(:as) if extras[:as].nil?
short = args.first.to_s.sub(/\A--?/, '')
if short.size == 1
options.push short
args.shift
else
options.push nil
end
long = args.first
boolean = [true, false].include? long
if !boolean && long.to_s =~ /\A(?:--?)?[a-z_-]+\s[A-Z\s\[\]]+\z/
arg, help = args.shift.split(/ /, 2)
options.push arg.sub(/\A--?/, '')
extras[:optional] = help[0, 1] == '[' && help[-1, 1] == ']'
extras[:help] = help
elsif !boolean && long.to_s =~ /\A(?:--?)?[a-zA-Z][a-zA-Z0-9_-]+\z/
options.push args.shift.to_s.sub(/\A--?/, '')
else
options.push nil
end
options.push args.first.respond_to?(:to_sym) ? args.shift : nil
options.push @arguments ? true : (args.shift ? true : false)
options.push extras
end
end