Created
May 25, 2011 04:59
-
-
Save daytonn/990367 to your computer and use it in GitHub Desktop.
Default bin file for a ruby gem
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 | |
require File.join(File.dirname(__FILE__), *%w".. lib mygem") | |
require 'optparse' | |
options = { | |
:help => false, | |
:flag => false, | |
:mandatory => nil | |
} | |
optparse = OptionParser.new do|opts| | |
opts.on( '-h', '--help', 'Display help screen' ) do | |
options[:help] = true | |
end | |
opts.on( '-f', '--flag', 'Simple flag') do | |
options[:flag] = true | |
end | |
opts.on( '-m', '--mandatory', 'Mandatory argument') do |mandatory| | |
options[:mandatory] = mandatory | |
end | |
end | |
optparse.parse! | |
command = ARGV[0] | |
case command | |
when "sub_command" | |
#do sub command | |
else | |
puts %Q{ | |
mygem is a command line application | |
Usage: | |
mygem sub_command -f -m "this is mandatory" | |
Options: | |
-h, --help - Display the help screen | |
-f, --flag - This flag toggles functionality | |
and can be used without a parameter | |
-m, --mandatory - When this option is used, there is a madatory argument. | |
You should give an example: mygem -m "Some string" | |
} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment