Options by Example

No-code options parser that automatically detects command-line options from the usage text.

Features

  • Automatically infers options and argument names from usage text
  • Parses those arguments and options from the command line (ARGV)
  • Raises errors for unknown options or missing required arguments
  • Supports typed arguments, eg --lines NUM or --since DATE

Example

require %(options_by_example)

flags = OptionsByExample.read(DATA).parse(ARGV)

puts 'Feeling verbose today' if flags.include_verbose?
puts flags.get_words.sample(flags.get_num)

__END__

And then call the program with eg

ruby random.rb -n 2 foo bar qux

Installation

To use options_by_example, first install the gem by running:

gem install options_by_example

Alternatively, add this line to your Gemfile and run bundle install:

gem 'options_by_example'