Comparison of Ruby command-line parsers
Ruby offers several popular command-line parsers—which one is best for your project?
![](https://terminalwire-assets.fly.storage.tigris.dev/Shared-Image-2025-02-04-17-03-31-sFNTRVvJyIj9wiLCfgSm9tqu17Fzi7EjgnKnlb3AyLtBOCoY4BO0OgpmyHcAv1HJ9Fu7IZ8MxJeAcJjmgT2fvV2WCgHqdvBS3JJ6.png)
Whether you realize it or not, you’ve probably used all of the command-line libraries that Ruby has to offer. Rails uses Thor to power its command line interface, Rake is dropped into every Rails project for developers to customize with their own tasks, OptionsParser is included with Ruby itself and runs their CLIs, and Bake is an alternative to Rake that addresses some of its shortcomings.
For this article we’ll look at a simple example where we try to have a hello world CLI that accepts a “name” argument and a “greeting” flag so you can see how both of those work in their respective libraries. The idea is we get as close to something like this as possible:
./greet world -g "hello"
Let’s see how this works.
Rake
Rake is a Ruby build tool that is used to automate tasks. It’s a great choice for building and deploying Ruby applications. Rake is also a great choice for building command line tools. Rake is a great choice for building command line tools because it’s easy to use and it’s easy to extend.
require "rake" task :default => :hello desc "Say hello" task :hello do puts "#{ENV.fetch("GREETING", "Hello")} #{ENV.fetch("NAME", "World")}!" end
Rake isn’t designed to be run directly; instead you run rake
and it looks for a Rakefile
in the current directory. Additionally, rake doesn’t have a built-in way to parse command line arguments, so you end up calling it via rake
and passing the arguments as environment variables.
NAME="John" GREETING="Hello" rake hello
One thing you’ll notice is we can’t
OptionsParser
A comparions of the most popular Ruby command line parsers wouldn’t be complete without the one that ships with Ruby itself. OptionsParser is a simple and easy to use command line parser that is included with Ruby. It’s not as feature rich as some of the other options, but it’s a great choice for simple command line parsing tasks.
#!/usr/bin/env ruby require 'optparse' options = {} OptionParser.new do |opts| opts.banner = "Usage: hi [options]" opts.on("-n", "--name NAME", "Name to greet") do |name| options[:name] = name end opts.on("-g", "--greeting GREETING", "Greeting to use") do |greeting| options[:greeting] = greeting end end.parse! puts "#{options[:greeting]} #{options[:name]}!"
To call it you’d run:
./hi greet John -g "hello"
And if you run it with nothing, you get:
Usage: options_parser.rb [options]
-n, --name NAME Name to greet
-g, --greeting GREETING Greeting to use
Thor
Thor is a Ruby command line parser that is used to build command line tools. It’s a great choice for building command line tools because it’s easy to use and it’s easy to extend. Thor is also a great choice for building command line tools because it’s easy to test.
#!/usr/bin/env ruby require 'thor' class MyCLI < Thor desc "greet NAME", "Say hello to NAME" option :greeting, type: :string, desc: "Greeting to use" def greet(name) puts "Hello, #{name}!" end end
To call it you’d run:
./hi greet John -g "hello"
Bake
Bake is a Ruby command line parser that is used to build command line tools. It’s a great choice for building command line tools because it’s easy to use and it’s easy to extend. Bake is also a great choice for building command line tools because it’s easy to test.
Support this blog 🤗
If you like what you read and want to see more articles like this, please consider using Terminalwire for your web application’s command-line interface. In under 10 minutes you can build a command-line in your favorite language and web framework, deploy it to your server, then stream it to the Terminalwire thin-client that runs on your users desktops. Terminalwire manages the binaries, installation, and updates, so you can focus on building a great CLI experience.