Skip to content

Instantly share code, notes, and snippets.

@leejarvis
Created November 2, 2011 18:52
Show Gist options
  • Save leejarvis/1334515 to your computer and use it in GitHub Desktop.
Save leejarvis/1334515 to your computer and use it in GitHub Desktop.
require 'slop'
optspec = <<-SPEC
ruby foo.rb [options]
---
n,name= Set your name
a,age= Set your age
v,verbose Enable verbose mode
debug Enable debug mode
SPEC
opts = Slop.optspec(optspec)
opts.parse
puts opts #=>
# ruby foo.rb [options]
#
# options:
#
# -n, --name Set your name
# -a, --age Set your age
# -v, --verbose Enable verbose mode
# --debug Enable debug mode
# ruby foo.rb --name "Lee Jarvis" --debug -a 102
opts.debug? #=> true
opts[:name] #=> "Lee Jarvis"
opts[:age] #=> 102
opts.verbose? #=> false
@leejarvis
Copy link
Author

@alialshahib I found that to be a unique use case, because --options are usually just that, optional. You can handle it pretty easily yourself, though:

raise "You didn't supply the --omg option!" unless opts.omg?

If you want something to do this built into Slop, please open an issue with a suggestion :)

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