OptionParser::Range
This RubyGem allows standard bundled OptionParser to accept option arguments as Range object.
Inspired by Slop gem's Range feature.
Note that OptionParser::Range module doesn't exist.
Installation
Add this line to your application's Gemfile:
gem 'optparse-range'
And then execute:
$ bundle
Or install it yourself as:
$ gem install optparse-range
Usage
require 'optparse/range'
opts = OptionParser.new do |opti|
opti.on '--page=STARTPAGE-ENDPAGE', OptionParser::DecimalIntegerRange
end.getopts
# for instance, when --page=024-160 passed
opts #=> {"page"=>24..160}
opts['page'].class #=> Range
When only one argument passed, it will be handled as both start and end:
opts = OptionParser.new do |opti|
opti.on '--port=STARTPORT-ENDPORT', OptionParser::DecimalIntegerRange
end
# when --port=8080
opts #=> {"port"=>8080..8080}
You can replace OptionParser::DecimalIntegerRange in above example with other type of Range.
See below for types currently provided.
Supported Types
OptionParser::DecimalIntegerRange
- decimal
Integerrange --number=1-5=>1..5- only zero or positive integers are supported currently
OptionParser::FloatRange
Floatrange--ratio=1.2-1.5=>1.2..1.5- only zero or positive floats are supported currently
OptionParser::DateRange
Daterange--trip=0810-0820=>#<Date: 2012-08-10 ((2456150j,0s,0n),+0s,2299161j)>..#<Date: 2012-08-20 ((2456160j,0s,0n),+0s,2299161j)>- format including hyphen is not supported currently
--trip=2012-08-10-2012-08-20=>OptionParser::AmbiguousArgumentraised
OptionParser::DateTimeRange
DateTimerange--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)>- format including hyphen is not supported currently
--trip=2012-08-10T10:00:00-2012-08-20T19:30:00=>OptionParser::AmbiguousArgumentraised
OptionParser::TimeRange
Timerange--flight=10:00-12:20=>>2012-07-29 10:00:00 +0900..2012-07-29 12:20:00 +0900- format including hyphen is not supported currently
--trip=2012-08-10T10:00:00-2012-08-20T19:30:00=>OptionParser::AmbiguousArgumentraised
OptionParser::StringRange
Stringrange--password-chars=A-Z=>"A".."Z"
Adding Type of Rnage
To add a type of range, use OptionParser.accept_range.
- Define class which can be used as points of
Range(MyClasshere). - Define class or
Regexpwhich allowsOptionParserto accept class above as the points ofRange(MyClassRange). Class is recommended. - Pass the class and block,
SymbolorProcwhich converts a point in command line option(String) to an object of point(MyClass). Block is used here.to_procwill be called whenSymbolis passed. - Now, you can pass the class for range(
MyClassRange) toOptionParser#onand use the option argument asRange.
Example:
require 'optparse/range'
class MyClass
class << self
def parse(arg)
# parse arg and return MyClass object
new(arg)
end
end
attr_reader :arg
def initialize(arg)
@arg = arg
end
def <=>(other)
@arg <=> other.arg
end
end
class MyClassRange; end
OptionParser.accept_range MyClassRange do |point_of_range|
MyClass.parse point_of_range
end
opts = OptionParser.new do |opti|
opti.on '--myoption=MYSTART-MYEND', MyClassRange
end.getopts
p opts
Result:
$ ./mycommand --myoption=mystart-myend
{"myoption"=>#<MyClass:0x000000031824c0 @arg="mystart">..#<MyClass:0x00000003182420 @arg="myend">}
Todos
- More comments for YARD
- Negative numbers
- More classes
OptioniParser#accept_range- List of range(--page=1-3,6-7)
- API for
Rangeexcluding the end point(1...5in result) - Range with single point, such like --age=-18(under 18) or --page=192-(all after page 192)
- Alternate delimiters
- Parsing
Datesuch like "2012-07-23"(including "-")
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create new Pull Request
Links
License
OptionParser::Range is released under the Ruby's license. See the file LICENSE.txt.
