mirror of https://github.com/captn3m0/muse-dl.git
52 lines
1.4 KiB
Crystal
52 lines
1.4 KiB
Crystal
require "option_parser"
|
|
require "dir"
|
|
|
|
module Muse::Dl
|
|
class Parser
|
|
@bookmarks = true
|
|
@tmp : String
|
|
@cleanup = true
|
|
@output = "tempfilename.pdf"
|
|
@url = "INVALID_URL"
|
|
|
|
getter :bookmarks, :tmp, :cleanup, :output, :url
|
|
|
|
def find_next(arg : Array(String), flag : String, default)
|
|
search = arg.index flag
|
|
if search
|
|
return arg[search + 1]
|
|
end
|
|
|
|
return default
|
|
end
|
|
|
|
def initialize(arg : Array(String) = [] of String)
|
|
@tmp = Dir.tempdir
|
|
|
|
parser = OptionParser.new
|
|
parser.banner = "Usage: muse-dl [--flags] URL"
|
|
parser.on(long_flag = "--no-cleanup", description = "Don't cleanup temporary files") { @cleanup = false }
|
|
parser.on(long_flag = "--tmp-dir PATH", description = "Temporary Directory to use") { |path| @tmp = path }
|
|
parser.on(long_flag = "--output FILE", description = "Output Filename") { |file| @output = file }
|
|
parser.on(long_flag = "--no-bookmarks", description = "Don't add bookmarks in the PDF") { @bookmarks = false }
|
|
parser.on("-h", "--help", "Show this help") { puts parser }
|
|
|
|
parser.unknown_args do |args|
|
|
if args.size != 1
|
|
puts parser
|
|
exit 1
|
|
end
|
|
@url = args[0]
|
|
end
|
|
|
|
parser.invalid_option do |flag|
|
|
STDERR.puts "ERROR: #{flag} is not a valid option."
|
|
STDERR.puts parser
|
|
exit(1)
|
|
end
|
|
|
|
parser.parse(arg)
|
|
end
|
|
end
|
|
end
|