optparse-pathname
This gem allow you convert command line options to Pathname
objects
when parsing them using OptionParser
.
It's similar to optparse/uri
, a Ruby standard bundled library.
Installation
Add this line to your application's Gemfile:
gem 'optparse-pathname'
And then execute:
$ bundle
Or install it yourself as:
$ gem install optparse-pathname
Usage
Your command file(bin/my-command here):
#!/usr/bin/env ruby
require 'optparse/pathname'
= {}
parser = OptionParser.new
parser.on '-f', '--file=FILE', Pathname do |path|
unless path.file?
warn 'ERROR: `file` option must be an existing file'
raise OptionParser::InvalidArgument
end
[:path] = path
end
parser.parse!
puts [:path].class
puts [:path].
You will see like this:
$ bundle exec my-command --file=./bin/my-command
Pathname
path/to/current/bin/my-command
Or like this:
$ bundle exec bin/my-command --file=.
ERROR: `file` option must be an existing file
bin/my-command:9:in `block in <main>': invalid argument: --file=. (OptionParser::InvalidArgument)
from bin/my-command:13:in `<main>'
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Merge Request