added an option_parser class to use with the command line binary

This commit is contained in:
booboy 2018-11-09 01:51:21 -06:00
parent 5aa45e28c3
commit 4c43f54e96
2 changed files with 52 additions and 38 deletions

View file

@ -1,10 +1,9 @@
#!/usr/bin/env ruby
require '../lib/pastebinner'
require 'optparse'
require '../lib/option_parser'
# setup our object and grab a session key
pb = Pastebinner.new(ENV['pastebin_api_key'], ENV['pastebin_username'], ENV['pastebin_password'])
api_dev_key = ENV['pastebin_api_key']
# pass in the Pastebinner.new client.
# will download all of the raw pastes from the public scrape results into each own file in data dir.
@ -16,51 +15,23 @@ def download_pastes(pb)
end
end
opts = {}
options = OptionParser.parse!
OptionParser.new do |parser|
parser.banner = "Usage: pastebinner [options]"
parser.on('-h', '--help', "Show this help message") do ||
puts parser
exit
end
parser.on('-s', '--scrape_public', "Scrape public pastes") do |s|
opts[:s] = true
end
parser.on('-r', '--raw', "Raw paste. Requires --key passed with a valid key") do |r|
opts[:r] = true
end
parser.on('-k', '--key=', "Unique paste key") do |k|
opts[:k] = k
end
parser.on('-g', '--get_keys', "Get unique paste keys from public pastes") do |g|
opts[:g] = true
end
parser.on('-d', '--download', 'Download all public pastes to data directory.') do |d|
opts[:d] = true
end
parser.on('-t', '--trending', 'Trending pastes.') do |t|
opts[:t] = true
end
end.parse!
if opts[:s]
if options[:s]
puts pb.scrape_public_pastes
elsif opts[:r] and opts[:k]
key = opts[:k]
elsif options[:r] and options[:k]
key = options[:k]
puts pb.raw_paste_data(key)
elsif opts[:t]
elsif options[:t]
puts pb.list_trending_pastes
elsif opts[:g]
elsif options[:g]
r = pb.scrape_public_pastes
puts pb.get_unique_paste_keys(r)
elsif opts[:d]
elsif options[:d]
puts "Downloading paste data into the data directory..."
download_pastes(pb)
puts "Complete."
else opts = false
else options = false
puts 'please provide arguments'
exit
end

43
lib/option_parser.rb Executable file
View file

@ -0,0 +1,43 @@
require 'optparse'
class OptionParser
def self.parse!(argv = ARGV)
options = {}
OptParse.new do |opts|
opts.default_argv = argv
opts.banner = "Usage: pastebinner [options]"
opts.on('-h', '--help', 'Show this help messae') do ||
puts opts
exit
end
opts.on('-s', '--scrape_public', 'Scrape public pastes') do |s|
options[:s] = true
end
opts.on('-r', '--raw', 'Raw paste. Requires --key passed with a valid key') do |r|
options[:r] = true
end
opts.on('-g', '--get_keys', 'Get unique paste keys from public pastes') do |g|
options[:g] = true
end
opts.on('-k', '--key=', 'Unique paste key') do |k|
options[:k] = k
end
opts.on('-d', '--download', 'Download all public pastes to data directory') do |d|
options[:d] = true
end
opts.on('-t', '--trending', 'Trending pastes') do |t|
options[:t] = true
end
opts.parse!
end
options
end
end