Skip to content

Instantly share code, notes, and snippets.

@Atalanta
Created February 15, 2011 21:56
Show Gist options
  • Save Atalanta/828351 to your computer and use it in GitHub Desktop.
Save Atalanta/828351 to your computer and use it in GitHub Desktop.
# Way to parse knife exec options....
# knife exec example.rb option:value option:value
# Gets converted to --option value --option value
require 'awesome_print'
require 'trollop'
def convert_input(arguments)
arguments.reject do |option|
option !~ /^[a-z]+:[a-z]{2,}-*[a-z]*-*\d*$/
end.map do |option|
opt, value = option.split(':')
[ '--' + opt, value ]
end.flatten
end
parser = Trollop::Parser.new do
opt :region, "Region to use", :default => "eu-west-1"
opt :release, "Release name", :default => "lucid"
opt :group, "Security group", :default => "default"
opt :size, "Size of instance", :default => "small"
opt :key, "Name of AWS keypair", :default => "gsg-opseng"
opt :ebs, "Should the instance be EBS backed?", :default => "no"
end
opts = Trollop::with_standard_exception_handling parser do
converted_args = convert_input(ARGV)
raise Trollop::HelpNeeded if converted_args.empty? # show help screen
o = parser.parse converted_args
o
end
ap opts
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment