Advertisement
Guest User

Untitled

a guest
Dec 5th, 2011
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 1.53 KB | None | 0 0
  1.  #!/usr/bin/env ruby
  2.  # A script that will pretend to resize a number of images
  3.  require 'optparse'
  4.  
  5.  # This hash will hold all of the options
  6.  # parsed from the command-line by
  7.  # OptionParser.
  8.  options = {}
  9.  
  10.  optparse = OptionParser.new do|opts|
  11.    # Set a banner, displayed at the top
  12.    # of the help screen.
  13.    opts.banner = "Usage: optparse1.rb [options] file1 file2 ..."
  14.  
  15.    # Define the options, and what they do
  16.    options[:verbose] = false
  17.    opts.on( '-v', '--verbose', 'Output more information' ) do
  18.      options[:verbose] = true
  19.    end
  20.  
  21.    options[:quick] = false
  22.    opts.on( '-q', '--quick', 'Perform the task quickly' ) do
  23.      options[:quick] = true
  24.    end
  25.  
  26.    options[:logfile] = nil
  27.    opts.on( '-l', '--logfile FILE', 'Write log to FILE' ) do|file|
  28.      options[:logfile] = file
  29.    end
  30.  
  31.    # This displays the help screen, all programs are
  32.    # assumed to have this option.
  33.    opts.on( '-h', '--help', 'Display this screen' ) do
  34.      puts opts
  35.      exit
  36.    end
  37.  end
  38.  
  39.  # Parse the command-line. Remember there are two forms
  40.  # of the parse method. The 'parse' method simply parses
  41.  # ARGV, while the 'parse!' method parses ARGV and removes
  42.  # any options found there, as well as any parameters for
  43.  # the options. What's left is the list of files to resize.
  44.  optparse.parse!
  45.  
  46.  puts "Being verbose" if options[:verbose]
  47.  puts "Being quick" if options[:quick]
  48.  puts "Logging to file #{options[:logfile]}" if options[:logfile]
  49.  
  50.  ARGV.each do|f|
  51.    puts "Resizing image #{f}..."
  52.    sleep 0.5
  53.  end
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement