Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix page parsing for command line usage #131

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions lib/docsplit/command_line.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ def parse_options
opts.on('-o', '--output [DIR]', 'set the directory for all output') do |d|
@options[:output] = d
end
opts.on('-p', '--pages [PAGES]', "extract specific pages (eg: 5-10)") do |p|
@options[:pages] = p
opts.on('-p', '--pages [PAGES]', "extract specific pages (eg: 5-10 or 1,3,5)") do |p|
@options[:pages] = self.class.format_page_param(p)
end
opts.on('-s', '--size [SIZE]', 'set a fixed size (eg: 50x75)') do |s|
@options[:size] = s.split(',')
Expand Down Expand Up @@ -120,6 +120,19 @@ def parse_options
end
end

def self.format_page_param(pages_arg)
numbers_and_ranges = pages_arg.to_s.scan(/[\d\-]+/)
output = numbers_and_ranges.collect {|page_arg|
if page_arg.include?("-")
Range.new(*page_arg.split("-")).to_a
else
page_arg
end
}

output.flatten.uniq.map(&:to_i).sort
end

end

end
end
25 changes: 25 additions & 0 deletions test/unit/test_command_line.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
here = File.expand_path(File.dirname(__FILE__))
require File.join(here, '..', 'test_helper')
require "#{Docsplit::ROOT}/lib/docsplit/command_line"

class ConvertToPdfTest < Minitest::Test

def test_page_cli_parsing
input = "1,3-6,9,12,2,3"
output = Docsplit::CommandLine.format_page_param(input)
assert_equal [1,2,3,4,5,6,9,12], output

input = "3"
output = Docsplit::CommandLine.format_page_param(input)
assert_equal [3], output

input = "3-6"
output = Docsplit::CommandLine.format_page_param(input)
assert_equal [3, 4, 5, 6], output

input = "2,4,6,8"
output = Docsplit::CommandLine.format_page_param(input)
assert_equal [2, 4, 6, 8], output
end

end