No commit activity in last 3 years
No release in over 3 years
Simple command line argument parsing
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

~> 1.15
~> 5.0
~> 10.0
 Project Readme

Command-line argument parser

A small library for parsing command-line arguments in Ruby.

Designed to be used with command-line tools like git which have a set of subcommands that each have different options and switches.

On rubygems.org

Sample

require "cmdline_arg_parser"

# Define a parser using the DSL
class ParserFromDsl
  extend CmdlineArgParser::Dsl

  subcommand "merge" do
    option "branches", multiple: true, short_key: "b"
    option "into", default: "master"
    option("from-step") { |value| value.to_i }
    switch "dry-run"
  end

  subcommand "version"
end

# Have some ARGV
ARGV = ["merge", "--dry-run", "--from-step", "10", "-b", "release-branch", "other-branch"]

args = ParserFromDsl.parse(ARGV)

# Accessing the parsed data
args.subcommand # => "merge"
args.options # => { "into" => "master", "branches" => ["release-branch", "other-branch"], "from-step" => 10 }
args.switches # => Set.new(["dry-run"])

If you don't want to use the DSL, the above parser can also be written as:

parser = CmdlineArgParser::Parser.new(
  subcommands: [
    CmdlineArgParser::Parser::Subcommand.new(
      "merge",
      options: [
        CmdlineArgParser::Parser::Option.new("branches", multiple: true, short_key: "b"),
        CmdlineArgParser::Parser::Option.new("into", default: "master"),
        CmdlineArgParser::Parser::Option.new("from-step") { |value| value.to_i },
      ],
      switches: [
        CmdlineArgParser::Parser::Switch.new("dry-run"),
      ],
    ),
    CmdlineArgParser::Parser::Subcommand.new("version"),
  ]
)

Readme generation

TODO

Installation

Add this line to your application's Gemfile:

gem "cmdline_arg_parser"

And then execute:

$ bundle

Or install it yourself as:

$ gem install cmdline_arg_parser

Built by

@davidpdrsn