New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

cmd.rb

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cmd.rb

  • 0.5.1
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

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

License

BSD Zero Clause.
See LICENSE.

FAQs

Package last updated on 27 Mar 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc