optparse-range 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gemtest +0 -0
- data/.gitignore +17 -0
- data/.travis.yml +3 -0
- data/BSDL +22 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +56 -0
- data/README.markdown +142 -0
- data/Rakefile +6 -0
- data/lib/optparse/range.rb +77 -0
- data/optparse-range.gemspec +14 -0
- data/test/test_range.rb +54 -0
- data/test/test_range.rb~ +1 -0
- metadata +66 -0
data/.gemtest
ADDED
File without changes
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/BSDL
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (C) 2012 KITAITI Makoto. All rights reserved.
|
2
|
+
|
3
|
+
Redistribution and use in source and binary forms, with or without
|
4
|
+
modification, are permitted provided that the following conditions
|
5
|
+
are met:
|
6
|
+
1. Redistributions of source code must retain the above copyright
|
7
|
+
notice, this list of conditions and the following disclaimer.
|
8
|
+
2. Redistributions in binary form must reproduce the above copyright
|
9
|
+
notice, this list of conditions and the following disclaimer in the
|
10
|
+
documentation and/or other materials provided with the distribution.
|
11
|
+
|
12
|
+
THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
13
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
14
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
15
|
+
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
16
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
17
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
18
|
+
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
19
|
+
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
20
|
+
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
21
|
+
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
22
|
+
SUCH DAMAGE.
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
OptionParser::Range is copyrighted free software by KITAITI Makoto <KitaitiMakoto@gmail.com>.
|
2
|
+
You can redistribute it and/or modify it under either the terms of the
|
3
|
+
2-clause BSDL (see the file BSDL), or the conditions below:
|
4
|
+
|
5
|
+
1. You may make and give away verbatim copies of the source form of the
|
6
|
+
software without restriction, provided that you duplicate all of the
|
7
|
+
original copyright notices and associated disclaimers.
|
8
|
+
|
9
|
+
2. You may modify your copy of the software in any way, provided that
|
10
|
+
you do at least ONE of the following:
|
11
|
+
|
12
|
+
a) place your modifications in the Public Domain or otherwise
|
13
|
+
make them Freely Available, such as by posting said
|
14
|
+
modifications to Usenet or an equivalent medium, or by allowing
|
15
|
+
the author to include your modifications in the software.
|
16
|
+
|
17
|
+
b) use the modified software only within your corporation or
|
18
|
+
organization.
|
19
|
+
|
20
|
+
c) give non-standard binaries non-standard names, with
|
21
|
+
instructions on where to get the original software distribution.
|
22
|
+
|
23
|
+
d) make other distribution arrangements with the author.
|
24
|
+
|
25
|
+
3. You may distribute the software in object code or binary form,
|
26
|
+
provided that you do at least ONE of the following:
|
27
|
+
|
28
|
+
a) distribute the binaries and library files of the software,
|
29
|
+
together with instructions (in the manual page or equivalent)
|
30
|
+
on where to get the original distribution.
|
31
|
+
|
32
|
+
b) accompany the distribution with the machine-readable source of
|
33
|
+
the software.
|
34
|
+
|
35
|
+
c) give non-standard binaries non-standard names, with
|
36
|
+
instructions on where to get the original software distribution.
|
37
|
+
|
38
|
+
d) make other distribution arrangements with the author.
|
39
|
+
|
40
|
+
4. You may modify and include the part of the software into any other
|
41
|
+
software (possibly commercial). But some files in the distribution
|
42
|
+
are not written by the author, so that they are not under these terms.
|
43
|
+
|
44
|
+
For the list of those files and their copying conditions, see the
|
45
|
+
file LEGAL.
|
46
|
+
|
47
|
+
5. The scripts and library files supplied as input to or produced as
|
48
|
+
output from the software do not automatically fall under the
|
49
|
+
copyright of the software, but belong to whomever generated them,
|
50
|
+
and may be sold commercially, and may be aggregated with this
|
51
|
+
software.
|
52
|
+
|
53
|
+
6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
|
54
|
+
IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
55
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
56
|
+
PURPOSE.
|
data/README.markdown
ADDED
@@ -0,0 +1,142 @@
|
|
1
|
+
OptionParser::Range
|
2
|
+
===================
|
3
|
+
|
4
|
+
This RubyGem allows standard bundled `OptionParser` to accept option arguments as `Range` object.
|
5
|
+
|
6
|
+
Inspired by [Slop][slop] gem's [Range][sloprange] feature
|
7
|
+
|
8
|
+
Note that `OptionParser::Range` module doesn't exist.
|
9
|
+
|
10
|
+
Installation
|
11
|
+
------------
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
gem 'optparse-range'
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install optparse-range
|
24
|
+
|
25
|
+
Usage
|
26
|
+
-----
|
27
|
+
|
28
|
+
require 'optparse/range'
|
29
|
+
|
30
|
+
opts = OptionParser.new do |opti|
|
31
|
+
opti.on '--page=STARTPAGE-ENDPAGE', OptionParser::DecimalRange
|
32
|
+
end.getopts
|
33
|
+
# for instance, when --page=024-160 passed
|
34
|
+
opts #=> {"page"=>24..160}
|
35
|
+
opts['page'].class #=> Range
|
36
|
+
|
37
|
+
When only one argument passed, it handled as both start and end:
|
38
|
+
|
39
|
+
opts = OptionParser.new do |opti|
|
40
|
+
opti.on '--port=STARTPORT-ENDPORT', OptionParser::DecimalRange
|
41
|
+
end
|
42
|
+
# when --port=8080
|
43
|
+
opts #=> {"port"=>8080..8080}
|
44
|
+
|
45
|
+
You can replace `OptionParser::DecimalRange` in above example with other type of `Range`.
|
46
|
+
Currently provided:
|
47
|
+
|
48
|
+
### `OptionParser::DecimalRange` ###
|
49
|
+
|
50
|
+
* `Integer` range
|
51
|
+
* `--number=1-5` => `1..5`
|
52
|
+
* only zero or positive integers are supported currently
|
53
|
+
|
54
|
+
### `OptionParser::FloatRange` ###
|
55
|
+
|
56
|
+
* `Float` range
|
57
|
+
* `--ratio=1.2-1.5` => `1.2..1.5`
|
58
|
+
* only zero or positive floats are supported currently
|
59
|
+
|
60
|
+
### `OptionParser::DateRange` ###
|
61
|
+
* `Date` range
|
62
|
+
* `--trip=0810-0820` => `#<Date: 2012-08-10 ((2456150j,0s,0n),+0s,2299161j)>..#<Date: 2012-08-20 ((2456160j,0s,0n),+0s,2299161j)>`
|
63
|
+
|
64
|
+
### `OptionParser::DateTimeRange` ###
|
65
|
+
|
66
|
+
* `DateTime` range
|
67
|
+
* `--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)>`
|
68
|
+
|
69
|
+
### `OptionParser::TimeRange` ###
|
70
|
+
|
71
|
+
* `Time` range
|
72
|
+
* `--flight=10:00-12:20` => `>2012-07-29 10:00:00 +0900..2012-07-29 12:20:00 +0900`
|
73
|
+
|
74
|
+
### `OptionParser::StringRange` ###
|
75
|
+
|
76
|
+
* `String` range
|
77
|
+
* `--chars=A-Z` => `"A".."Z"`
|
78
|
+
|
79
|
+
Adding Type of Rnage
|
80
|
+
--------------------
|
81
|
+
|
82
|
+
To add a type of range, use `OptionParser.accept_range`.
|
83
|
+
|
84
|
+
1. Define class which can be used as points of `Range`(`MyClass` here).
|
85
|
+
2. Define class or `Regexp` which allows `OptionParser` to accept class above as the points of `Range`(`MyClassRange`). Class is recommended.
|
86
|
+
3. Pass the class and `Symbol` or `Proc` which converts a command line option(`String`) to an object of point(`MyClass`). `lambda` is used here. `to_proc` will be called when `Symbol` is passed.
|
87
|
+
4. Now, you can pass the class for range(`MyClassRange`) to `OptionParser#on` and use the option argument as `Range`.
|
88
|
+
|
89
|
+
Example:
|
90
|
+
|
91
|
+
require 'optparse/range'
|
92
|
+
|
93
|
+
class MyClass
|
94
|
+
class << self
|
95
|
+
def parse(arg)
|
96
|
+
# parse arg and return MyClass object
|
97
|
+
new(arg)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
attr_reader :arg
|
102
|
+
|
103
|
+
def initialize(arg)
|
104
|
+
@arg = arg
|
105
|
+
end
|
106
|
+
|
107
|
+
def <=>(other)
|
108
|
+
@arg <=> other.arg
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
class MyClassRange; end
|
113
|
+
OptionParser.accept_range MyClassRange, lambda {|range| MyClass.parse range}
|
114
|
+
|
115
|
+
opts = OptionParser.new do |opti|
|
116
|
+
opti.on '--myoption=MYSTART-MYEND', MyClassRange
|
117
|
+
end.getopts
|
118
|
+
p opts
|
119
|
+
|
120
|
+
Result:
|
121
|
+
|
122
|
+
$ ./mycommand --myoption=mystart-myend
|
123
|
+
{"myoption"=>#<MyClass:0x000000031824c0 @arg="mystart">..#<MyClass:0x00000003182420 @arg="myend">}
|
124
|
+
|
125
|
+
Contributing
|
126
|
+
------------
|
127
|
+
|
128
|
+
1. Fork it
|
129
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
130
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
131
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
132
|
+
5. Create new Pull Request
|
133
|
+
|
134
|
+
License
|
135
|
+
-------
|
136
|
+
|
137
|
+
OptionParser::Range is released under the Ruby's license.
|
138
|
+
See the file LICENSE.txt.
|
139
|
+
|
140
|
+
<!-- Links -->
|
141
|
+
[slop]: https://github.com/injekt/slop
|
142
|
+
[sloprange]: https://github.com/injekt/slop/wiki/Ranges
|
data/Rakefile
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'optparse'
|
2
|
+
|
3
|
+
class OptionParser
|
4
|
+
class << self
|
5
|
+
def accept_range(accepter, converter)
|
6
|
+
accept accepter do |range,|
|
7
|
+
return unless range
|
8
|
+
terms = range.split('-')
|
9
|
+
raise AmbiguousArgument if terms.length > 2
|
10
|
+
terms << terms.first if terms.length == 1
|
11
|
+
Range.new *terms.map(&converter)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
decimal = '\d+(?:_\d+)*'
|
17
|
+
DecimalRange = /#{decimal}(?:\-#{decimal})/io
|
18
|
+
accept_range DecimalRange, :to_i
|
19
|
+
|
20
|
+
float = "(?:#{decimal}(?:\\.(?:#{decimal})?)?|\\.#{decimal})(?:E[-+]?#{decimal})?"
|
21
|
+
FloatRange = /#{float}-#{float}/io
|
22
|
+
accept_range FloatRange, :to_f
|
23
|
+
|
24
|
+
class DateRange
|
25
|
+
class << self
|
26
|
+
attr_reader :converter
|
27
|
+
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
|
+
end
|
39
|
+
accept_range DateRange, DateRange.converter
|
40
|
+
|
41
|
+
class DateTimeRange
|
42
|
+
class << self
|
43
|
+
attr_reader :converter
|
44
|
+
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
|
+
end
|
56
|
+
accept_range DateTimeRange, DateTimeRange.converter
|
57
|
+
|
58
|
+
class TimeRange
|
59
|
+
class << self
|
60
|
+
attr_reader :converter
|
61
|
+
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
|
+
end
|
73
|
+
accept_range TimeRange, TimeRange.converter
|
74
|
+
|
75
|
+
class StringRange; end
|
76
|
+
accept_range StringRange, :to_s
|
77
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
Gem::Specification.new do |gem|
|
2
|
+
gem.name = "optparse-range"
|
3
|
+
gem.version = '0.0.1'
|
4
|
+
gem.authors = ["KITAITI Makoto"]
|
5
|
+
gem.email = ["KitaitiMakoto@gmail.com"]
|
6
|
+
gem.description = %q{This RubyGem allows standard bundled `OptionParser` to accept option arguments as `Range` object.}
|
7
|
+
gem.summary = %q{Range command line arguments handling}
|
8
|
+
gem.homepage = "https://github.com/KitaitiMakoto/optparse-range"
|
9
|
+
|
10
|
+
gem.files = `git ls-files`.split($/)
|
11
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
12
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
13
|
+
gem.require_paths = ["lib"]
|
14
|
+
end
|
data/test/test_range.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
|
2
|
+
require 'test/unit'
|
3
|
+
require 'optparse/range'
|
4
|
+
|
5
|
+
class TestOptionParserRange < Test::Unit::TestCase
|
6
|
+
def setup
|
7
|
+
@opt = OptionParser.new
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_decimal_range
|
11
|
+
assert_equal 81..123, parse_option(OptionParser::DecimalRange, '081-123')
|
12
|
+
assert_equal 1..1, parse_option(OptionParser::DecimalRange, '1')
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_float_range
|
16
|
+
assert_equal (1.2)..(64.33), parse_option(OptionParser::FloatRange, '01.20-64.33')
|
17
|
+
assert_equal (30.0)..(30.0), parse_option(OptionParser::FloatRange, '30')
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_date_range
|
21
|
+
actual = parse_option(OptionParser::DateRange, '0701-0731')
|
22
|
+
assert_equal Date.parse('0701')..Date.parse('0731'), actual
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_date_time_range
|
26
|
+
actual = parse_option(OptionParser::DateTimeRange, '20120601T00:00:00-20120630T24:00:00')
|
27
|
+
assert_equal DateTime.parse('20120601T00:00:00')..DateTime.parse('20120630T24:00:00'),
|
28
|
+
actual
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_time_range
|
32
|
+
actual = parse_option(OptionParser::TimeRange, '04:30-19:12:14')
|
33
|
+
dawn = '4:30'
|
34
|
+
dawn = Time.httpdate(dawn) rescue Time.parse(dawn)
|
35
|
+
dusk = '19:12:14'
|
36
|
+
dusk = Time.httpdate(dusk) rescue Time.parse(dusk)
|
37
|
+
assert_equal dawn..dusk, actual
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_string_range
|
41
|
+
assert_equal 'A'..'Z', parse_option(OptionParser::StringRange, 'A-Z')
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def parse_option(accepter, arg)
|
47
|
+
opts = {}
|
48
|
+
@opt.def_option "--range=START-END", accepter do |range|
|
49
|
+
opts['range'] = range
|
50
|
+
end
|
51
|
+
@opt.parse! ["--range=#{arg}"]
|
52
|
+
opts['range']
|
53
|
+
end
|
54
|
+
end
|
data/test/test_range.rb~
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'test/unit'
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: optparse-range
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- KITAITI Makoto
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-07-28 00:00:00.000000000Z
|
13
|
+
dependencies: []
|
14
|
+
description: This RubyGem allows standard bundled `OptionParser` to accept option
|
15
|
+
arguments as `Range` object.
|
16
|
+
email:
|
17
|
+
- KitaitiMakoto@gmail.com
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- .gemtest
|
23
|
+
- .gitignore
|
24
|
+
- .travis.yml
|
25
|
+
- BSDL
|
26
|
+
- Gemfile
|
27
|
+
- LICENSE.txt
|
28
|
+
- README.markdown
|
29
|
+
- Rakefile
|
30
|
+
- lib/optparse/range.rb
|
31
|
+
- optparse-range.gemspec
|
32
|
+
- test/test_range.rb
|
33
|
+
- test/test_range.rb~
|
34
|
+
homepage: https://github.com/KitaitiMakoto/optparse-range
|
35
|
+
licenses: []
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options: []
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
segments:
|
47
|
+
- 0
|
48
|
+
hash: 714120662123402692
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
segments:
|
56
|
+
- 0
|
57
|
+
hash: 714120662123402692
|
58
|
+
requirements: []
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 1.8.8
|
61
|
+
signing_key:
|
62
|
+
specification_version: 3
|
63
|
+
summary: Range command line arguments handling
|
64
|
+
test_files:
|
65
|
+
- test/test_range.rb
|
66
|
+
- test/test_range.rb~
|