trollop 2.1.1 → 2.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +5 -5
- data/Gemfile.travis +9 -0
- data/History.txt +6 -0
- data/README.md +1 -1
- data/Rakefile +5 -2
- data/lib/trollop.rb +51 -48
- data/test/test_helper.rb +5 -10
- data/test/test_trollop.rb +229 -275
- data/trollop.gemspec +1 -1
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dbee7d87ff54b18dc15e986c58e8a9f511dda7d1
|
4
|
+
data.tar.gz: 6fc068202fc2863d9d851931cc67718e8c0b4bd1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4d79f559d36748e03c32069363d2d2415f341e13ffd5fb6165fb2e62dbfc7a8af70ae164d1c19f02d0be0ab056de2d0e6e15f6319b8f5a760087357db92d21cd
|
7
|
+
data.tar.gz: 4f0db3cd5cc44e6ff4e23abbc91a0175d9d0132af400cf13890696a9b68ccb8dadcef7b94f9bb3b1576cba6d549cf9b5d28a0c9a6635793fd28553524f641909
|
data/.travis.yml
CHANGED
data/Gemfile.travis
ADDED
data/History.txt
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
== 2.1.2 / 2015-03-10
|
2
|
+
* loosen mime-types requirements (for better ruby 1.8.7 support)
|
3
|
+
* use io/console gem instead of curses (for better jruby support)
|
4
|
+
* fix parsing bug when chronic gem is not available
|
5
|
+
* allow default array to be empty if a type is specified
|
6
|
+
* better specified license and better spec coverage
|
1
7
|
== 2.1.1 / 2015-01-03
|
2
8
|
* Remove curses as a hard dependency. It is optional. This can leverage the gem if it is present.
|
3
9
|
* Fix ruby -w warnings
|
data/README.md
CHANGED
data/Rakefile
CHANGED
@@ -1,6 +1,5 @@
|
|
1
|
-
require
|
1
|
+
require 'bundler/gem_tasks'
|
2
2
|
require 'rake/testtask'
|
3
|
-
require 'coveralls/rake/task'
|
4
3
|
|
5
4
|
task :default => :test
|
6
5
|
|
@@ -9,4 +8,8 @@ Rake::TestTask.new do |t|
|
|
9
8
|
t.pattern = "test/test_*.rb"
|
10
9
|
end
|
11
10
|
|
11
|
+
begin
|
12
|
+
require 'coveralls/rake/task'
|
12
13
|
Coveralls::RakeTask.new
|
14
|
+
rescue LoadError
|
15
|
+
end
|
data/lib/trollop.rb
CHANGED
@@ -6,7 +6,7 @@
|
|
6
6
|
require 'date'
|
7
7
|
|
8
8
|
module Trollop
|
9
|
-
VERSION = "2.1.
|
9
|
+
VERSION = "2.1.2"
|
10
10
|
|
11
11
|
## Thrown by Parser in the event of a commandline error. Not needed if
|
12
12
|
## you're using the Trollop::options entry.
|
@@ -174,16 +174,22 @@ class Parser
|
|
174
174
|
when Date; :date
|
175
175
|
when Array
|
176
176
|
if opts[:default].empty?
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
when IO; :ios
|
184
|
-
when Date; :dates
|
177
|
+
if opts[:type]
|
178
|
+
raise ArgumentError, "multiple argument type must be plural" unless MULTI_ARG_TYPES.include?(opts[:type])
|
179
|
+
nil
|
180
|
+
else
|
181
|
+
raise ArgumentError, "multiple argument type cannot be deduced from an empty array for '#{opts[:default][0].class.name}'"
|
182
|
+
end
|
185
183
|
else
|
186
|
-
|
184
|
+
case opts[:default][0] # the first element determines the types
|
185
|
+
when Integer; :ints
|
186
|
+
when Numeric; :floats
|
187
|
+
when String; :strings
|
188
|
+
when IO; :ios
|
189
|
+
when Date; :dates
|
190
|
+
else
|
191
|
+
raise ArgumentError, "unsupported multiple argument type '#{opts[:default][0].class.name}'"
|
192
|
+
end
|
187
193
|
end
|
188
194
|
when nil; nil
|
189
195
|
else
|
@@ -204,7 +210,7 @@ class Parser
|
|
204
210
|
raise ArgumentError, "long option name #{opts[:long].inspect} is already taken; please specify a (different) :long" if @long[opts[:long]]
|
205
211
|
|
206
212
|
## fill in :short
|
207
|
-
opts[:short] = opts[:short].to_s if opts[:short]
|
213
|
+
opts[:short] = opts[:short].to_s if opts[:short] && opts[:short] != :none
|
208
214
|
opts[:short] = case opts[:short]
|
209
215
|
when /^-(.)$/; $1
|
210
216
|
when nil, :none, /^.$/; opts[:short]
|
@@ -294,7 +300,7 @@ class Parser
|
|
294
300
|
vals = {}
|
295
301
|
required = {}
|
296
302
|
|
297
|
-
opt :version, "Print version and exit" if @version
|
303
|
+
opt :version, "Print version and exit" if @version && ! ( @specs[:version] || @long["version"])
|
298
304
|
opt :help, "Show this message" unless @specs[:help] || @long["help"]
|
299
305
|
|
300
306
|
@specs.each do |sym, opts|
|
@@ -323,10 +329,8 @@ class Parser
|
|
323
329
|
|
324
330
|
sym = nil if arg =~ /--no-/ # explicitly invalidate --no-no- arguments
|
325
331
|
|
326
|
-
|
327
|
-
|
328
|
-
raise CommandlineError, "unknown argument '#{arg}'" unless sym
|
329
|
-
end
|
332
|
+
next 0 if ignore_invalid_options && !sym
|
333
|
+
raise CommandlineError, "unknown argument '#{arg}'" unless sym
|
330
334
|
|
331
335
|
if given_args.include?(sym) && !@specs[sym][:multi]
|
332
336
|
raise CommandlineError, "option '#{arg}' specified multiple times"
|
@@ -381,7 +385,7 @@ class Parser
|
|
381
385
|
opts = @specs[sym]
|
382
386
|
if params.empty? && opts[:type] != :flag
|
383
387
|
raise CommandlineError, "option '#{arg}' needs a parameter" unless opts[:default]
|
384
|
-
params << [opts[:default]]
|
388
|
+
params << (opts[:default].kind_of?(Array) ? opts[:default].clone : [opts[:default]])
|
385
389
|
end
|
386
390
|
|
387
391
|
vals["#{sym}_given".intern] = true # mark argument as specified on the commandline
|
@@ -402,10 +406,10 @@ class Parser
|
|
402
406
|
end
|
403
407
|
|
404
408
|
if SINGLE_ARG_TYPES.include?(opts[:type])
|
405
|
-
|
406
|
-
vals[sym] = vals[sym][0][0]
|
407
|
-
else # multiple options, each with a single parameter
|
409
|
+
if opts[:multi] # multiple options, each with a single parameter
|
408
410
|
vals[sym] = vals[sym].map { |p| p[0] }
|
411
|
+
else # single parameter
|
412
|
+
vals[sym] = vals[sym][0][0]
|
409
413
|
end
|
410
414
|
elsif MULTI_ARG_TYPES.include?(opts[:type]) && !opts[:multi]
|
411
415
|
vals[sym] = vals[sym][0] # single option, with multiple parameters
|
@@ -434,7 +438,7 @@ class Parser
|
|
434
438
|
begin
|
435
439
|
require 'chronic'
|
436
440
|
time = Chronic.parse(param)
|
437
|
-
rescue
|
441
|
+
rescue LoadError
|
438
442
|
# chronic is not available
|
439
443
|
end
|
440
444
|
time ? Date.new(time.year, time.month, time.day) : Date.parse(param)
|
@@ -516,19 +520,24 @@ class Parser
|
|
516
520
|
def width #:nodoc:
|
517
521
|
@width ||= if $stdout.tty?
|
518
522
|
begin
|
519
|
-
require '
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
x
|
524
|
-
rescue Exception
|
525
|
-
80
|
523
|
+
require 'io/console'
|
524
|
+
IO.console.winsize.last
|
525
|
+
rescue LoadError, NoMethodError, Errno::ENOTTY, Errno::EBADF, Errno::EINVAL
|
526
|
+
legacy_width
|
526
527
|
end
|
527
528
|
else
|
528
529
|
80
|
529
530
|
end
|
530
531
|
end
|
531
532
|
|
533
|
+
def legacy_width
|
534
|
+
# Support for older Rubies where io/console is not available
|
535
|
+
`tput cols`.to_i
|
536
|
+
rescue Errno::ENOENT
|
537
|
+
80
|
538
|
+
end
|
539
|
+
private :legacy_width
|
540
|
+
|
532
541
|
def wrap str, opts={} # :nodoc:
|
533
542
|
if str == ""
|
534
543
|
[""]
|
@@ -562,52 +571,48 @@ private
|
|
562
571
|
|
563
572
|
until i >= args.length
|
564
573
|
if @stop_words.member? args[i]
|
565
|
-
remains += args[i .. -1]
|
566
|
-
return remains
|
574
|
+
return remains += args[i .. -1]
|
567
575
|
end
|
568
576
|
case args[i]
|
569
577
|
when /^--$/ # arg terminator
|
570
|
-
remains += args[(i + 1) .. -1]
|
571
|
-
return remains
|
578
|
+
return remains += args[(i + 1) .. -1]
|
572
579
|
when /^--(\S+?)=(.*)$/ # long argument with equals
|
573
580
|
yield "--#{$1}", [$2]
|
574
581
|
i += 1
|
575
582
|
when /^--(\S+)$/ # long argument
|
576
583
|
params = collect_argument_parameters(args, i + 1)
|
577
|
-
|
584
|
+
if params.empty?
|
585
|
+
yield args[i], nil
|
586
|
+
i += 1
|
587
|
+
else
|
578
588
|
num_params_taken = yield args[i], params
|
579
589
|
unless num_params_taken
|
580
590
|
if @stop_on_unknown
|
581
|
-
remains += args[i + 1 .. -1]
|
582
|
-
return remains
|
591
|
+
return remains += args[i + 1 .. -1]
|
583
592
|
else
|
584
593
|
remains += params
|
585
594
|
end
|
586
595
|
end
|
587
596
|
i += 1 + num_params_taken
|
588
|
-
else # long argument no parameter
|
589
|
-
yield args[i], nil
|
590
|
-
i += 1
|
591
597
|
end
|
592
598
|
when /^-(\S+)$/ # one or more short arguments
|
593
599
|
shortargs = $1.split(//)
|
594
600
|
shortargs.each_with_index do |a, j|
|
595
601
|
if j == (shortargs.length - 1)
|
596
602
|
params = collect_argument_parameters(args, i + 1)
|
597
|
-
|
603
|
+
if params.empty?
|
604
|
+
yield "-#{a}", nil
|
605
|
+
i += 1
|
606
|
+
else
|
598
607
|
num_params_taken = yield "-#{a}", params
|
599
608
|
unless num_params_taken
|
600
609
|
if @stop_on_unknown
|
601
|
-
remains += args[i + 1 .. -1]
|
602
|
-
return remains
|
610
|
+
return remains += args[i + 1 .. -1]
|
603
611
|
else
|
604
612
|
remains += params
|
605
613
|
end
|
606
614
|
end
|
607
615
|
i += 1 + num_params_taken
|
608
|
-
else # argument no parameter
|
609
|
-
yield "-#{a}", nil
|
610
|
-
i += 1
|
611
616
|
end
|
612
617
|
else
|
613
618
|
yield "-#{a}", nil
|
@@ -615,8 +620,7 @@ private
|
|
615
620
|
end
|
616
621
|
else
|
617
622
|
if @stop_on_unknown
|
618
|
-
remains += args[i .. -1]
|
619
|
-
return remains
|
623
|
+
return remains += args[i .. -1]
|
620
624
|
else
|
621
625
|
remains << args[i]
|
622
626
|
i += 1
|
@@ -662,9 +666,8 @@ private
|
|
662
666
|
|
663
667
|
def resolve_default_short_options!
|
664
668
|
@order.each do |type, name|
|
665
|
-
next unless type == :opt
|
666
669
|
opts = @specs[name]
|
667
|
-
next if opts[:short]
|
670
|
+
next if type != :opt || opts[:short]
|
668
671
|
|
669
672
|
c = opts[:long].split(//).find { |d| d !~ INVALID_SHORT_ARG_REGEX && !@short.member?(d) }
|
670
673
|
if c # found a character to use
|
data/test/test_helper.rb
CHANGED
@@ -1,14 +1,11 @@
|
|
1
1
|
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
|
2
2
|
|
3
3
|
unless ENV['MUTANT']
|
4
|
+
begin
|
4
5
|
require "coveralls"
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
Coveralls::SimpleCov::Formatter,
|
9
|
-
SimpleCov::Formatter::HTMLFormatter,
|
10
|
-
]
|
11
|
-
|
6
|
+
Coveralls.wear!
|
7
|
+
rescue LoadError
|
8
|
+
end
|
12
9
|
end
|
13
10
|
|
14
11
|
begin
|
@@ -16,8 +13,6 @@ begin
|
|
16
13
|
rescue LoadError
|
17
14
|
end
|
18
15
|
|
19
|
-
require '
|
20
|
-
|
21
|
-
SimpleCov.start unless ENV['MUTANT']
|
16
|
+
require 'minitest/autorun'
|
22
17
|
|
23
18
|
require 'trollop'
|
data/test/test_trollop.rb
CHANGED
@@ -1,33 +1,33 @@
|
|
1
1
|
require 'stringio'
|
2
|
-
|
2
|
+
require 'test_helper'
|
3
3
|
|
4
4
|
module Trollop
|
5
5
|
module Test
|
6
6
|
|
7
|
-
class Trollop < ::
|
7
|
+
class Trollop < ::MiniTest::Unit::TestCase
|
8
8
|
def setup
|
9
9
|
@p = Parser.new
|
10
10
|
end
|
11
11
|
|
12
12
|
def test_die_without_options_ever_run
|
13
13
|
::Trollop.send(:instance_variable_set, "@last_parser", nil)
|
14
|
-
|
14
|
+
assert_raises(ArgumentError) { ::Trollop.die 'hello' }
|
15
15
|
end
|
16
16
|
|
17
17
|
def test_unknown_arguments
|
18
|
-
|
18
|
+
assert_raises(CommandlineError) { @p.parse(%w(--arg)) }
|
19
19
|
@p.opt "arg"
|
20
|
-
|
21
|
-
|
20
|
+
@p.parse(%w(--arg))
|
21
|
+
assert_raises(CommandlineError) { @p.parse(%w(--arg2)) }
|
22
22
|
end
|
23
23
|
|
24
24
|
def test_syntax_check
|
25
25
|
@p.opt "arg"
|
26
26
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
27
|
+
@p.parse(%w(--arg))
|
28
|
+
@p.parse(%w(arg))
|
29
|
+
assert_raises(CommandlineError) { @p.parse(%w(---arg)) }
|
30
|
+
assert_raises(CommandlineError) { @p.parse(%w(-arg)) }
|
31
31
|
end
|
32
32
|
|
33
33
|
def test_required_flags_are_required
|
@@ -35,10 +35,10 @@ class Trollop < ::Test::Unit::TestCase
|
|
35
35
|
@p.opt "arg2", "desc", :required => false
|
36
36
|
@p.opt "arg3", "desc", :required => false
|
37
37
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
38
|
+
@p.parse(%w(--arg))
|
39
|
+
@p.parse(%w(--arg --arg2))
|
40
|
+
assert_raises(CommandlineError) { @p.parse(%w(--arg2)) }
|
41
|
+
assert_raises(CommandlineError) { @p.parse(%w(--arg2 --arg3)) }
|
42
42
|
end
|
43
43
|
|
44
44
|
## flags that take an argument error unless given one
|
@@ -46,17 +46,17 @@ class Trollop < ::Test::Unit::TestCase
|
|
46
46
|
@p.opt "goodarg", "desc", :type => String
|
47
47
|
@p.opt "goodarg2", "desc", :type => String
|
48
48
|
|
49
|
-
|
50
|
-
|
51
|
-
|
49
|
+
@p.parse(%w(--goodarg goat))
|
50
|
+
assert_raises(CommandlineError) { @p.parse(%w(--goodarg --goodarg2 goat)) }
|
51
|
+
assert_raises(CommandlineError) { @p.parse(%w(--goodarg)) }
|
52
52
|
end
|
53
53
|
|
54
54
|
## flags that don't take arguments ignore them
|
55
55
|
def test_arglessflags_refuse_args
|
56
56
|
@p.opt "goodarg"
|
57
57
|
@p.opt "goodarg2"
|
58
|
-
|
59
|
-
|
58
|
+
@p.parse(%w(--goodarg))
|
59
|
+
@p.parse(%w(--goodarg --goodarg2))
|
60
60
|
opts = @p.parse %w(--goodarg a)
|
61
61
|
assert_equal true, opts["goodarg"]
|
62
62
|
assert_equal ["a"], @p.leftovers
|
@@ -65,125 +65,121 @@ class Trollop < ::Test::Unit::TestCase
|
|
65
65
|
## flags that require args of a specific type refuse args of other
|
66
66
|
## types
|
67
67
|
def test_typed_args_refuse_args_of_other_types
|
68
|
-
|
69
|
-
|
68
|
+
@p.opt "goodarg", "desc", :type => :int
|
69
|
+
assert_raises(ArgumentError) { @p.opt "badarg", "desc", :type => :asdf }
|
70
70
|
|
71
|
-
|
72
|
-
|
73
|
-
|
71
|
+
@p.parse(%w(--goodarg 3))
|
72
|
+
assert_raises(CommandlineError) { @p.parse(%w(--goodarg 4.2)) }
|
73
|
+
assert_raises(CommandlineError) { @p.parse(%w(--goodarg hello)) }
|
74
74
|
end
|
75
75
|
|
76
76
|
## type is correctly derived from :default
|
77
77
|
def test_type_correctly_derived_from_default
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
opts = nil
|
78
|
+
assert_raises(ArgumentError) { @p.opt "badarg", "desc", :default => [] }
|
79
|
+
assert_raises(ArgumentError) { @p.opt "badarg3", "desc", :default => [{1 => 2}] }
|
80
|
+
assert_raises(ArgumentError) { @p.opt "badarg4", "desc", :default => Hash.new }
|
83
81
|
|
84
82
|
# single arg: int
|
85
|
-
|
86
|
-
|
83
|
+
@p.opt "argsi", "desc", :default => 0
|
84
|
+
opts = @p.parse(%w(--))
|
87
85
|
assert_equal 0, opts["argsi"]
|
88
|
-
|
86
|
+
opts = @p.parse(%w(--argsi 4))
|
89
87
|
assert_equal 4, opts["argsi"]
|
90
|
-
|
88
|
+
opts = @p.parse(%w(--argsi=4))
|
91
89
|
assert_equal 4, opts["argsi"]
|
92
|
-
|
90
|
+
opts = @p.parse(%w(--argsi=-4))
|
93
91
|
assert_equal -4, opts["argsi"]
|
94
92
|
|
95
|
-
|
96
|
-
|
93
|
+
assert_raises(CommandlineError) { @p.parse(%w(--argsi 4.2)) }
|
94
|
+
assert_raises(CommandlineError) { @p.parse(%w(--argsi hello)) }
|
97
95
|
|
98
96
|
# single arg: float
|
99
|
-
|
100
|
-
|
97
|
+
@p.opt "argsf", "desc", :default => 3.14
|
98
|
+
opts = @p.parse(%w(--))
|
101
99
|
assert_equal 3.14, opts["argsf"]
|
102
|
-
|
100
|
+
opts = @p.parse(%w(--argsf 2.41))
|
103
101
|
assert_equal 2.41, opts["argsf"]
|
104
|
-
|
102
|
+
opts = @p.parse(%w(--argsf 2))
|
105
103
|
assert_equal 2, opts["argsf"]
|
106
|
-
|
104
|
+
opts = @p.parse(%w(--argsf 1.0e-2))
|
107
105
|
assert_equal 1.0e-2, opts["argsf"]
|
108
|
-
|
106
|
+
assert_raises(CommandlineError) { @p.parse(%w(--argsf hello)) }
|
109
107
|
|
110
108
|
# single arg: date
|
111
109
|
date = Date.today
|
112
|
-
|
113
|
-
|
110
|
+
@p.opt "argsd", "desc", :default => date
|
111
|
+
opts = @p.parse(%w(--))
|
114
112
|
assert_equal Date.today, opts["argsd"]
|
115
|
-
|
113
|
+
opts = @p.parse(['--argsd', 'Jan 4, 2007'])
|
116
114
|
assert_equal Date.civil(2007, 1, 4), opts["argsd"]
|
117
|
-
|
115
|
+
assert_raises(CommandlineError) { @p.parse(%w(--argsd hello)) }
|
118
116
|
|
119
117
|
# single arg: string
|
120
|
-
|
121
|
-
|
118
|
+
@p.opt "argss", "desc", :default => "foobar"
|
119
|
+
opts = @p.parse(%w(--))
|
122
120
|
assert_equal "foobar", opts["argss"]
|
123
|
-
|
121
|
+
opts = @p.parse(%w(--argss 2.41))
|
124
122
|
assert_equal "2.41", opts["argss"]
|
125
|
-
|
123
|
+
opts = @p.parse(%w(--argss hello))
|
126
124
|
assert_equal "hello", opts["argss"]
|
127
125
|
|
128
126
|
# multi args: ints
|
129
|
-
|
130
|
-
|
127
|
+
@p.opt "argmi", "desc", :default => [3, 5]
|
128
|
+
opts = @p.parse(%w(--))
|
131
129
|
assert_equal [3, 5], opts["argmi"]
|
132
|
-
|
130
|
+
opts = @p.parse(%w(--argmi 4))
|
133
131
|
assert_equal [4], opts["argmi"]
|
134
|
-
|
135
|
-
|
132
|
+
assert_raises(CommandlineError) { @p.parse(%w(--argmi 4.2)) }
|
133
|
+
assert_raises(CommandlineError) { @p.parse(%w(--argmi hello)) }
|
136
134
|
|
137
135
|
# multi args: floats
|
138
|
-
|
139
|
-
|
136
|
+
@p.opt "argmf", "desc", :default => [3.34, 5.21]
|
137
|
+
opts = @p.parse(%w(--))
|
140
138
|
assert_equal [3.34, 5.21], opts["argmf"]
|
141
|
-
|
139
|
+
opts = @p.parse(%w(--argmf 2))
|
142
140
|
assert_equal [2], opts["argmf"]
|
143
|
-
|
141
|
+
opts = @p.parse(%w(--argmf 4.0))
|
144
142
|
assert_equal [4.0], opts["argmf"]
|
145
|
-
|
143
|
+
assert_raises(CommandlineError) { @p.parse(%w(--argmf hello)) }
|
146
144
|
|
147
145
|
# multi args: dates
|
148
146
|
dates = [Date.today, Date.civil(2007, 1, 4)]
|
149
|
-
|
150
|
-
|
147
|
+
@p.opt "argmd", "desc", :default => dates
|
148
|
+
opts = @p.parse(%w(--))
|
151
149
|
assert_equal dates, opts["argmd"]
|
152
|
-
|
150
|
+
opts = @p.parse(['--argmd', 'Jan 4, 2007'])
|
153
151
|
assert_equal [Date.civil(2007, 1, 4)], opts["argmd"]
|
154
|
-
|
152
|
+
assert_raises(CommandlineError) { @p.parse(%w(--argmd hello)) }
|
155
153
|
|
156
154
|
# multi args: strings
|
157
|
-
|
158
|
-
|
155
|
+
@p.opt "argmst", "desc", :default => %w(hello world)
|
156
|
+
opts = @p.parse(%w(--))
|
159
157
|
assert_equal %w(hello world), opts["argmst"]
|
160
|
-
|
158
|
+
opts = @p.parse(%w(--argmst 3.4))
|
161
159
|
assert_equal ["3.4"], opts["argmst"]
|
162
|
-
|
160
|
+
opts = @p.parse(%w(--argmst goodbye))
|
163
161
|
assert_equal ["goodbye"], opts["argmst"]
|
164
162
|
end
|
165
163
|
|
166
164
|
## :type and :default must match if both are specified
|
167
165
|
def test_type_and_default_must_match
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
166
|
+
assert_raises(ArgumentError) { @p.opt "badarg", "desc", :type => :int, :default => "hello" }
|
167
|
+
assert_raises(ArgumentError) { @p.opt "badarg2", "desc", :type => :String, :default => 4 }
|
168
|
+
assert_raises(ArgumentError) { @p.opt "badarg2", "desc", :type => :String, :default => ["hi"] }
|
169
|
+
assert_raises(ArgumentError) { @p.opt "badarg2", "desc", :type => :ints, :default => [3.14] }
|
172
170
|
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
171
|
+
@p.opt "argsi", "desc", :type => :int, :default => 4
|
172
|
+
@p.opt "argsf", "desc", :type => :float, :default => 3.14
|
173
|
+
@p.opt "argsd", "desc", :type => :date, :default => Date.today
|
174
|
+
@p.opt "argss", "desc", :type => :string, :default => "yo"
|
175
|
+
@p.opt "argmi", "desc", :type => :ints, :default => [4]
|
176
|
+
@p.opt "argmf", "desc", :type => :floats, :default => [3.14]
|
177
|
+
@p.opt "argmd", "desc", :type => :dates, :default => [Date.today]
|
178
|
+
@p.opt "argmst", "desc", :type => :strings, :default => ["yo"]
|
181
179
|
end
|
182
180
|
|
183
181
|
##
|
184
182
|
def test_flags_with_defaults_and_no_args_act_as_switches
|
185
|
-
opts = nil
|
186
|
-
|
187
183
|
@p.opt :argd, "desc", :default => "default_string"
|
188
184
|
|
189
185
|
opts = @p.parse(%w(--))
|
@@ -197,24 +193,48 @@ class Trollop < ::Test::Unit::TestCase
|
|
197
193
|
opts = @p.parse(%w(--argd different_string))
|
198
194
|
assert opts[:argd_given]
|
199
195
|
assert_equal "different_string", opts[:argd]
|
196
|
+
end
|
200
197
|
|
198
|
+
def test_flag_with_no_defaults_and_no_args_act_as_switches_array
|
199
|
+
opts = nil
|
200
|
+
|
201
|
+
@p.opt :argd, "desc", :type => :strings, :default => ["default_string"]
|
202
|
+
|
203
|
+
opts = @p.parse(%w(--argd))
|
204
|
+
assert_equal ["default_string"], opts[:argd]
|
205
|
+
end
|
206
|
+
|
207
|
+
def test_type_and_empty_array
|
208
|
+
@p.opt "argmi", "desc", :type => :ints, :default => []
|
209
|
+
@p.opt "argmf", "desc", :type => :floats, :default => []
|
210
|
+
@p.opt "argmd", "desc", :type => :dates, :default => []
|
211
|
+
@p.opt "argms", "desc", :type => :strings, :default => []
|
212
|
+
assert_raises(ArgumentError) { @p.opt "badi", "desc", :type => :int, :default => [] }
|
213
|
+
assert_raises(ArgumentError) { @p.opt "badf", "desc", :type => :float, :default => [] }
|
214
|
+
assert_raises(ArgumentError) { @p.opt "badd", "desc", :type => :date, :default => [] }
|
215
|
+
assert_raises(ArgumentError) { @p.opt "bads", "desc", :type => :string, :default => [] }
|
216
|
+
opts = @p.parse([])
|
217
|
+
assert_equal(opts["argmi"], [])
|
218
|
+
assert_equal(opts["argmf"], [])
|
219
|
+
assert_equal(opts["argmd"], [])
|
220
|
+
assert_equal(opts["argms"], [])
|
201
221
|
end
|
202
222
|
|
203
223
|
def test_long_detects_bad_names
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
224
|
+
@p.opt "goodarg", "desc", :long => "none"
|
225
|
+
@p.opt "goodarg2", "desc", :long => "--two"
|
226
|
+
assert_raises(ArgumentError) { @p.opt "badarg", "desc", :long => "" }
|
227
|
+
assert_raises(ArgumentError) { @p.opt "badarg2", "desc", :long => "--" }
|
228
|
+
assert_raises(ArgumentError) { @p.opt "badarg3", "desc", :long => "-one" }
|
229
|
+
assert_raises(ArgumentError) { @p.opt "badarg4", "desc", :long => "---toomany" }
|
210
230
|
end
|
211
231
|
|
212
232
|
def test_short_detects_bad_names
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
233
|
+
@p.opt "goodarg", "desc", :short => "a"
|
234
|
+
@p.opt "goodarg2", "desc", :short => "-b"
|
235
|
+
assert_raises(ArgumentError) { @p.opt "badarg", "desc", :short => "" }
|
236
|
+
assert_raises(ArgumentError) { @p.opt "badarg2", "desc", :short => "-ab" }
|
237
|
+
assert_raises(ArgumentError) { @p.opt "badarg3", "desc", :short => "--t" }
|
218
238
|
end
|
219
239
|
|
220
240
|
def test_short_names_created_automatically
|
@@ -231,8 +251,8 @@ class Trollop < ::Test::Unit::TestCase
|
|
231
251
|
@p.opt :arg # auto: a
|
232
252
|
@p.opt :arg_potato # auto: r
|
233
253
|
@p.opt :arg_muffin # auto: g
|
234
|
-
|
235
|
-
|
254
|
+
@p.opt :arg_daisy # auto: d (not _)!
|
255
|
+
@p.opt :arg_r2d2f # auto: f (not 2)!
|
236
256
|
|
237
257
|
opts = @p.parse %w(-f -d)
|
238
258
|
assert_equal true, opts[:arg_daisy]
|
@@ -247,38 +267,36 @@ class Trollop < ::Test::Unit::TestCase
|
|
247
267
|
@p.opt :arg2 # auto: r
|
248
268
|
@p.opt :arg3 # auto: g
|
249
269
|
@p.opt :arg4 # auto: uh oh!
|
250
|
-
|
270
|
+
@p.parse []
|
251
271
|
end
|
252
272
|
|
253
273
|
def test_short_can_be_nothing
|
254
|
-
|
255
|
-
|
256
|
-
@p.parse []
|
257
|
-
end
|
274
|
+
@p.opt "arg", "desc", :short => :none
|
275
|
+
@p.parse []
|
258
276
|
|
259
277
|
sio = StringIO.new "w"
|
260
278
|
@p.educate sio
|
261
279
|
assert sio.string =~ /--arg\s+desc/
|
262
280
|
|
263
|
-
|
281
|
+
assert_raises(CommandlineError) { @p.parse %w(-a) }
|
264
282
|
end
|
265
283
|
|
266
284
|
## two args can't have the same name
|
267
285
|
def test_conflicting_names_are_detected
|
268
|
-
|
269
|
-
|
286
|
+
@p.opt "goodarg"
|
287
|
+
assert_raises(ArgumentError) { @p.opt "goodarg" }
|
270
288
|
end
|
271
289
|
|
272
290
|
## two args can't have the same :long
|
273
291
|
def test_conflicting_longs_detected
|
274
|
-
|
275
|
-
|
292
|
+
@p.opt "goodarg", "desc", :long => "--goodarg"
|
293
|
+
assert_raises(ArgumentError) { @p.opt "badarg", "desc", :long => "--goodarg" }
|
276
294
|
end
|
277
295
|
|
278
296
|
## two args can't have the same :short
|
279
297
|
def test_conflicting_shorts_detected
|
280
|
-
|
281
|
-
|
298
|
+
@p.opt "goodarg", "desc", :short => "-g"
|
299
|
+
assert_raises(ArgumentError) { @p.opt "badarg", "desc", :short => "-g" }
|
282
300
|
end
|
283
301
|
|
284
302
|
## note: this behavior has changed in trollop 2.0!
|
@@ -332,13 +350,13 @@ class Trollop < ::Test::Unit::TestCase
|
|
332
350
|
assert_equal false, opts[:no_default_true]
|
333
351
|
|
334
352
|
## disallow double negatives for reasons of sanity preservation
|
335
|
-
|
353
|
+
assert_raises(CommandlineError) { @p.parse %w(--no-no-default-true) }
|
336
354
|
end
|
337
355
|
|
338
356
|
def test_special_flags_work
|
339
357
|
@p.version "asdf fdas"
|
340
|
-
|
341
|
-
|
358
|
+
assert_raises(VersionNeeded) { @p.parse(%w(-v)) }
|
359
|
+
assert_raises(HelpNeeded) { @p.parse(%w(-h)) }
|
342
360
|
end
|
343
361
|
|
344
362
|
def test_short_options_combine
|
@@ -346,18 +364,17 @@ class Trollop < ::Test::Unit::TestCase
|
|
346
364
|
@p.opt :arg2, "desc", :short => "b"
|
347
365
|
@p.opt :arg3, "desc", :short => "c", :type => :int
|
348
366
|
|
349
|
-
opts =
|
350
|
-
assert_nothing_raised { opts = @p.parse %w(-a -b) }
|
367
|
+
opts = @p.parse %w(-a -b)
|
351
368
|
assert_equal true, opts[:arg1]
|
352
369
|
assert_equal true, opts[:arg2]
|
353
370
|
assert_equal nil, opts[:arg3]
|
354
371
|
|
355
|
-
|
372
|
+
opts = @p.parse %w(-ab)
|
356
373
|
assert_equal true, opts[:arg1]
|
357
374
|
assert_equal true, opts[:arg2]
|
358
375
|
assert_equal nil, opts[:arg3]
|
359
376
|
|
360
|
-
|
377
|
+
opts = @p.parse %w(-ac 4 -b)
|
361
378
|
assert_equal true, opts[:arg1]
|
362
379
|
assert_equal true, opts[:arg2]
|
363
380
|
assert_equal 4, opts[:arg3]
|
@@ -368,24 +385,23 @@ class Trollop < ::Test::Unit::TestCase
|
|
368
385
|
|
369
386
|
def test_version_only_appears_if_set
|
370
387
|
@p.opt "arg"
|
371
|
-
|
388
|
+
assert_raises(CommandlineError) { @p.parse %w(-v) }
|
372
389
|
@p.version "trollop 1.2.3.4"
|
373
|
-
|
390
|
+
assert_raises(VersionNeeded) { @p.parse %w(-v) }
|
374
391
|
end
|
375
392
|
|
376
393
|
def test_doubledash_ends_option_processing
|
377
394
|
@p.opt :arg1, "desc", :short => "a", :default => 0
|
378
395
|
@p.opt :arg2, "desc", :short => "b", :default => 0
|
379
|
-
opts =
|
380
|
-
assert_nothing_raised { opts = @p.parse %w(-- -a 3 -b 2) }
|
396
|
+
opts = @p.parse %w(-- -a 3 -b 2)
|
381
397
|
assert_equal opts[:arg1], 0
|
382
398
|
assert_equal opts[:arg2], 0
|
383
399
|
assert_equal %w(-a 3 -b 2), @p.leftovers
|
384
|
-
|
400
|
+
opts = @p.parse %w(-a 3 -- -b 2)
|
385
401
|
assert_equal opts[:arg1], 3
|
386
402
|
assert_equal opts[:arg2], 0
|
387
403
|
assert_equal %w(-b 2), @p.leftovers
|
388
|
-
|
404
|
+
opts = @p.parse %w(-a 3 -b 2 --)
|
389
405
|
assert_equal opts[:arg1], 3
|
390
406
|
assert_equal opts[:arg2], 2
|
391
407
|
assert_equal %w(), @p.leftovers
|
@@ -428,24 +444,23 @@ Options:
|
|
428
444
|
|
429
445
|
def test_floating_point_formatting
|
430
446
|
@p.opt :arg, "desc", :type => :float, :short => "f"
|
431
|
-
opts =
|
432
|
-
assert_nothing_raised { opts = @p.parse %w(-f 1) }
|
447
|
+
opts = @p.parse %w(-f 1)
|
433
448
|
assert_equal 1.0, opts[:arg]
|
434
|
-
|
449
|
+
opts = @p.parse %w(-f 1.0)
|
435
450
|
assert_equal 1.0, opts[:arg]
|
436
|
-
|
451
|
+
opts = @p.parse %w(-f 0.1)
|
437
452
|
assert_equal 0.1, opts[:arg]
|
438
|
-
|
453
|
+
opts = @p.parse %w(-f .1)
|
439
454
|
assert_equal 0.1, opts[:arg]
|
440
|
-
|
455
|
+
opts = @p.parse %w(-f .99999999999999999999)
|
441
456
|
assert_equal 1.0, opts[:arg]
|
442
|
-
|
457
|
+
opts = @p.parse %w(-f -1)
|
443
458
|
assert_equal(-1.0, opts[:arg])
|
444
|
-
|
459
|
+
opts = @p.parse %w(-f -1.0)
|
445
460
|
assert_equal(-1.0, opts[:arg])
|
446
|
-
|
461
|
+
opts = @p.parse %w(-f -0.1)
|
447
462
|
assert_equal(-0.1, opts[:arg])
|
448
|
-
|
463
|
+
opts = @p.parse %w(-f -.1)
|
449
464
|
assert_equal(-0.1, opts[:arg])
|
450
465
|
assert_raises(CommandlineError) { @p.parse %w(-f a) }
|
451
466
|
assert_raises(CommandlineError) { @p.parse %w(-f 1a) }
|
@@ -458,10 +473,9 @@ Options:
|
|
458
473
|
|
459
474
|
def test_date_formatting
|
460
475
|
@p.opt :arg, "desc", :type => :date, :short => 'd'
|
461
|
-
opts =
|
462
|
-
assert_nothing_raised { opts = @p.parse(['-d', 'Jan 4, 2007']) }
|
476
|
+
opts = @p.parse(['-d', 'Jan 4, 2007'])
|
463
477
|
assert_equal Date.civil(2007, 1, 4), opts[:arg]
|
464
|
-
|
478
|
+
opts = @p.parse(['-d', 'today'])
|
465
479
|
assert_equal Date.today, opts[:arg]
|
466
480
|
end
|
467
481
|
|
@@ -469,96 +483,82 @@ Options:
|
|
469
483
|
assert_raises(ArgumentError) { @p.opt :arg, "desc", :short => "-1" }
|
470
484
|
@p.opt :a1b, "desc"
|
471
485
|
@p.opt :a2b, "desc"
|
472
|
-
|
486
|
+
assert @p.specs[:a2b][:short].to_i == 0
|
473
487
|
end
|
474
488
|
|
475
489
|
def test_short_options_can_be_weird
|
476
|
-
|
477
|
-
|
490
|
+
@p.opt :arg1, "desc", :short => "#"
|
491
|
+
@p.opt :arg2, "desc", :short => "."
|
478
492
|
assert_raises(ArgumentError) { @p.opt :arg3, "desc", :short => "-" }
|
479
493
|
end
|
480
494
|
|
481
495
|
def test_options_cant_be_set_multiple_times_if_not_specified
|
482
496
|
@p.opt :arg, "desc", :short => "-x"
|
483
|
-
|
497
|
+
@p.parse %w(-x)
|
484
498
|
assert_raises(CommandlineError) { @p.parse %w(-x -x) }
|
485
499
|
assert_raises(CommandlineError) { @p.parse %w(-xx) }
|
486
500
|
end
|
487
501
|
|
488
502
|
def test_options_can_be_set_multiple_times_if_specified
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
assert_nothing_raised { @p.parse %w(-x -x) }
|
494
|
-
assert_nothing_raised { @p.parse %w(-xx) }
|
503
|
+
@p.opt :arg, "desc", :short => "-x", :multi => true
|
504
|
+
@p.parse %w(-x)
|
505
|
+
@p.parse %w(-x -x)
|
506
|
+
@p.parse %w(-xx)
|
495
507
|
end
|
496
508
|
|
497
509
|
def test_short_options_with_multiple_options
|
498
|
-
|
499
|
-
|
500
|
-
assert_nothing_raised do
|
501
|
-
@p.opt :xarg, "desc", :short => "-x", :type => String, :multi => true
|
502
|
-
end
|
503
|
-
assert_nothing_raised { opts = @p.parse %w(-x a -x b) }
|
510
|
+
@p.opt :xarg, "desc", :short => "-x", :type => String, :multi => true
|
511
|
+
opts = @p.parse %w(-x a -x b)
|
504
512
|
assert_equal %w(a b), opts[:xarg]
|
505
513
|
assert_equal [], @p.leftovers
|
506
514
|
end
|
507
515
|
|
508
516
|
def test_short_options_with_multiple_options_does_not_affect_flags_type
|
509
|
-
|
510
|
-
|
511
|
-
assert_nothing_raised do
|
512
|
-
@p.opt :xarg, "desc", :short => "-x", :type => :flag, :multi => true
|
513
|
-
end
|
517
|
+
@p.opt :xarg, "desc", :short => "-x", :type => :flag, :multi => true
|
514
518
|
|
515
|
-
|
519
|
+
opts = @p.parse %w(-x a)
|
516
520
|
assert_equal true, opts[:xarg]
|
517
521
|
assert_equal %w(a), @p.leftovers
|
518
522
|
|
519
|
-
|
523
|
+
opts = @p.parse %w(-x a -x b)
|
520
524
|
assert_equal true, opts[:xarg]
|
521
525
|
assert_equal %w(a b), @p.leftovers
|
522
526
|
|
523
|
-
|
527
|
+
opts = @p.parse %w(-xx a -x b)
|
524
528
|
assert_equal true, opts[:xarg]
|
525
529
|
assert_equal %w(a b), @p.leftovers
|
526
530
|
end
|
527
531
|
|
528
532
|
def test_short_options_with_multiple_arguments
|
529
|
-
opts = nil
|
530
|
-
|
531
533
|
@p.opt :xarg, "desc", :type => :ints
|
532
|
-
|
534
|
+
opts = @p.parse %w(-x 3 4 0)
|
533
535
|
assert_equal [3, 4, 0], opts[:xarg]
|
534
536
|
assert_equal [], @p.leftovers
|
535
537
|
|
536
538
|
@p.opt :yarg, "desc", :type => :floats
|
537
|
-
|
539
|
+
opts = @p.parse %w(-y 3.14 4.21 0.66)
|
538
540
|
assert_equal [3.14, 4.21, 0.66], opts[:yarg]
|
539
541
|
assert_equal [], @p.leftovers
|
540
542
|
|
541
543
|
@p.opt :zarg, "desc", :type => :strings
|
542
|
-
|
544
|
+
opts = @p.parse %w(-z a b c)
|
543
545
|
assert_equal %w(a b c), opts[:zarg]
|
544
546
|
assert_equal [], @p.leftovers
|
545
547
|
end
|
546
548
|
|
547
549
|
def test_short_options_with_multiple_options_and_arguments
|
548
|
-
opts = nil
|
549
|
-
|
550
550
|
@p.opt :xarg, "desc", :type => :ints, :multi => true
|
551
|
-
|
551
|
+
opts = @p.parse %w(-x 3 4 5 -x 6 7)
|
552
552
|
assert_equal [[3, 4, 5], [6, 7]], opts[:xarg]
|
553
553
|
assert_equal [], @p.leftovers
|
554
554
|
|
555
555
|
@p.opt :yarg, "desc", :type => :floats, :multi => true
|
556
|
-
|
556
|
+
opts = @p.parse %w(-y 3.14 4.21 5.66 -y 6.99 7.01)
|
557
557
|
assert_equal [[3.14, 4.21, 5.66], [6.99, 7.01]], opts[:yarg]
|
558
558
|
assert_equal [], @p.leftovers
|
559
559
|
|
560
560
|
@p.opt :zarg, "desc", :type => :strings, :multi => true
|
561
|
-
|
561
|
+
opts = @p.parse %w(-z a b c -z d e)
|
562
562
|
assert_equal [%w(a b c), %w(d e)], opts[:zarg]
|
563
563
|
assert_equal [], @p.leftovers
|
564
564
|
end
|
@@ -569,14 +569,12 @@ Options:
|
|
569
569
|
@p.opt :arg3, "desc", :short => "c", :type => :ints
|
570
570
|
@p.opt :arg4, "desc", :short => "d", :type => :floats
|
571
571
|
|
572
|
-
opts =
|
573
|
-
|
574
|
-
assert_nothing_raised { opts = @p.parse %w(-abc 4 6 9) }
|
572
|
+
opts = @p.parse %w(-abc 4 6 9)
|
575
573
|
assert_equal true, opts[:arg1]
|
576
574
|
assert_equal true, opts[:arg2]
|
577
575
|
assert_equal [4, 6, 9], opts[:arg3]
|
578
576
|
|
579
|
-
|
577
|
+
opts = @p.parse %w(-ac 4 6 9 -bd 3.14 2.41)
|
580
578
|
assert_equal true, opts[:arg1]
|
581
579
|
assert_equal true, opts[:arg2]
|
582
580
|
assert_equal [4, 6, 9], opts[:arg3]
|
@@ -587,79 +585,73 @@ Options:
|
|
587
585
|
|
588
586
|
def test_long_options_with_multiple_options
|
589
587
|
@p.opt :xarg, "desc", :type => String, :multi => true
|
590
|
-
opts =
|
591
|
-
assert_nothing_raised { opts = @p.parse %w(--xarg=a --xarg=b) }
|
588
|
+
opts = @p.parse %w(--xarg=a --xarg=b)
|
592
589
|
assert_equal %w(a b), opts[:xarg]
|
593
590
|
assert_equal [], @p.leftovers
|
594
|
-
|
591
|
+
opts = @p.parse %w(--xarg a --xarg b)
|
595
592
|
assert_equal %w(a b), opts[:xarg]
|
596
593
|
assert_equal [], @p.leftovers
|
597
594
|
end
|
598
595
|
|
599
596
|
def test_long_options_with_multiple_arguments
|
600
|
-
opts = nil
|
601
|
-
|
602
597
|
@p.opt :xarg, "desc", :type => :ints
|
603
|
-
|
598
|
+
opts = @p.parse %w(--xarg 3 2 5)
|
604
599
|
assert_equal [3, 2, 5], opts[:xarg]
|
605
600
|
assert_equal [], @p.leftovers
|
606
|
-
|
601
|
+
opts = @p.parse %w(--xarg=3)
|
607
602
|
assert_equal [3], opts[:xarg]
|
608
603
|
assert_equal [], @p.leftovers
|
609
604
|
|
610
605
|
@p.opt :yarg, "desc", :type => :floats
|
611
|
-
|
606
|
+
opts = @p.parse %w(--yarg 3.14 2.41 5.66)
|
612
607
|
assert_equal [3.14, 2.41, 5.66], opts[:yarg]
|
613
608
|
assert_equal [], @p.leftovers
|
614
|
-
|
609
|
+
opts = @p.parse %w(--yarg=3.14)
|
615
610
|
assert_equal [3.14], opts[:yarg]
|
616
611
|
assert_equal [], @p.leftovers
|
617
612
|
|
618
613
|
@p.opt :zarg, "desc", :type => :strings
|
619
|
-
|
614
|
+
opts = @p.parse %w(--zarg a b c)
|
620
615
|
assert_equal %w(a b c), opts[:zarg]
|
621
616
|
assert_equal [], @p.leftovers
|
622
|
-
|
617
|
+
opts = @p.parse %w(--zarg=a)
|
623
618
|
assert_equal %w(a), opts[:zarg]
|
624
619
|
assert_equal [], @p.leftovers
|
625
620
|
end
|
626
621
|
|
627
622
|
def test_long_options_with_multiple_options_and_arguments
|
628
|
-
opts = nil
|
629
|
-
|
630
623
|
@p.opt :xarg, "desc", :type => :ints, :multi => true
|
631
|
-
|
624
|
+
opts = @p.parse %w(--xarg 3 2 5 --xarg 2 1)
|
632
625
|
assert_equal [[3, 2, 5], [2, 1]], opts[:xarg]
|
633
626
|
assert_equal [], @p.leftovers
|
634
|
-
|
627
|
+
opts = @p.parse %w(--xarg=3 --xarg=2)
|
635
628
|
assert_equal [[3], [2]], opts[:xarg]
|
636
629
|
assert_equal [], @p.leftovers
|
637
630
|
|
638
631
|
@p.opt :yarg, "desc", :type => :floats, :multi => true
|
639
|
-
|
632
|
+
opts = @p.parse %w(--yarg 3.14 2.72 5 --yarg 2.41 1.41)
|
640
633
|
assert_equal [[3.14, 2.72, 5], [2.41, 1.41]], opts[:yarg]
|
641
634
|
assert_equal [], @p.leftovers
|
642
|
-
|
635
|
+
opts = @p.parse %w(--yarg=3.14 --yarg=2.41)
|
643
636
|
assert_equal [[3.14], [2.41]], opts[:yarg]
|
644
637
|
assert_equal [], @p.leftovers
|
645
638
|
|
646
639
|
@p.opt :zarg, "desc", :type => :strings, :multi => true
|
647
|
-
|
640
|
+
opts = @p.parse %w(--zarg a b c --zarg d e)
|
648
641
|
assert_equal [%w(a b c), %w(d e)], opts[:zarg]
|
649
642
|
assert_equal [], @p.leftovers
|
650
|
-
|
643
|
+
opts = @p.parse %w(--zarg=a --zarg=d)
|
651
644
|
assert_equal [%w(a), %w(d)], opts[:zarg]
|
652
645
|
assert_equal [], @p.leftovers
|
653
646
|
end
|
654
647
|
|
655
648
|
def test_long_options_also_take_equals
|
656
649
|
@p.opt :arg, "desc", :long => "arg", :type => String, :default => "hello"
|
657
|
-
opts =
|
658
|
-
assert_nothing_raised { opts = @p.parse %w() }
|
650
|
+
opts = @p.parse %w()
|
659
651
|
assert_equal "hello", opts[:arg]
|
660
|
-
|
652
|
+
opts = @p.parse %w(--arg goat)
|
661
653
|
assert_equal "goat", opts[:arg]
|
662
|
-
|
654
|
+
opts = @p.parse %w(--arg=goat)
|
663
655
|
assert_equal "goat", opts[:arg]
|
664
656
|
## actually, this next one is valid. empty string for --arg, and goat as a
|
665
657
|
## leftover.
|
@@ -797,26 +789,26 @@ Options:
|
|
797
789
|
@p.opt :hello, "desc", :short => "-h"
|
798
790
|
@p.version "version"
|
799
791
|
|
800
|
-
|
792
|
+
@p.parse(%w(-v))
|
801
793
|
assert_raises(VersionNeeded) { @p.parse(%w(--version)) }
|
802
|
-
|
794
|
+
@p.parse(%w(-h))
|
803
795
|
assert_raises(HelpNeeded) { @p.parse(%w(--help)) }
|
804
796
|
end
|
805
797
|
|
806
798
|
def test_version_and_help_long_args_can_be_overridden
|
807
799
|
@p.opt :asdf, "desc", :long => "help"
|
808
800
|
@p.opt :asdf2, "desc2", :long => "version"
|
809
|
-
|
810
|
-
|
811
|
-
|
812
|
-
|
813
|
-
|
801
|
+
@p.parse %w()
|
802
|
+
@p.parse %w(--help)
|
803
|
+
@p.parse %w(--version)
|
804
|
+
@p.parse %w(-h)
|
805
|
+
@p.parse %w(-v)
|
814
806
|
end
|
815
807
|
|
816
808
|
def test_version_and_help_override_errors
|
817
809
|
@p.opt :asdf, "desc", :type => String
|
818
810
|
@p.version "version"
|
819
|
-
|
811
|
+
@p.parse %w(--asdf goat)
|
820
812
|
assert_raises(CommandlineError) { @p.parse %w(--asdf) }
|
821
813
|
assert_raises(HelpNeeded) { @p.parse %w(--asdf --help) }
|
822
814
|
assert_raises(VersionNeeded) { @p.parse %w(--asdf --version) }
|
@@ -826,9 +818,9 @@ Options:
|
|
826
818
|
@p.opt :one
|
827
819
|
assert_raises(ArgumentError) { @p.conflicts :one, :two }
|
828
820
|
@p.opt :two
|
829
|
-
|
830
|
-
|
831
|
-
|
821
|
+
@p.conflicts :one, :two
|
822
|
+
@p.parse %w(--one)
|
823
|
+
@p.parse %w(--two)
|
832
824
|
assert_raises(CommandlineError) { @p.parse %w(--one --two) }
|
833
825
|
|
834
826
|
@p.opt :hello
|
@@ -840,13 +832,13 @@ Options:
|
|
840
832
|
assert_raises(CommandlineError) { @p.parse %w(--hello --mellow --jello) }
|
841
833
|
assert_raises(CommandlineError) { @p.parse %w(--hello --jello) }
|
842
834
|
|
843
|
-
|
844
|
-
|
845
|
-
|
846
|
-
|
835
|
+
@p.parse %w(--hello)
|
836
|
+
@p.parse %w(--jello)
|
837
|
+
@p.parse %w(--yellow)
|
838
|
+
@p.parse %w(--mellow)
|
847
839
|
|
848
|
-
|
849
|
-
|
840
|
+
@p.parse %w(--mellow --one)
|
841
|
+
@p.parse %w(--mellow --two)
|
850
842
|
|
851
843
|
assert_raises(CommandlineError) { @p.parse %w(--mellow --two --jello) }
|
852
844
|
assert_raises(CommandlineError) { @p.parse %w(--one --mellow --two --jello) }
|
@@ -857,21 +849,15 @@ Options:
|
|
857
849
|
@p.opt "two"
|
858
850
|
@p.conflicts :one, "two"
|
859
851
|
|
860
|
-
|
861
|
-
@p.parse %w(--one --two)
|
862
|
-
flunk "no error thrown"
|
863
|
-
rescue CommandlineError => e
|
864
|
-
assert_match(/--one/, e.message)
|
865
|
-
assert_match(/--two/, e.message)
|
866
|
-
end
|
852
|
+
assert_raises(CommandlineError, /--one.*--two/) { @p.parse %w(--one --two) }
|
867
853
|
end
|
868
854
|
|
869
855
|
def test_depends
|
870
856
|
@p.opt :one
|
871
857
|
assert_raises(ArgumentError) { @p.depends :one, :two }
|
872
858
|
@p.opt :two
|
873
|
-
|
874
|
-
|
859
|
+
@p.depends :one, :two
|
860
|
+
@p.parse %w(--one --two)
|
875
861
|
assert_raises(CommandlineError) { @p.parse %w(--one) }
|
876
862
|
assert_raises(CommandlineError) { @p.parse %w(--two) }
|
877
863
|
|
@@ -880,15 +866,15 @@ Options:
|
|
880
866
|
@p.opt :mellow
|
881
867
|
@p.opt :jello
|
882
868
|
@p.depends :hello, :yellow, :mellow, :jello
|
883
|
-
|
869
|
+
@p.parse %w(--hello --yellow --mellow --jello)
|
884
870
|
assert_raises(CommandlineError) { @p.parse %w(--hello --mellow --jello) }
|
885
871
|
assert_raises(CommandlineError) { @p.parse %w(--hello --jello) }
|
886
872
|
|
887
873
|
assert_raises(CommandlineError) { @p.parse %w(--hello) }
|
888
874
|
assert_raises(CommandlineError) { @p.parse %w(--mellow) }
|
889
875
|
|
890
|
-
|
891
|
-
|
876
|
+
@p.parse %w(--hello --yellow --mellow --jello --one --two)
|
877
|
+
@p.parse %w(--hello --yellow --mellow --jello --one --two a b c)
|
892
878
|
|
893
879
|
assert_raises(CommandlineError) { @p.parse %w(--mellow --two --jello --one) }
|
894
880
|
end
|
@@ -898,23 +884,10 @@ Options:
|
|
898
884
|
@p.opt "two"
|
899
885
|
@p.depends :one, "two"
|
900
886
|
|
901
|
-
|
902
|
-
|
903
|
-
begin
|
904
|
-
@p.parse %w(--one)
|
905
|
-
flunk "no error thrown"
|
906
|
-
rescue CommandlineError => e
|
907
|
-
assert_match(/--one/, e.message)
|
908
|
-
assert_match(/--two/, e.message)
|
909
|
-
end
|
887
|
+
@p.parse %w(--one --two)
|
910
888
|
|
911
|
-
|
912
|
-
|
913
|
-
flunk "no error thrown"
|
914
|
-
rescue CommandlineError => e
|
915
|
-
assert_match(/--one/, e.message)
|
916
|
-
assert_match(/--two/, e.message)
|
917
|
-
end
|
889
|
+
assert_raises(CommandlineError, /--one.*--two/) { @p.parse %w(--one) }
|
890
|
+
assert_raises(CommandlineError, /--one.*--two/) { @p.parse %w(--two) }
|
918
891
|
end
|
919
892
|
|
920
893
|
## courtesy neill zero
|
@@ -922,21 +895,9 @@ Options:
|
|
922
895
|
@p.opt "arg1", "desc1", :required => true
|
923
896
|
@p.opt "arg2", "desc2", :required => true
|
924
897
|
|
925
|
-
|
926
|
-
|
927
|
-
|
928
|
-
rescue CommandlineError => e
|
929
|
-
assert e.message =~ /arg2/, "didn't mention arg2 in the error msg: #{e.message}"
|
930
|
-
end
|
931
|
-
|
932
|
-
begin
|
933
|
-
@p.parse(%w(--arg2))
|
934
|
-
flunk "should have failed on a missing req"
|
935
|
-
rescue CommandlineError => e
|
936
|
-
assert e.message =~ /arg1/, "didn't mention arg1 in the error msg: #{e.message}"
|
937
|
-
end
|
938
|
-
|
939
|
-
assert_nothing_raised { @p.parse(%w(--arg1 --arg2)) }
|
898
|
+
assert_raises(CommandlineError, /arg2/) { @p.parse(%w(--arg1)) }
|
899
|
+
assert_raises(CommandlineError, /arg1/) { @p.parse(%w(--arg2)) }
|
900
|
+
@p.parse(%w(--arg1 --arg2))
|
940
901
|
end
|
941
902
|
|
942
903
|
def test_stopwords_mixed
|
@@ -1064,21 +1025,20 @@ Options:
|
|
1064
1025
|
@p.opt :arg2, 'desc', :type => Date
|
1065
1026
|
@p.opt :arg3, 'desc', :default => temp
|
1066
1027
|
|
1067
|
-
opts =
|
1068
|
-
assert_nothing_raised { opts = @p.parse }
|
1028
|
+
opts = @p.parse []
|
1069
1029
|
assert_equal temp, opts[:arg3]
|
1070
1030
|
|
1071
|
-
|
1031
|
+
opts = @p.parse %w(--arg 5/1/2010)
|
1072
1032
|
assert_kind_of Date, opts[:arg]
|
1073
1033
|
assert_equal Date.new(2010, 5, 1), opts[:arg]
|
1074
1034
|
|
1075
|
-
|
1035
|
+
opts = @p.parse %w(--arg2 5/1/2010)
|
1076
1036
|
assert_kind_of Date, opts[:arg2]
|
1077
1037
|
assert_equal Date.new(2010, 5, 1), opts[:arg2]
|
1078
1038
|
end
|
1079
1039
|
|
1080
1040
|
def test_unknown_arg_class_type
|
1081
|
-
|
1041
|
+
assert_raises ArgumentError do
|
1082
1042
|
@p.opt :arg, 'desc', :type => Hash
|
1083
1043
|
end
|
1084
1044
|
end
|
@@ -1088,19 +1048,18 @@ Options:
|
|
1088
1048
|
@p.opt :arg2, "desc", :type => IO
|
1089
1049
|
@p.opt :arg3, "desc", :default => $stdout
|
1090
1050
|
|
1091
|
-
opts =
|
1092
|
-
assert_nothing_raised { opts = @p.parse() }
|
1051
|
+
opts = @p.parse []
|
1093
1052
|
assert_equal $stdout, opts[:arg3]
|
1094
1053
|
|
1095
|
-
|
1054
|
+
opts = @p.parse %w(--arg /dev/null)
|
1096
1055
|
assert_kind_of File, opts[:arg]
|
1097
1056
|
assert_equal "/dev/null", opts[:arg].path
|
1098
1057
|
|
1099
1058
|
#TODO: move to mocks
|
1100
|
-
#
|
1059
|
+
#opts = @p.parse %w(--arg2 http://google.com/)
|
1101
1060
|
#assert_kind_of StringIO, opts[:arg2]
|
1102
1061
|
|
1103
|
-
|
1062
|
+
opts = @p.parse %w(--arg3 stdin)
|
1104
1063
|
assert_equal $stdin, opts[:arg3]
|
1105
1064
|
|
1106
1065
|
assert_raises(CommandlineError) { opts = @p.parse %w(--arg /fdasfasef/fessafef/asdfasdfa/fesasf) }
|
@@ -1112,8 +1071,8 @@ Options:
|
|
1112
1071
|
|
1113
1072
|
opts = @p.parse(%w(--arg1 3 --arg2 4))
|
1114
1073
|
|
1115
|
-
|
1116
|
-
|
1074
|
+
opts.arg1
|
1075
|
+
opts.arg2
|
1117
1076
|
assert_equal 3, opts.arg1
|
1118
1077
|
assert_equal 4, opts.arg2
|
1119
1078
|
end
|
@@ -1122,7 +1081,7 @@ Options:
|
|
1122
1081
|
@p.opt :arg1, "desc", :default => "hello", :multi => true
|
1123
1082
|
@p.opt :arg2, "desc", :default => ["hello"], :multi => true
|
1124
1083
|
|
1125
|
-
opts = @p.parse
|
1084
|
+
opts = @p.parse []
|
1126
1085
|
assert_equal ["hello"], opts[:arg1]
|
1127
1086
|
assert_equal ["hello"], opts[:arg2]
|
1128
1087
|
|
@@ -1227,7 +1186,6 @@ Options:
|
|
1227
1186
|
::Trollop::options do
|
1228
1187
|
opt :potato
|
1229
1188
|
end
|
1230
|
-
raise "broken"
|
1231
1189
|
end
|
1232
1190
|
end
|
1233
1191
|
|
@@ -1239,18 +1197,14 @@ Options:
|
|
1239
1197
|
version "1.2"
|
1240
1198
|
opt :potato
|
1241
1199
|
end
|
1242
|
-
raise "broken"
|
1243
1200
|
end
|
1244
1201
|
end
|
1245
1202
|
|
1246
1203
|
def test_simple_interface_handles_regular_usage
|
1247
1204
|
ARGV.clear
|
1248
1205
|
ARGV.unshift "--potato"
|
1249
|
-
opts =
|
1250
|
-
|
1251
|
-
opts = ::Trollop::options do
|
1252
|
-
opt :potato
|
1253
|
-
end
|
1206
|
+
opts = ::Trollop::options do
|
1207
|
+
opt :potato
|
1254
1208
|
end
|
1255
1209
|
assert opts[:potato]
|
1256
1210
|
end
|
@@ -1281,7 +1235,7 @@ Options:
|
|
1281
1235
|
end
|
1282
1236
|
|
1283
1237
|
def test_with_standard_exception_handling
|
1284
|
-
|
1238
|
+
assert_raises(SystemExit) do
|
1285
1239
|
::Trollop.with_standard_exception_handling(@p) do
|
1286
1240
|
raise ::Trollop::CommandlineError.new('cl error')
|
1287
1241
|
end
|