Add URL to options

This commit is contained in:
Nemo 2020-03-29 02:44:48 +05:30
parent 2c07c5940d
commit d7043e233a
5 changed files with 30 additions and 5 deletions

View File

@ -2,7 +2,7 @@ require "./spec_helper"
describe Muse::Dl::Parser do describe Muse::Dl::Parser do
it "should parse options" do it "should parse options" do
parser = Muse::Dl::Parser.new(["--no-bookmarks", "--tmp-dir", "/tmp", "--no-cleanup", "--output", "file.pdf"]) parser = Muse::Dl::Parser.new(["--no-bookmarks", "--tmp-dir", "/tmp", "--no-cleanup", "--output", "file.pdf", "https://muse.jhu.edu/book/68534"])
parser.bookmarks.should eq false parser.bookmarks.should eq false
parser.tmp.should eq "/tmp" parser.tmp.should eq "/tmp"
parser.cleanup.should eq false parser.cleanup.should eq false
@ -10,10 +10,19 @@ describe Muse::Dl::Parser do
end end
it "should have reasonable defaults" do it "should have reasonable defaults" do
parser = Muse::Dl::Parser.new parser = Muse::Dl::Parser.new(["https://muse.jhu.edu/book/68534"])
parser.bookmarks.should eq true parser.bookmarks.should eq true
parser.cleanup.should eq true parser.cleanup.should eq true
parser.tmp.should eq "/tmp" parser.tmp.should eq "/tmp"
parser.output.should eq "tempfilename.pdf" parser.output.should eq "tempfilename.pdf"
parser.url.should eq "https://muse.jhu.edu/book/68534"
end
it "should raise error on missing options" do
begin
parser = Muse::Dl::Parser.new([] of String)
rescue e : Exception
e.class.should eq Muse::Dl::Errors::MissingLink
end
end end
end end

View File

@ -0,0 +1,4 @@
module Muse::Dl::Errors
class MissingLink < Exception
end
end

View File

@ -3,7 +3,7 @@ require "./errors/*"
module Muse::Dl module Muse::Dl
class Fetch class Fetch
def get_info(url : String) : (Muse::Dl::Book | Muse::Dl::Journal) def self.get_info(url : String) : (Muse::Dl::Book | Muse::Dl::Journal)
match = /https:\/\/muse.jhu.edu\/(book|journal)\/(\d+)/.match url match = /https:\/\/muse.jhu.edu\/(book|journal)\/(\d+)/.match url
if match if match
begin begin

View File

@ -11,7 +11,11 @@ module Muse::Dl
# TODO: Put your code here # TODO: Put your code here
class Main class Main
def self.run def self.run(args : Array(String))
parser = Parser.new(args)
Fetch.get_info(parser.url)
end end
end end
end end
Muse::Dl::Main.run(ARGV)

View File

@ -1,11 +1,14 @@
require "./errors/missing_link.cr"
module Muse::Dl module Muse::Dl
class Parser class Parser
@bookmarks : Bool @bookmarks : Bool
@tmp : String @tmp : String
@cleanup : Bool @cleanup : Bool
@output : String @output : String
@url : String | Nil
getter :bookmarks, :tmp, :cleanup, :output getter :bookmarks, :tmp, :cleanup, :output, :url
def find_next(arg : Array(String), flag : String, default) def find_next(arg : Array(String), flag : String, default)
search = arg.index flag search = arg.index flag
@ -21,6 +24,11 @@ module Muse::Dl
@cleanup = !arg.index "--no-cleanup" @cleanup = !arg.index "--no-cleanup"
@tmp = find_next(arg, "--tmp-dir", "/tmp") @tmp = find_next(arg, "--tmp-dir", "/tmp")
@output = find_next(arg, "--output", "tempfilename.pdf") @output = find_next(arg, "--output", "tempfilename.pdf")
begin
@url = arg[-1]
rescue e : Exception
raise Errors::MissingLink.new
end
end end
end end
end end