Skip to content

Instantly share code, notes, and snippets.

@postmodern
Last active December 31, 2015 05:19
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save postmodern/7939687 to your computer and use it in GitHub Desktop.
Save postmodern/7939687 to your computer and use it in GitHub Desktop.
Experiment to see if OptionParser allows options to define other options. Turns out you can!
require 'optparse'
options = OptionParser.new("usage: #{$0}") do |opts|
opts.on('-f','--file FILE','loads a file') do |file|
puts "loading file #{file}"
opts.separator "#{file} options:"
opts.on('-c','--custom') do
puts "custom option defined by file #{file}"
end
end
end
options.parse(ARGV)
@postmodern
Copy link
Author

$ ruby test.rb -f foo
loading file foo
$ ruby test.rb -f foo -c
loading file foo
custom option defined by file foo
$ ruby test.rb --help
usage: test.rb
    -f, --file FILE                  loads a file
$ ruby test.rb -f foo --help
loading file foo
usage: test.rb
    -f, --file FILE                  loads a file
foo options:
    -c, --custom

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment