Skip to content

Instantly share code, notes, and snippets.

@sumdog
Created September 30, 2014 22:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sumdog/db0d4060990197876cc5 to your computer and use it in GitHub Desktop.
Save sumdog/db0d4060990197876cc5 to your computer and use it in GitHub Desktop.
Ruby syntax error parsing args
options = {}
opts = OptionParser.new do |opts|
opts.banner = 'Usage: create_env [-f] [-e (build|run)] [-d (mysql|postgres|mssql)] <environment name>'
opts.on_tail("-h", "--help", "Show this message") do
puts STDERR.opts
exit
end
opts.on('-f','--fixtures','Install fixtures') do |f|
options[:fixtures] = f
end
opts.on('-e','--env ENVIRONMENT',[:build,:run],'Environment type (build|run) [default: run]') do |e|
options[:environment] = e
end
opts.on('-d','--database DATABASE',[:mysql,:postgres,:mssql],'Database backend (mysql|postgres|mssql)') do |db|
options[:database] = db
end
end
## Validation
begin opts.parse! ARGV
rescue *[OptionParser::InvalidOption,OptionParser::InvalidArgument] => e
STDERR.puts e
STDERR.puts opts
exit 1
end
if ARGV.length == 0
STDERR.puts 'You must specify an environment name'.red
exit 1
elsif ARGV.length != 1
STDERR.puts ('Unknown trailing arguments: %s' %[ARGV.drop(1)]).red
exit 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment