Project

argy

0.0
No commit activity in last 3 years
No release in over 3 years
Yet another command-line option parser.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 2.0
~> 13.0
~> 3.0
 Project Readme

Argy

Build Version

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.