Ruby Command Line Options

Ruby scripts can be executed with a variety of command-line options that can influence the behavior of your script or the Ruby interpreter. Here are some of the most commonly used options:

-e : Allows you to provide Ruby code directly from the command line.

ruby -e 'puts "Hello, world!"'

-p : Assume a loop around your script and print each line after it has been processed. This is useful for one-liners.

ruby -p -e '$_.upcase!' file.txt

This will print the contents of file.txt, but with all characters converted to uppercase.

-n : Assume a loop around your script, but do not automatically print each line.

ruby -n -e 'puts $_ if $_.include?("word")' file.txt

This will print lines from file.txt that include the string "word".

-l : Enable automatic line-ending processing. This sets $_ to have a newline character at the end of each line.

-r : Require a library before executing your script.

ruby -rjson -e 'puts JSON.parse("[1,2,3]").inspect'

This will require the json library and then parse a JSON string.

-v : Prints the version of Ruby that is currently being used.

ruby -v

-w : Turns warnings on for your script.

ruby -w script.rb

-d : Sets the debugging flag, $DEBUG, to true.

ruby -d script.rb

-I : Adds directories to the load path.

ruby -I /path/to/lib script.rb

This would make Ruby look in /path/to/lib when you require a file in script.rb.

These options can be combined as needed, and can be very useful for writing and debugging Ruby scripts. For a full list of command-line options, you can check out the Ruby man page by typing man ruby in your terminal, or check the official Ruby documentation.

  1. Ruby interpreter command line arguments:

    • Description: Command line arguments can be passed to the Ruby interpreter using ruby followed by the script name and arguments.

    • Code example:

      # script.rb
      puts "Arguments: #{ARGV.join(', ')}"
      

      Execute in the terminal:

      ruby script.rb arg1 arg2
      # Output: Arguments: arg1, arg2
      
  2. Ruby command line flags:

    • Description: Flags are special command line arguments preceded by dashes (-) or double dashes (--).

    • Code example:

      # script.rb
      flag_index = ARGV.index('-f')
      file_name = ARGV[flag_index + 1] if flag_index
      
      puts "File name: #{file_name}"
      

      Execute in the terminal:

      ruby script.rb -f myfile.txt
      # Output: File name: myfile.txt
      
  3. Ruby command line options examples:

    • Description: Command line options modify the behavior of a script and are often passed using flags.

    • Code example:

      # script.rb
      if ARGV.include?('--verbose')
        puts "Verbose mode enabled"
      else
        puts "Regular mode"
      end
      

      Execute in the terminal:

      ruby script.rb --verbose
      # Output: Verbose mode enabled
      
  4. Ruby -h command line:

    • Description: Conventionally, -h or --help is used to display help information about the script.
    • Code example:
      # script.rb
      if ARGV.include?('-h') || ARGV.include?('--help')
        puts "Help information: ..."
        exit
      end
      
      # Rest of the script
      
  5. Ruby command line help:

    • Description: The -h or --help option typically provides information on how to use the script and its options.
    • Code example:
      # script.rb
      if ARGV.include?('-h') || ARGV.include?('--help')
        puts "Usage: ruby script.rb [options]"
        puts "Options:"
        puts "  -h, --help   Show help information"
        puts "  -v, --version   Show script version"
        exit
      end
      
      # Rest of the script
      
  6. Ruby verbose mode command line:

    • Description: Enabling verbose mode can provide additional details or debugging information.
    • Code example:
      # script.rb
      if ARGV.include?('--verbose')
        $VERBOSE = true
        puts "Verbose mode enabled"
      end
      
      # Rest of the script
      
  7. Ruby debugging options command line:

    • Description: Debugging options, like --debug or -d, can be used to enable debugging features.
    • Code example:
      # script.rb
      if ARGV.include?('--debug') || ARGV.include?('-d')
        $DEBUG = true
        puts "Debug mode enabled"
      end
      
      # Rest of the script
      
  8. Ruby command line input arguments:

    • Description: Input arguments can be passed to a script for dynamic behavior.

    • Code example:

      # script.rb
      if ARGV.length >= 2
        input1 = ARGV[0]
        input2 = ARGV[1]
        puts "Inputs: #{input1}, #{input2}"
      else
        puts "Please provide at least two input arguments"
      end
      

      Execute in the terminal:

      ruby script.rb arg1 arg2
      # Output: Inputs: arg1, arg2
      
  9. Ruby command line script arguments:

    • Description: Arguments provided to a script can be accessed using the ARGV array.

    • Code example:

      # script.rb
      puts "Script arguments: #{ARGV.join(', ')}"
      

      Execute in the terminal:

      ruby script.rb arg1 arg2
      # Output: Script arguments: arg1, arg2
      
  10. Ruby execute script with command line options:

    • Description: Running a Ruby script with command line options allows customization.

    • Code example:

      # script.rb
      option = ARGV[0]
      case option
      when 'run'
        puts "Running the script"
      when 'test'
        puts "Running in test mode"
      else
        puts "Invalid option"
      end
      

      Execute in the terminal:

      ruby script.rb run
      # Output: Running the script
      
      ruby script.rb test
      # Output: Running in test mode
      
  11. Ruby command line option parsing:

    • Description: Libraries like OptionParser simplify command line option parsing.

    • Code example:

      require 'optparse'
      
      options = {}
      OptionParser.new do |opts|
        opts.banner = "Usage: ruby script.rb [options]"
      
        opts.on("-f", "--file FILE", "Specify a file") do |file|
          options[:file] = file
        end
      
        opts.on("-v", "--verbose", "Enable verbose mode") do
          options[:verbose] = true
        end
      end.parse!
      
      puts "File: #{options[:file]}" if options[:file]
      puts "Verbose mode enabled" if options[:verbose]
      

      Execute in the terminal:

      ruby script.rb -f myfile.txt --verbose
      # Output: File: myfile.txt
      #         Verbose mode enabled
      
  12. Ruby command line getopt:

    • Description: GetoptLong is another option parsing library in Ruby.

    • Code example:

      require 'getoptlong'
      
      opts = GetoptLong.new(
        ["--file", "-f", GetoptLong::REQUIRED_ARGUMENT],
        ["--verbose", "-v", GetoptLong::NO_ARGUMENT]
      )
      
      file = nil
      verbose = false
      
      opts.each do |opt, arg|
        case opt
        when '--file'
          file = arg
        when '--verbose'
          verbose = true
        end
      end
      
      puts "File: #{file}" if file
      puts "Verbose mode enabled" if verbose
      

      Execute in the terminal:

      ruby script.rb --file myfile.txt --verbose
      # Output: File: myfile.txt
      #         Verbose mode enabled
      
  13. Ruby ARGV array:

    • Description: The ARGV array holds command line arguments passed to a script.

    • Code example:

      # script.rb
      puts "Total arguments: #{ARGV.length}"
      puts "Arguments: #{ARGV.join(', ')}"
      

      Execute in the terminal:

      ruby script.rb arg1 arg2 arg3
      # Output: Total arguments: 3
      #         Arguments: arg1, arg2, arg3
      
  14. Ruby command line configuration:

    • Description: Configuring a script through command line options allows for flexibility.

    • Code example:

      # script.rb
      config = { verbose: false, log_file: nil }
      
      OptionParser.new do |opts|
        opts.on("-v", "--verbose", "Enable verbose mode") do
          config[:verbose] = true
        end
      
        opts.on("--log FILE", "Specify a log file") do |file|
          config[:log_file] = file
        end
      end.parse!
      
      puts "Verbose mode enabled" if config[:verbose]
      puts "Log file: #{config[:log_file]}" if config[:log_file]
      

      Execute in the terminal:

      ruby script.rb --verbose --log mylog.txt
      # Output: Verbose mode enabled
      #         Log file: mylog.txt