Created
July 11, 2012 15:41
-
-
Save mlafeldt/3091230 to your computer and use it in GitHub Desktop.
Skeleton for Ruby command-line app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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