Skip to content

0x1eef/cmd.rb

Repository files navigation

About

cmd.rb is a library for building command-line applications in Ruby. The interface is centered around a class that implements a command (or it could be a sub-command) that defines a banner, a set of options, and a set of defaults for those options. The option parsing implementation is delegated to ruby/optparse (with a few minor enhancements).

Example

The following example demonstrates a simple command that is implemented with Dir.entries. The command accepts two options that have fallback default values set for when an option is not given:

Definition

#!/usr/bin/env ruby

require "cmd"
class Ls < Cmd
  set_banner usage: "ls [OPTIONS]",
             description: "Lists the contents of a directory"
  set_option "-g PATTERN", "--grep PATTERN", "A regular expression", as: Regexp, default: /.+/
  set_option "-d PATH", "--directory PATH", "A path to a directory", as: String, default: Dir.home

  def run
    options = parse_options(argv)
    run_command(options)
  end

  private

  def run_command(options)
    puts Dir.entries(options.directory)
            .grep(options.grep)
            .sort
            .join("\n")
  end
end

##
# Run the command
Ls.new(ARGV).run

Command help (-h)

$ chmod u+x ls
$ ./ls --help
Usage: ls [OPTIONS]

Description:
  Lists the contents of a directory

Options:
  -g, --grep PATTERN         A regular expression
  -p, --path PATH            A path to a directory
  -h, --help                 Show help

Sources

Install

Git

cmd.rb is distributed as a RubyGem through its git repositories.
GitHub, and GitLab are available as sources.

# Gemfile
gem "cmd.rb", github: "0x1eef/cmd.rb"

Rubygems.org

cmd.rb can also be installed via rubygems.org.

gem install cmd.rb

BSD Zero Clause.
See LICENSE.

About

A library for building command-line applications in Ruby

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages