#!/usr/bin/ruby -W0 # # This is a CLI interface to pastebin.ca # $Revision:$ # require 'mechanize' require 'optparse' # set up all the options stuff options = {} opts = OptionParser.new do |opts| opts.banner = "Usage: pastie.rb [options] Examples: pastie.rb -f foo.rb cat index.html | pastie.rb -t html" opts.separator "" opts.separator "Options:" opts.on("-f ", "--file ", Array, "Use a file for input instead of STDIN") do |f| options[:file] = f end syntax_hash = { "c" => "c", "diff" => "diff", "html" => "html", "javascript" => "javascript", "nitro xml" => "nitro_xml", "pascal" => "pascal", "plaintext" => "plain text", "rhtml" => "rhtml", "ruby" => "ruby", "sql" => "sql" } code_list = syntax_hash.keys.sort.join(", ") opts.on("-t", "--type ", syntax_hash, "Syntax types can be abbriviated, as long as they are unambigous. Defaults to 'ruby'", " #{code_list}") do |encoding| options[:syntax] = encoding end opts.on_tail("-h", "--help", "Show this message") do puts opts exit end opts.on_tail("--version", "Show version") do VERSION = "$Revision:$" puts VERSION exit end end # parse options opts.parse(ARGV) # get page and create the form object agent = WWW::Mechanize.new agent.user_agent_alias = 'Mac Safari' page = agent.get("http://pastie.caboo.se") form = page.forms[1] # use options to fill in form if options[:file] == "" form['paste[body]'] = $stdin.read else form['paste[body]'] = `cat #{options[:file]}` end if options.has_key?(:syntax) form['paste[parser]'] = options[:syntax] end # submit results and show link search_results = agent.submit(form) #puts search_results.body[/http:\/\/www.pastebin.ca\/\d+/]