Skip to content

Instantly share code, notes, and snippets.

@leejarvis
Created March 16, 2011 17:41
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leejarvis/872910 to your computer and use it in GitHub Desktop.
Save leejarvis/872910 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'slop'
opts = Slop.parse do
banner "Usage: ruby foo.rb [options]\n"
on :n, :name, 'Your username', true
on :a, :age, 'Your age (optional)', :optional => true
on :V, :verbose, 'Run in verbose mode', :default => false
on :P, :people, 'Some people', true, :as => Array
on :h, :help, 'Print this help screen' do
puts help
end
end
# ruby foo.rb --name 'Lee Jarvis' -V --people john,phillip,dan
p opts.name? #=> true
p opts[:name] #=> "Lee Jarvis"
p opts.age? #=> false
p opts[:age] #=> nil
p opts.verbose? #=> true
p opts[:people] #=> ["john", "phillip", "dan"]
# grab an Option object
p opts.options[:people].description #=> "Some people"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment