optparse-range 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +28 -12
- data/lib/optparse/range.rb +29 -46
- data/optparse-range.gemspec +1 -1
- data/test/test_range.rb +6 -1
- metadata +6 -8
- data/test/test_range.rb~ +0 -1
data/README.markdown
CHANGED
@@ -45,35 +45,43 @@ When only one argument passed, it will be handled as both start and end:
|
|
45
45
|
opts #=> {"port"=>8080..8080}
|
46
46
|
|
47
47
|
You can replace `OptionParser::DecimalIntegerRange` in above example with other type of `Range`.
|
48
|
-
|
48
|
+
See below for types currently provided.
|
49
49
|
|
50
|
-
###
|
50
|
+
### Supported Types ###
|
51
|
+
|
52
|
+
#### `OptionParser::DecimalIntegerRange` ####
|
51
53
|
|
52
54
|
* decimal `Integer` range
|
53
55
|
* `--number=1-5` => `1..5`
|
54
56
|
* only zero or positive integers are supported currently
|
55
57
|
|
56
|
-
|
58
|
+
#### `OptionParser::FloatRange` ####
|
57
59
|
|
58
60
|
* `Float` range
|
59
61
|
* `--ratio=1.2-1.5` => `1.2..1.5`
|
60
62
|
* only zero or positive floats are supported currently
|
61
63
|
|
62
|
-
|
64
|
+
#### `OptionParser::DateRange` ####
|
63
65
|
* `Date` range
|
64
66
|
* `--trip=0810-0820` => `#<Date: 2012-08-10 ((2456150j,0s,0n),+0s,2299161j)>..#<Date: 2012-08-20 ((2456160j,0s,0n),+0s,2299161j)>`
|
67
|
+
* format including hyphen is not supported currently
|
68
|
+
* `--trip=2012-08-10-2012-08-20` => `OptionParser::AmbiguousArgument` raised
|
65
69
|
|
66
|
-
|
70
|
+
#### `OptionParser::DateTimeRange` ####
|
67
71
|
|
68
72
|
* `DateTime` range
|
69
73
|
* `--flight=0810T10:00-0810T12:20` => `#<DateTime: 2012-08-10T10:00:00+00:00 ((2456150j,36000s,0n),+0s,2299161j)>..#<DateTime: 2012-08-10T12:20:00+00:00 ((2456150j,44400s,0n),+0s,2299161j)>`
|
74
|
+
* format including hyphen is not supported currently
|
75
|
+
* `--trip=2012-08-10T10:00:00-2012-08-20T19:30:00` => `OptionParser::AmbiguousArgument` raised
|
70
76
|
|
71
|
-
|
77
|
+
#### `OptionParser::TimeRange` ####
|
72
78
|
|
73
79
|
* `Time` range
|
74
80
|
* `--flight=10:00-12:20` => `>2012-07-29 10:00:00 +0900..2012-07-29 12:20:00 +0900`
|
81
|
+
* format including hyphen is not supported currently
|
82
|
+
* `--trip=2012-08-10T10:00:00-2012-08-20T19:30:00` => `OptionParser::AmbiguousArgument` raised
|
75
83
|
|
76
|
-
|
84
|
+
#### `OptionParser::StringRange` ####
|
77
85
|
|
78
86
|
* `String` range
|
79
87
|
* `--password-chars=A-Z` => `"A".."Z"`
|
@@ -84,9 +92,13 @@ Adding Type of Rnage
|
|
84
92
|
To add a type of range, use `OptionParser.accept_range`.
|
85
93
|
|
86
94
|
1. Define class which can be used as points of `Range`(`MyClass` here).
|
87
|
-
2. Define class or `Regexp` which allows `OptionParser` to accept class above
|
88
|
-
|
89
|
-
|
95
|
+
2. Define class or `Regexp` which allows `OptionParser` to accept class above
|
96
|
+
as the points of `Range`(`MyClassRange`). Class is recommended.
|
97
|
+
3. Pass the class and block, `Symbol` or `Proc` which
|
98
|
+
converts a point in command line option(`String`) to an object of point(`MyClass`).
|
99
|
+
Block is used here. `to_proc` will be called when `Symbol` is passed.
|
100
|
+
4. Now, you can pass the class for range(`MyClassRange`) to `OptionParser#on`
|
101
|
+
and use the option argument as `Range`.
|
90
102
|
|
91
103
|
Example:
|
92
104
|
|
@@ -112,7 +124,9 @@ Example:
|
|
112
124
|
end
|
113
125
|
|
114
126
|
class MyClassRange; end
|
115
|
-
OptionParser.accept_range MyClassRange
|
127
|
+
OptionParser.accept_range MyClassRange do |point_of_range|
|
128
|
+
MyClass.parse point_of_range
|
129
|
+
end
|
116
130
|
|
117
131
|
opts = OptionParser.new do |opti|
|
118
132
|
opti.on '--myoption=MYSTART-MYEND', MyClassRange
|
@@ -134,6 +148,8 @@ Todos
|
|
134
148
|
* List of range(--page=1-3,6-7)
|
135
149
|
* API for `Range` excluding the end point(`1...5` in result)
|
136
150
|
* Range with single point, such like --age=-18(under 18) or --page=192-(all after page 192)
|
151
|
+
* Alternate delimiters
|
152
|
+
* Parsing `Date` such like "2012-07-23"(including "-")
|
137
153
|
|
138
154
|
Contributing
|
139
155
|
------------
|
@@ -148,7 +164,7 @@ Links
|
|
148
164
|
-----
|
149
165
|
|
150
166
|
* [Homepage and source code][repo]
|
151
|
-
* [
|
167
|
+
* [API Documentation][rubydoc]
|
152
168
|
|
153
169
|
[optparse]: http://rubydoc.info/stdlib/optparse/frames
|
154
170
|
[slop]: https://github.com/injekt/slop
|
data/lib/optparse/range.rb
CHANGED
@@ -2,12 +2,13 @@ require 'optparse'
|
|
2
2
|
|
3
3
|
class OptionParser
|
4
4
|
class << self
|
5
|
-
def accept_range(accepter, converter)
|
5
|
+
def accept_range(accepter, converter=nil, &block)
|
6
6
|
accept accepter do |range,|
|
7
7
|
return range unless range
|
8
8
|
points = range.split('-')
|
9
9
|
raise AmbiguousArgument if points.length > 2
|
10
10
|
points << points.first if points.length == 1
|
11
|
+
converter = block if block
|
11
12
|
Range.new *points.map(&converter)
|
12
13
|
end
|
13
14
|
end
|
@@ -21,57 +22,39 @@ class OptionParser
|
|
21
22
|
FloatRange = /#{float}-#{float}/io
|
22
23
|
accept_range FloatRange, :to_f
|
23
24
|
|
24
|
-
class
|
25
|
-
|
26
|
-
|
25
|
+
class StringRange; end
|
26
|
+
accept_range StringRange, :to_s
|
27
|
+
|
28
|
+
class DateRange; end
|
29
|
+
accept_range DateRange do |date|
|
30
|
+
begin
|
31
|
+
Date.parse date
|
32
|
+
rescue NameError
|
33
|
+
retry if require 'date'
|
34
|
+
rescue ArgumentError
|
35
|
+
raise InvalidArgument, date
|
27
36
|
end
|
28
|
-
@converter = lambda {|date|
|
29
|
-
begin
|
30
|
-
Date.parse date
|
31
|
-
rescue NameError
|
32
|
-
require 'date'
|
33
|
-
retry
|
34
|
-
rescue ArgumentError
|
35
|
-
raise InvalidArgument, date
|
36
|
-
end
|
37
|
-
}
|
38
37
|
end
|
39
|
-
accept_range DateRange, DateRange.converter
|
40
38
|
|
41
|
-
class DateTimeRange
|
42
|
-
|
43
|
-
|
39
|
+
class DateTimeRange; end
|
40
|
+
accept_range DateTimeRange do |dt|
|
41
|
+
begin
|
42
|
+
DateTime.parse dt
|
43
|
+
rescue NameError
|
44
|
+
retry if require 'date'
|
45
|
+
rescue ArgumentError
|
46
|
+
raise InvalidArgument, dt
|
44
47
|
end
|
45
|
-
@converter = lambda {|dt|
|
46
|
-
begin
|
47
|
-
DateTime.parse dt
|
48
|
-
rescue NameError
|
49
|
-
require 'date'
|
50
|
-
retry
|
51
|
-
rescue ArgumentError
|
52
|
-
raise InvalidArgument, dt
|
53
|
-
end
|
54
|
-
}
|
55
48
|
end
|
56
|
-
accept_range DateTimeRange, DateTimeRange.converter
|
57
49
|
|
58
|
-
class TimeRange
|
59
|
-
|
60
|
-
|
50
|
+
class TimeRange; end
|
51
|
+
accept_range TimeRange do |time|
|
52
|
+
begin
|
53
|
+
Time.httpdate(time) rescue Time.parse(time)
|
54
|
+
rescue NoMethodError
|
55
|
+
retry if require 'time'
|
56
|
+
rescue
|
57
|
+
raise InvalidArgument, time
|
61
58
|
end
|
62
|
-
@converter = lambda {|time|
|
63
|
-
begin
|
64
|
-
Time.httpdate(time) rescue Time.parse(time)
|
65
|
-
rescue NoMethodError
|
66
|
-
require 'time'
|
67
|
-
retry
|
68
|
-
rescue
|
69
|
-
raise InvalidArgument, time
|
70
|
-
end
|
71
|
-
}
|
72
59
|
end
|
73
|
-
accept_range TimeRange, TimeRange.converter
|
74
|
-
|
75
|
-
class StringRange; end
|
76
|
-
accept_range StringRange, :to_s
|
77
60
|
end
|
data/optparse-range.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |gem|
|
2
2
|
gem.name = "optparse-range"
|
3
|
-
gem.version = '0.0.
|
3
|
+
gem.version = '0.0.3'
|
4
4
|
gem.authors = ["KITAITI Makoto"]
|
5
5
|
gem.email = ["KitaitiMakoto@gmail.com"]
|
6
6
|
gem.description = %q{This RubyGem allows standard bundled `OptionParser` to accept option arguments as `Range` object.}
|
data/test/test_range.rb
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
|
2
1
|
require 'test/unit'
|
3
2
|
require 'optparse/range'
|
4
3
|
|
@@ -41,6 +40,12 @@ class TestOptionParserRange < Test::Unit::TestCase
|
|
41
40
|
assert_equal 'A'..'Z', parse_option(OptionParser::StringRange, 'A-Z')
|
42
41
|
end
|
43
42
|
|
43
|
+
def test_ambiguous_argument
|
44
|
+
assert_raise OptionParser::AmbiguousArgument do
|
45
|
+
parse_option OptionParser::DateRange, '2012-07-31-2012-08-01'
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
44
49
|
private
|
45
50
|
|
46
51
|
def parse_option(accepter, arg)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: optparse-range
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-08-08 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
16
|
-
requirement: &
|
16
|
+
requirement: &15293440 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *15293440
|
25
25
|
description: This RubyGem allows standard bundled `OptionParser` to accept option
|
26
26
|
arguments as `Range` object.
|
27
27
|
email:
|
@@ -41,7 +41,6 @@ files:
|
|
41
41
|
- lib/optparse/range.rb
|
42
42
|
- optparse-range.gemspec
|
43
43
|
- test/test_range.rb
|
44
|
-
- test/test_range.rb~
|
45
44
|
homepage: https://github.com/KitaitiMakoto/optparse-range
|
46
45
|
licenses:
|
47
46
|
- Ruby
|
@@ -58,7 +57,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
58
57
|
version: '0'
|
59
58
|
segments:
|
60
59
|
- 0
|
61
|
-
hash: -
|
60
|
+
hash: -114764199510757300
|
62
61
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
62
|
none: false
|
64
63
|
requirements:
|
@@ -67,7 +66,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
67
66
|
version: '0'
|
68
67
|
segments:
|
69
68
|
- 0
|
70
|
-
hash: -
|
69
|
+
hash: -114764199510757300
|
71
70
|
requirements: []
|
72
71
|
rubyforge_project:
|
73
72
|
rubygems_version: 1.8.8
|
@@ -76,4 +75,3 @@ specification_version: 3
|
|
76
75
|
summary: Range command line arguments handling
|
77
76
|
test_files:
|
78
77
|
- test/test_range.rb
|
79
|
-
- test/test_range.rb~
|
data/test/test_range.rb~
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
require 'test/unit'
|