Skip to content

Instantly share code, notes, and snippets.

@alainravet
Created April 1, 2010 10:32
Show Gist options
  • Save alainravet/351637 to your computer and use it in GitHub Desktop.
Save alainravet/351637 to your computer and use it in GitHub Desktop.
# with Daemons, extract the params. for the daemonized process
#
# Input :
# ["run", "--", "--source=/tmp/sour", "--archive=/tmp/arch"]
# ^ ^^^^^^^^^ ^^^^^^^^^
# | :source :archive
# the separator
#
# Output :
# {:source=>"/tmp/sour", :archive=>"/tmp/arch"}
#
def extract_process_params(argv)
return {} unless argv.include?('--')
idx = argv.index('--')
argv_useful = argv[idx+1..-1]
# argv_useful = ["--source=/tmp/sour", "--archive=/tmp/arch"]
choices_a = (argv_useful || []).collect do |p|
if p =~ /--source=(.*)/
[:source, $1]
elsif p =~ /--archive=(.*)/
[:archive, $1]
end
end.compact
# choices_a = [[:source, "/tmp/sour"], [:archive, "/tmp/arch"]]
choices_h = Hash[*choices_a.flatten]
# {:source=>"/tmp/sour", :archive=>"/tmp/arch"}
end
argv = ["run", "--", "--source=/tmp/sour", "--archive=/tmp/arch"]
puts extract_process_params(argv).inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment