A tiny option parser inspired by slop.
Original idea is taken from Slop option parser (https://github.com/leejarvis/slop).
I make a simpler syntax for my needs. Many thanks to Lee Jarvis.
Options parses somes words:
- banner : banner add text to the help message. Help message is displayed with implicit option -h|--help|?|help.
- flag : Linux style boolean option. For example [--verbose] sets verbose flag true. Short option is the first letter of the long option name.
- value : Linux style option. Next argument is the value of the option. For example [--name pyer] means name='pyer'. Short option is the first letter of the long option name, as flag option.
- command : Git style sub-commands, For example [programm status --verbose] means programm executes status command in verbose mode.
- cmd : alias of command
opts = Options.parse do
banner 'Some help text'
value 'name', 'Your name'
value 'password', 'Your password'
flag 'verbose', 'Enable verbose mode'
end
# if ARGV is `--name Lee -v`
opts.verbose? #=> true
opts.password? #=> false
opts.other? #=> false
opts.name? #=> true
opts[:name] #=> 'Lee'
opts.to_hash #=> {:name=>"Lee", :password=>nil, :verbose=>true}
gem install pyer-options
Building pyer-options gem is a rake task.
rake build
rake test
Options attempts to build a good looking help string to print to your users.
You can get this string by calling opts.help
method.
The options '-h', '--help' and '?' are available and show this help.
Example:
# example.rb
require 'pyer/options'
opts = Options.parse do
banner 'Hello world'
flag :foo, 'Enable foo mode'
end
ruby example.rb --help ("ruby example.rb -h" or "ruby example.rb ?" give the same result)
Usage: example.rb [options]
Hello world
Options:
-f|--foo : Enable foo mode