dreamsys software

Ruby Scripting Tutorial

TOC | Introduction | Variables | If/Else | OptionParser | Looping | Functions | Classes | Strings | Lists & Maps | Shell


The ruby class optparse.OptionParser, is a powerful tool for creating options for your script. In our previous example, we just had the user enter two command line arguments to the ruby script, without specifying which is which. Most users expect to be able to give parameters in any specific order and specify which is which, this can be done in ruby very easily. Let's change our years.rb script to show how:

# Years till 100
require 'optparse'

options = {:name => nil, :age => nil}

parser = OptionParser.new do|opts|
	opts.banner = "Usage: years.rb [options]"
	opts.on('-n', '--name name', 'Name') do |name|
		options[:name] = name;

	opts.on('-a', '--age age', 'Age') do |age|
		options[:age] = age;

	opts.on('-h', '--help', 'Displays Help') do
		puts opts


if options[:name] == nil
	print 'Enter Name: '
    options[:name] = gets.chomp

if options[:age] == nil
	print 'Enter Age: '
    options[:age] = gets.chomp

sayHello = 'Hello ' + options[:name] + ', '

if Integer(options[:age]) == 100
    sayAge = 'You are already 100 years old!'
elsif Integer(options[:age]) < 100
    sayAge = 'You will be 100 in ' + String(100 - Integer(options[:age])) + ' years!'
    sayAge = 'You turned 100 ' + String(Integer(options[:age]) - 100) + ' years ago!'

puts sayHello + sayAge

The first thing we do in this script is add a hash. This hash is where we will store the options. We could also use variables to store the options. Notice that we create a hash with the values specified inline. By default the values are null, but we have two keys in the hash, name and age.

This script requires the optparse package in order to make use of the class OptionParser. The OptionParse class will allow you to add options to your script and it will generate a help option based on the options you provide. In this example, we are adding three options: -n (or --name), -a (or --age) and -h (or --help). The first parameter to on is the short option and the 2nd is the long option, it is quite common in the Unix & Linux environment to add a short and long version of an option. The final parameter is the help text which is the help text generated when the user uses the -h option.

Finally, after adding the options and setting up the banner text for the help option, we call the parse function, which will run the parser and our handler code. We can access the variables stored in the hash when adding options on the options object. So it will have two options, options[:name] and options[:age]. If one of the variables wasn't passed in, we can check by using the "if options[:name] == nil" condition. Then we will load from user input as before.

Now, there are several ways to run this script:

./years.rbPrompts for user and age
./years.rb -n JoeSets user, prompts for age
./years.rb --name JoeSets user, prompts for age
./years.rb -a 25Sets age, prompts for user
./years.rb --age 25Sets age, prompts for user
./years.rb -a 25 --name JoeSets age, sets user
./years.rb -n Joe --age 25Sets age, sets user

Another thing you can do now is run the help option, by specifying either -h or --help:

./years.rb -h

This will give the following output and then exit the script:

Usage: years.rb [options]
    -n, --name name                  Name
    -a, --age age                    Age
    -h, --help                       Displays Help

Now that our script is complete, we can move on to more interesting topics. In the next section we will cover looping.

Prev (If/Else) | Next (Looping)