Skip to content

Instantly share code, notes, and snippets.

@ursm
Created December 27, 2008 23:32
Show Gist options
  • Select an option

  • Save ursm/40331 to your computer and use it in GitHub Desktop.

Select an option

Save ursm/40331 to your computer and use it in GitHub Desktop.
out = `rake --help`
sss = out.lines.map(&:strip).grep(/^-/).map {|s|
opt = /(?:-¥w|--[-¥w]+?)/
arg = /¥[?([A-Z]+)¥]?/
s.match(/(#{opt}(?:, #{opt})*)(?: #{arg})? {2,}(.*)/).to_a[1..-1]
}.reject(&:nil?)
hs = sss.map {|opts, arg, desc|
opts.split(/, /).inject(:short => [], :long => []) {|h, opt|
case opt
when /^--([-¥w]+)$/
h.tap {|i| i[:long] << $1 }
when /^-(¥w)$/
h.tap {|i| i[:short] << $1 }
else
raise opt
end
}.merge(:arg => arg, :desc => desc)
}
puts hs.map {|h|
short = h[:short].map {|s| "-s #{s}" }
long = h[:long].map {|s| "-l #{s}" }
desc = (d = h[:desc]) ? "-d #{d.inspect}" : ''
['complete -c rake', short, long, desc].reject(&:empty?).join(' ')
}.join("¥n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment