Rails Insights

Command Line Options in Ruby CLI

Introduction

When working with Ruby command line applications, it's important to understand how to handle command line options. Command line options allow users to customize the behavior of a CLI program by passing arguments when running the program. In this article, we will explore how to work with command line options in Ruby CLI applications.

Using OptionParser

Ruby provides a built-in library called OptionParser that makes it easy to define and parse command line options. OptionParser allows you to define the options that your program accepts and automatically parses the command line arguments passed by the user.

Basic Usage

Here's a simple example of how to use OptionParser in a Ruby CLI program:

require 'optparse'

options = {}
OptionParser.new do |opts|
  opts.banner = "Usage: example.rb [options]"

  opts.on("-v", "--verbose", "Enable verbose mode") do
    options[:verbose] = true
  end

  opts.on("-n", "--name NAME", "Specify a name") do |name|
    options[:name] = name
  end
end.parse!

puts "Options: #{options}"

In this example, we define two options: --verbose and --name. The --verbose option enables verbose mode, while the --name option allows the user to specify a name. The opts.on method is used to define each option, specifying the short and long forms of the option as well as a description. The block passed to opts.on is executed when the option is encountered, allowing you to store the option value in the options hash.

Handling Arguments

OptionParser also allows you to define options that require arguments. In the previous example, the --name option requires the user to specify a name when using the option. The argument passed to the option is captured in the block passed to opts.on as a parameter.

Here's an example of how to handle arguments with OptionParser:

require 'optparse'

options = {}
OptionParser.new do |opts|
  opts.on("-n", "--name NAME", "Specify a name") do |name|
    options[:name] = name
  end
end.parse!

puts "Name: #{options[:name]}"

In this example, the user can specify a name by passing the --name option followed by the name as an argument. The name argument is captured in the block and stored in the options hash for later use.

Advanced Usage

OptionParser provides a wide range of features for defining and parsing command line options. Here are some advanced usage examples:

Default Values

You can specify default values for options by providing an initial value when defining the options in OptionParser. If the user does not provide a value for the option, the default value will be used.

require 'optparse'

options = { name: "John" }
OptionParser.new do |opts|
  opts.on("-n", "--name NAME", "Specify a name") do |name|
    options[:name] = name
  end
end.parse!

puts "Name: #{options[:name]}"

In this example, the --name option has a default value of "John". If the user does not provide a name when running the program, the default value will be used.

Required Options

You can mark options as required by using the required method. If a required option is not provided by the user, OptionParser will raise an error.

require 'optparse'

options = {}
OptionParser.new do |opts|
  opts.on("-n", "--name NAME", "Specify a name") do |name|
    options[:name] = name
  end

  opts.on("-a", "--age AGE", Integer, "Specify an age") do |age|
    options[:age] = age
  end

  opts.required("-n", "--name NAME", "Name is required")
end.parse!

puts "Name: #{options[:name]}"
puts "Age: #{options[:age]}"

In this example, the --name option is marked as required. If the user does not provide a name when running the program, an error will be raised.

Conclusion

Command line options are a powerful feature of Ruby CLI applications that allow users to customize the behavior of a program. By using OptionParser, you can easily define and parse command line options in your Ruby CLI programs. Experiment with different options and features of OptionParser to create flexible and user-friendly CLI applications.

Published: June 09, 2024

© 2024 RailsInsights. All rights reserved.