Argy Build Status Coverage Status

Yet another command-line option parser.

Installation

Add this line to your application's Gemfile:

gem 'argy'

And then execute:

$ bundle

Or install it yourself as:

$ gem install argy

Usage

Here's an example:

require "argy"

parser = Argy.new do |o|
  o.description "Prints messages"
  o.usage "example"
  o.example "$ example hello"

  o.argument :message, desc: "message to print", required: true

  o.option :loud, type: :boolean, desc: "say the message loudly"
  o.option :count, type: :integer, desc: "number of times to print", default: 1

  o.on "-v", "print the verison and exit" do
    puts Example::VERSION
    exit
  end

  o.on "-h", "--help", "show this help and exit" do
    puts o.help
    puts o.help.section "SECTION"
    puts o.help.entry "foo", desc: "bar"
    exit
  end
end

begin
  options = parser.parse(ARGV)
  message = options.message
  message = message.upcase if options.loud?
  options.count.times { puts message }
rescue Argy::Error => err
  abort err.message
end

Option Types

Argy supports the following option types:

  • :string
  • :boolean
  • :integer
  • :float
  • :array
  • :pathname

However, Argy also supports custom option types. For example:

class NameOption
  def self.call(input)
    parts = input.split(" ")
    raise Argy::CoersionError, "Invalid name" if parts.length != 2
    new(*parts)
  end

  def initialize(first, last)
    @first = first
    @last = last
  end
end

Argy.new do |o|
  o.option :name, type: NameOption
end

An option type is anything that responds to call. So, your option type could just be a lambda.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/rzane/argy. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Argy project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.