Skip to content

Instantly share code, notes, and snippets.

@mlafeldt
Created July 11, 2012 15:41
Show Gist options
  • Save mlafeldt/3091230 to your computer and use it in GitHub Desktop.
Save mlafeldt/3091230 to your computer and use it in GitHub Desktop.
Skeleton for Ruby command-line app
#!/usr/bin/env ruby
#/ SYNOPSIS
#
# Run `CMD --help` for full documentation.
#
# Written by NAME
require 'optparse'
options = {
:foo => false,
:bar => nil
}
ARGV.options do |opts|
opts.banner = "Usage: #{opts.program_name} [<options>]"
opts.separator ""
opts.separator "Options:"
opts.on("-f", "--foo", "some random foo option") do
options[:foo] = true
end
opts.on("-b", "--bar BAR", "some random bar option") do |bar|
options[:bar] = bar
end
opts.on("-h", "--help", "show help text") do
puts opts
exit 0
end
opts.separator ""
opts.separator File.readlines(__FILE__).grep(/^#\/.*/).map { |line| line.chomp[3..-1] }.join("\n")
begin
opts.parse!
rescue => e
abort "error: #{e}"
end
end
# Do something useful with options and arguments here
p options
p ARGV
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment