Created
May 25, 2011 05:23
-
-
Save daytonn/990394 to your computer and use it in GitHub Desktop.
Basic optparse usage
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
command = ARGV[0] | |
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! | |
case command | |
when "sub_command" | |
#do sub command | |
else | |
puts %Q{ | |
myapp is a command line application | |
Usage: | |
myapp sub_command -o -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: myapp -m "Some string" | |
} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment